Complete set refactor for schema-converter-shex

main
Jackson Morgan 6 months ago
parent e86b552f24
commit 9392014e8c
  1. 1
      packages/ldo/src/index.ts
  2. 10
      packages/schema-converter-shex/src/typing/ShexJTypingTransformer.ts
  3. 2
      packages/schema-converter-shex/src/typing/shexjToTyping.ts
  4. 33
      packages/schema-converter-shex/src/typing/util/dedupeObjectTypeMembers.ts
  5. 2
      packages/schema-converter-shex/test/testData/activityPub.ts
  6. 2
      packages/schema-converter-shex/test/testData/andSimple.ts
  7. 2
      packages/schema-converter-shex/test/testData/circular.ts
  8. 49
      packages/schema-converter-shex/test/testData/eachOfAndSimple.ts
  9. 2
      packages/schema-converter-shex/test/testData/extendsSimple.ts
  10. 2
      packages/schema-converter-shex/test/testData/oldExtends.ts
  11. 2
      packages/schema-converter-shex/test/testData/orSimple.ts
  12. 2
      packages/schema-converter-shex/test/testData/profile.ts
  13. 2
      packages/schema-converter-shex/test/testData/reducedProfile.ts
  14. 2
      packages/schema-converter-shex/test/testData/reusedPredicates.ts
  15. 2
      packages/schema-converter-shex/test/testData/simple.ts
  16. 2
      packages/schema-converter-shex/test/testData/testData.ts

@ -8,3 +8,4 @@ export * from "./createLdoDataset";
import type { LdoBase as LdoBaseImport } from "./util"; import type { LdoBase as LdoBaseImport } from "./util";
export type LdoBase = LdoBaseImport; export type LdoBase = LdoBaseImport;
export * from "./types"; export * from "./types";
export { LdSet, LdoJsonldContext, set } from "@ldo/jsonld-dataset-proxy";

@ -222,7 +222,7 @@ export const ShexJTypingTransformer = ShexJTraverser.createTransformer<
tripleConstraint.predicate, tripleConstraint.predicate,
rdfTypes[0], rdfTypes[0],
); );
const isArray = const isSet =
tripleConstraint.max !== undefined && tripleConstraint.max !== 1; tripleConstraint.max !== undefined && tripleConstraint.max !== 1;
const isOptional = tripleConstraint.min === 0; const isOptional = tripleConstraint.min === 0;
let type: dom.Type = dom.type.any; let type: dom.Type = dom.type.any;
@ -232,7 +232,13 @@ export const ShexJTypingTransformer = ShexJTraverser.createTransformer<
const propertyDeclaration = dom.create.property( const propertyDeclaration = dom.create.property(
propertyName, propertyName,
isArray ? dom.type.array(type) : type, isSet
? {
kind: "name",
name: "LdSet",
typeArguments: [type],
}
: type,
isOptional ? dom.DeclarationFlags.Optional : dom.DeclarationFlags.None, isOptional ? dom.DeclarationFlags.Optional : dom.DeclarationFlags.None,
); );

@ -47,7 +47,7 @@ export async function shexjToTyping(
}; };
}); });
const typingsString = const typingsString =
`import {ContextDefinition} from "jsonld"\n\n` + `import { LdSet, LdoJsonldContext } from "@ldo/ldo"\n\n` +
typings.map((typing) => `export ${typing.typingString}`).join(""); typings.map((typing) => `export ${typing.typingString}`).join("");
const typeingReturn: TypeingReturn = { const typeingReturn: TypeingReturn = {

@ -10,24 +10,22 @@ export function dedupeObjectTypeMembers(
// Combine properties if they're duplicates // Combine properties if they're duplicates
if (properties[propertyDeclaration.name]) { if (properties[propertyDeclaration.name]) {
const oldPropertyDeclaration = properties[propertyDeclaration.name]; const oldPropertyDeclaration = properties[propertyDeclaration.name];
const oldPropertyTypeAsArray = const oldPropertyType = isLdSetType(oldPropertyDeclaration.type)
oldPropertyDeclaration.type as dom.ArrayTypeReference; ? oldPropertyDeclaration.type.typeArguments[0]
const oldProeprtyType = : oldPropertyDeclaration.type;
oldPropertyTypeAsArray.kind === "array" const propertyType = isLdSetType(propertyDeclaration.type)
? oldPropertyTypeAsArray.type ? propertyDeclaration.type.typeArguments[0]
: oldPropertyDeclaration.type; : propertyDeclaration.type;
const propertyTypeAsArray =
propertyDeclaration.type as dom.ArrayTypeReference;
const propertyType =
propertyTypeAsArray.kind === "array"
? propertyTypeAsArray.type
: propertyDeclaration.type;
const isOptional = const isOptional =
propertyDeclaration.flags === dom.DeclarationFlags.Optional || propertyDeclaration.flags === dom.DeclarationFlags.Optional ||
oldPropertyDeclaration.flags === dom.DeclarationFlags.Optional; oldPropertyDeclaration.flags === dom.DeclarationFlags.Optional;
properties[propertyDeclaration.name] = dom.create.property( properties[propertyDeclaration.name] = dom.create.property(
propertyDeclaration.name, propertyDeclaration.name,
dom.type.array(dom.create.union([oldProeprtyType, propertyType])), {
kind: "name",
name: "LdSet",
typeArguments: [dom.create.union([oldPropertyType, propertyType])],
},
isOptional ? dom.DeclarationFlags.Optional : dom.DeclarationFlags.None, isOptional ? dom.DeclarationFlags.Optional : dom.DeclarationFlags.None,
); );
// Set JS Comment // Set JS Comment
@ -42,3 +40,12 @@ export function dedupeObjectTypeMembers(
}); });
return Object.values(properties); return Object.values(properties);
} }
function isLdSetType(
potentialLdSet: dom.Type,
): potentialLdSet is dom.NamedTypeReference {
return (
(potentialLdSet as dom.NamedTypeReference).kind === "name" &&
(potentialLdSet as dom.NamedTypeReference).name === "LdSet"
);
}

File diff suppressed because one or more lines are too long

@ -43,5 +43,5 @@ export const andSimple: TestData = {
Image: "https://example.com/Image", Image: "https://example.com/Image",
}, },
successfulTypings: successfulTypings:
'import {ContextDefinition} from "jsonld"\n\nexport interface MediaContainerShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "MediaContainer";\n };\n videoImage: VideoShape & ImageShape;\n}\n\nexport interface VideoShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Video";\n };\n}\n\nexport interface ImageShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Image";\n };\n}\n\n', 'import { LdSet, LdoJsonldContext } from "@ldo/ldo"\n\nexport interface MediaContainerShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "MediaContainer";\n };\n videoImage: VideoShape & ImageShape;\n}\n\nexport interface VideoShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Video";\n };\n}\n\nexport interface ImageShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Image";\n };\n}\n\n',
}; };

@ -47,5 +47,5 @@ export const circular: TestData = {
}, },
}, },
successfulTypings: successfulTypings:
'import {ContextDefinition} from "jsonld"\n\nexport interface ParentShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type?: {\n "@id": "Parent";\n };\n hasChild: ChildShape;\n}\n\nexport interface ChildShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type?: {\n "@id": "Child";\n };\n hasParent: ParentShape;\n}\n\n', 'import { LdSet, LdoJsonldContext } from "@ldo/ldo"\n\nexport interface ParentShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type?: {\n "@id": "Parent";\n };\n hasChild: ChildShape;\n}\n\nexport interface ChildShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type?: {\n "@id": "Child";\n };\n hasParent: ParentShape;\n}\n\n',
}; };

@ -0,0 +1,49 @@
import type { TestData } from "./testData";
/**
* EACH OF AND SIMPLE
*/
export const eachOfAndSimple: TestData = {
name: "eachOfAndSimple",
shexc: `
PREFIX ex: <https://example.com/>
ex:MediaContainerShape {
a [ ex:MediaContainer ];
ex:videoImage @ex:VideoShape * ;
ex:videoImage @ex:ImageShape * ;
}
ex:VideoShape {
a [ ex:Video ];
}
ex:ImageShape {
a [ ex:Image ];
}
`,
sampleTurtle: "",
baseNode: "",
successfulContext: {
MediaContainer: {
"@id": "https://example.com/MediaContainer",
"@context": {
type: {
"@id": "@type",
},
videoImage: {
"@id": "https://example.com/videoImage",
"@type": "@id",
"@isCollection": true,
},
},
},
type: {
"@id": "@type",
},
Video: "https://example.com/Video",
Image: "https://example.com/Image",
},
successfulTypings:
'import { LdSet, LdoJsonldContext } from "@ldo/ldo"\n\nexport interface MediaContainerShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "MediaContainer";\n };\n videoImage?: LdSet<VideoShape | ImageShape>;\n}\n\nexport interface VideoShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Video";\n };\n}\n\nexport interface ImageShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Image";\n };\n}\n\n',
};

@ -69,5 +69,5 @@ export const extendsSimple: TestData = {
}, },
}, },
successfulTypings: successfulTypings:
'import {ContextDefinition} from "jsonld"\n\nexport interface EntityShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Entity";\n };\n entityId: any;\n}\n\nexport interface PersonShape {\n "@id"?: (string | string)[];\n "@context"?: (ContextDefinition | ContextDefinition)[];\n type: ({\n "@id": "Entity";\n } | {\n "@id": "Person";\n })[];\n entityId: any;\n name: any;\n}\n\nexport interface EmployeeShape {\n "@id"?: (string | string | string)[];\n "@context"?: (ContextDefinition | ContextDefinition | ContextDefinition)[];\n type: ({\n "@id": "Entity";\n } | {\n "@id": "Person";\n } | {\n "@id": "Employee";\n })[];\n entityId: any;\n name: any;\n employeeNumber: any;\n}\n\n', 'import { LdSet, LdoJsonldContext } from "@ldo/ldo"\n\nexport interface EntityShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Entity";\n };\n entityId: any;\n}\n\nexport interface PersonShape {\n "@id"?: LdSet<string | string>;\n "@context"?: LdSet<ContextDefinition | ContextDefinition>;\n type: LdSet<{\n "@id": "Entity";\n } | {\n "@id": "Person";\n }>;\n entityId: any;\n name: any;\n}\n\nexport interface EmployeeShape {\n "@id"?: LdSet<string | string | string>;\n "@context"?: LdSet<ContextDefinition | ContextDefinition | ContextDefinition>;\n type: LdSet<{\n "@id": "Entity";\n } | {\n "@id": "Person";\n } | {\n "@id": "Employee";\n }>;\n entityId: any;\n name: any;\n employeeNumber: any;\n}\n\n',
}; };

@ -74,5 +74,5 @@ export const oldExtends: TestData = {
}, },
}, },
successfulTypings: successfulTypings:
'import {ContextDefinition} from "jsonld"\n\nexport interface EntityShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Entity";\n };\n entityId: any;\n}\n\nexport interface PersonShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: ({\n "@id": "Entity";\n } | {\n "@id": "Person";\n })[];\n entityId: any;\n name: any;\n}\n\nexport interface EmployeeShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: ({\n "@id": "Entity";\n } | {\n "@id": "Person";\n } | {\n "@id": "Employee";\n })[];\n entityId: any;\n name: any;\n employeeNumber: any;\n}\n\n', 'import { LdSet, LdoJsonldContext } from "@ldo/ldo"\n\nexport interface EntityShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Entity";\n };\n entityId: any;\n}\n\nexport interface PersonShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: LdSet<{\n "@id": "Entity";\n } | {\n "@id": "Person";\n }>;\n entityId: any;\n name: any;\n}\n\nexport interface EmployeeShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: LdSet<{\n "@id": "Entity";\n } | {\n "@id": "Person";\n } | {\n "@id": "Employee";\n }>;\n entityId: any;\n name: any;\n employeeNumber: any;\n}\n\n',
}; };

@ -49,5 +49,5 @@ export const orSimple: TestData = {
Image: "https://example.com/Image", Image: "https://example.com/Image",
}, },
successfulTypings: successfulTypings:
'import {ContextDefinition} from "jsonld"\n\nexport interface MediaContainerShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "MediaContainer";\n };\n primaryMedia: VideoShape | ImageShape;\n media?: (VideoShape | ImageShape)[];\n}\n\nexport interface VideoShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Video";\n };\n}\n\nexport interface ImageShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Image";\n };\n}\n\n', 'import { LdSet, LdoJsonldContext } from "@ldo/ldo"\n\nexport interface MediaContainerShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "MediaContainer";\n };\n primaryMedia: VideoShape | ImageShape;\n media?: LdSet<VideoShape | ImageShape>;\n}\n\nexport interface VideoShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Video";\n };\n}\n\nexport interface ImageShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Image";\n };\n}\n\n',
}; };

File diff suppressed because one or more lines are too long

@ -219,5 +219,5 @@ srs:EmailShape EXTRA a {
}, },
}, },
successfulTypings: successfulTypings:
'import {ContextDefinition} from "jsonld"\n\nexport interface SolidProfileShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n /**\n * Defines the node as a Person | Defines the node as a Person\n */\n type: ({\n "@id": "Person";\n } | {\n "@id": "Person2";\n })[];\n /**\n * The person\'s email.\n */\n hasEmail?: (EmailShape)[];\n /**\n * An alternate way to define a person\'s name\n */\n name?: string;\n}\n\nexport interface EmailShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n /**\n * The type of email.\n */\n type?: {\n "@id": "Dom";\n } | {\n "@id": "Home";\n } | {\n "@id": "ISDN";\n } | {\n "@id": "Internet";\n } | {\n "@id": "Intl";\n } | {\n "@id": "Label";\n } | {\n "@id": "Parcel";\n } | {\n "@id": "Postal";\n } | {\n "@id": "Pref";\n } | {\n "@id": "Work";\n } | {\n "@id": "X400";\n };\n /**\n * The value of an email as a mailto link (Example <mailto:jane@example.com>)\n */\n value: {\n "@id": string;\n };\n}\n\n', 'import { LdSet, LdoJsonldContext } from "@ldo/ldo"\n\nexport interface SolidProfileShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n /**\n * Defines the node as a Person | Defines the node as a Person\n */\n type: LdSet<{\n "@id": "Person";\n } | {\n "@id": "Person2";\n }>;\n /**\n * The person\'s email.\n */\n hasEmail?: LdSet<EmailShape>;\n /**\n * An alternate way to define a person\'s name\n */\n name?: string;\n}\n\nexport interface EmailShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n /**\n * The type of email.\n */\n type?: {\n "@id": "Dom";\n } | {\n "@id": "Home";\n } | {\n "@id": "ISDN";\n } | {\n "@id": "Internet";\n } | {\n "@id": "Intl";\n } | {\n "@id": "Label";\n } | {\n "@id": "Parcel";\n } | {\n "@id": "Postal";\n } | {\n "@id": "Pref";\n } | {\n "@id": "Work";\n } | {\n "@id": "X400";\n };\n /**\n * The value of an email as a mailto link (Example <mailto:jane@example.com>)\n */\n value: {\n "@id": string;\n };\n}\n\n',
}; };

@ -86,5 +86,5 @@ export const reusedPredicates: TestData = {
}, },
}, },
successfulTypings: successfulTypings:
'import {ContextDefinition} from "jsonld"\n\nexport interface DocumentShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Document";\n };\n vocabulary?: (VocabularyShape)[];\n law: LawShape;\n}\n\nexport interface LawShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Law";\n };\n name?: string[];\n path: {\n "@id": string;\n };\n}\n\nexport interface VocabularyShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Vocabulary";\n };\n name: string;\n path?: {\n "@id": string;\n }[];\n}\n\n', 'import { LdSet, LdoJsonldContext } from "@ldo/ldo"\n\nexport interface DocumentShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Document";\n };\n vocabulary?: LdSet<VocabularyShape>;\n law: LawShape;\n}\n\nexport interface LawShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Law";\n };\n name?: LdSet<string>;\n path: {\n "@id": string;\n };\n}\n\nexport interface VocabularyShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n type: {\n "@id": "Vocabulary";\n };\n name: string;\n path?: LdSet<{\n "@id": string;\n }>;\n}\n\n',
}; };

@ -46,5 +46,5 @@ export const simple: TestData = {
mbox: { "@id": "http://xmlns.com/foaf/0.1/mbox", "@type": "@id" }, mbox: { "@id": "http://xmlns.com/foaf/0.1/mbox", "@type": "@id" },
}, },
successfulTypings: successfulTypings:
'import {ContextDefinition} from "jsonld"\n\nexport interface EmployeeShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n givenName: string[];\n familyName: string;\n phone?: {\n "@id": string;\n }[];\n mbox: {\n "@id": string;\n };\n}\n\n', 'import { LdSet, LdoJsonldContext } from "@ldo/ldo"\n\nexport interface EmployeeShape {\n "@id"?: string;\n "@context"?: ContextDefinition;\n givenName: LdSet<string>;\n familyName: string;\n phone?: LdSet<{\n "@id": string;\n }>;\n mbox: {\n "@id": string;\n };\n}\n\n',
}; };

@ -9,6 +9,7 @@ import { reusedPredicates } from "./reusedPredicates";
import { oldExtends } from "./oldExtends"; import { oldExtends } from "./oldExtends";
import { orSimple } from "./orSimple"; import { orSimple } from "./orSimple";
import { andSimple } from "./andSimple"; import { andSimple } from "./andSimple";
import { eachOfAndSimple } from "./eachOfAndSimple";
export interface TestData { export interface TestData {
name: string; name: string;
@ -30,4 +31,5 @@ export const testData: TestData[] = [
reusedPredicates, reusedPredicates,
orSimple, orSimple,
andSimple, andSimple,
eachOfAndSimple,
]; ];

Loading…
Cancel
Save