Skip to content

Commit

Permalink
Fix invalid ontology generation by cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Polleps committed Oct 3, 2024
1 parent 4d5419f commit 6047a67
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 15 deletions.
6 changes: 6 additions & 0 deletions browser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ This changelog covers all five packages, as they are (for now) updated as a whol

- BREAKING CHANGE: removed the `importJsonAdString` function.
- Added `store.importJsonAD()` method.
- Added support for commonJS modules.

### @tomic/cli

- Fix shortnames in externals.ts are not converted to camelCase.
- Filter out duplicate classes and properties in generated types.

### @tomic/create-template

Expand Down
12 changes: 7 additions & 5 deletions browser/cli/src/generateBaseObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Resource, urls } from '@tomic/lib';
import { core, Resource } from '@tomic/lib';
import { store } from './store.js';
import { camelCaseify } from './utils.js';
import { camelCaseify, dedupe } from './utils.js';

export type ReverseMapping = Record<string, string>;

Expand All @@ -16,8 +16,10 @@ export const generateBaseObject = async (
throw ontology.error;
}

const classes = ontology.get(urls.properties.classes) as string[];
const properties = ontology.get(urls.properties.properties) as string[];
const classes = dedupe(ontology.get(core.properties.classes)) as string[];
const properties = dedupe(
ontology.get(core.properties.properties),
) as string[];
const name = camelCaseify(ontology.title);

const baseObj = {
Expand All @@ -36,7 +38,7 @@ export const generateBaseObject = async (
const listToObj = async (list: string[]): Promise<Record<string, string>> => {
const entries = await Promise.all(
list.map(async subject => {
const resource = await store.getResourceAsync(subject);
const resource = await store.getResource(subject);

return [camelCaseify(resource.title), subject];
}),
Expand Down
5 changes: 3 additions & 2 deletions browser/cli/src/generateClasses.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Core, Resource } from '@tomic/lib';
import { type Core, type Resource } from '@tomic/lib';
import { store } from './store.js';
import { ReverseMapping } from './generateBaseObject.js';
import { PropertyRecord } from './PropertyRecord.js';
import { dedupe } from './utils.js';

export const generateClasses = (
ontology: Resource<Core.Ontology>,
reverseMapping: ReverseMapping,
propertyRecord: PropertyRecord,
): string => {
const classes = ontology.props.classes ?? [];
const classes = dedupe(ontology.props.classes ?? []);

const classStringList = classes.map(subject => {
return generateClass(subject, reverseMapping, propertyRecord);
Expand Down
8 changes: 4 additions & 4 deletions browser/cli/src/generateExternals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const generateTypeMapping = (properties: Resource<Core.Property>[]) => {
const lines = properties.map(prop => {
const type = DatatypeToTSTypeMap[prop.props.datatype as Datatype];

return `["${prop.getSubject()}"]: ${type};`;
return `["${prop.subject}"]: ${type};`;
});

return lines.join('\n');
Expand All @@ -48,7 +48,7 @@ const generateNameMapping = (properties: Resource<Core.Property>[]) => {
const lines = properties.map(prop => {
const name = camelCaseify(prop.props.shortname);

return `["${prop.getSubject()}"]: "${name}";`;
return `["${prop.subject}"]: "${name}";`;
});

return lines.join('\n');
Expand All @@ -58,15 +58,15 @@ const generateBaseObjectProperties = (
properties: Resource<Core.Property>[],
) => {
const lines = properties.map(
p => `${p.props.shortname}: '${p.getSubject()}',`,
p => `${camelCaseify(p.props.shortname)}: '${p.subject}',`,
);

return lines.join('\n');
};

export const generateExternals = async (props: string[]) => {
const properties: Resource<Core.Property>[] = await Promise.all(
props.map(p => store.getResourceAsync<Core.Property>(p)),
props.map(p => store.getResource<Core.Property>(p)),
);

const baseOjbectProperties = generateBaseObjectProperties(properties);
Expand Down
4 changes: 4 additions & 0 deletions browser/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ export const camelCaseify = (str: string) =>
str.replace(/-([a-z])/g, g => {
return g[1].toUpperCase();
});

export const dedupe = <T>(array: T[]): T[] => {
return Array.from(new Set(array));
};
1 change: 0 additions & 1 deletion browser/data-browser/src/components/forms/ResourceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ export function ResourceForm({
setSubject={set => {
handleAddProp(set);
}}
error={newPropErr}
isA={core.classes.property}
/>
</div>
Expand Down
6 changes: 5 additions & 1 deletion browser/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
],
"gitHead": "2172c73d8df4e5f273e6386676abc91b6c5b2707",
"license": "MIT",
"main": "dist/index.js",
"exports": {
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"types": "./dist/src/index.d.ts"
},
"main-dev": "src/index.ts",
"name": "@tomic/lib",
"publishConfig": {
Expand Down
4 changes: 2 additions & 2 deletions browser/react/src/components/Image.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Resource, Server } from '@tomic/lib';
import { type Resource, type Server, unknownSubject, server } from '@tomic/lib';
import React from 'react';
import { server, unknownSubject, useResource, useString } from '../index.js';
import { useResource, useString } from '../index.js';

const imageFormatsWithBasicSupport = new Set([
'image/svg+xml',
Expand Down

0 comments on commit 6047a67

Please sign in to comment.