parent
2db70df471
commit
3188733329
@ -0,0 +1,8 @@ |
|||||||
|
import type { CompactSchema } from "@ldo/ldo"; |
||||||
|
|
||||||
|
/** |
||||||
|
* ============================================================================= |
||||||
|
* <%- fileName %>Schema: Compact Schema for <%- fileName %> |
||||||
|
* ============================================================================= |
||||||
|
*/ |
||||||
|
export const <%- fileName %>Schema: CompactSchema = <%- compactSchema %>; |
@ -1,4 +1,6 @@ |
|||||||
import { shexjToTyping } from "./typing/shexjToTyping.js"; |
import { shexjToTyping } from "./typing/shexjToTyping.js"; |
||||||
export { annotateReadablePredicates } from "./util/annotateReadablePredicates.js"; |
export { annotateReadablePredicates } from "./util/annotateReadablePredicates.js"; |
||||||
|
export type { CompactShape } from "./schema/ShexJSchemaTransformerCompact.js"; |
||||||
|
export { ShexJSchemaTransformerCompact } from "./schema/ShexJSchemaTransformerCompact.js"; |
||||||
|
export type { CompactSchema } from "./typing/shexjToTypingCompact.js"; |
||||||
export default shexjToTyping; |
export default shexjToTyping; |
||||||
|
@ -0,0 +1,209 @@ |
|||||||
|
import type { ObjectLiteral } from "@ldo/traverser-shexj"; |
||||||
|
import ShexJTraverser from "@ldo/traverser-shexj"; |
||||||
|
|
||||||
|
export interface CompactShape { |
||||||
|
schemaUri: string; |
||||||
|
predicates: CompactSchemaProperty[]; |
||||||
|
} |
||||||
|
|
||||||
|
type NodeConstraintRet = { |
||||||
|
literals?: number[] | string[] | boolean; |
||||||
|
type: "number" | "string" | "boolean" | "literal"; |
||||||
|
}; |
||||||
|
|
||||||
|
interface CompactSchemaProperty { |
||||||
|
type: "number" | "string" | "boolean" | "nested" | "literal"; |
||||||
|
predicateUri: string; |
||||||
|
readablePredicate: string; |
||||||
|
literalValue?: number | string | boolean | number[] | string[]; |
||||||
|
nestedSchema?: string | CompactShape; |
||||||
|
maxCardinality: number; |
||||||
|
minCardinality: number; |
||||||
|
} |
||||||
|
|
||||||
|
export const ShexJSchemaTransformerCompact = ShexJTraverser.createTransformer< |
||||||
|
{ |
||||||
|
Schema: { return: CompactShape[] }; |
||||||
|
ShapeDecl: { return: CompactShape }; |
||||||
|
Shape: { return: CompactShape }; |
||||||
|
EachOf: { return: CompactShape }; |
||||||
|
TripleConstraint: { return: CompactSchemaProperty }; |
||||||
|
NodeConstraint: { return: NodeConstraintRet }; |
||||||
|
ShapeOr: { return: never }; |
||||||
|
ShapeAnd: { return: never }; |
||||||
|
ShapeNot: { return: never }; |
||||||
|
ShapeExternal: { return: never }; |
||||||
|
}, |
||||||
|
null |
||||||
|
>({ |
||||||
|
// Transformer from Schema to interfaces
|
||||||
|
Schema: { |
||||||
|
transformer: async (_schema, getTransformedChildren) => { |
||||||
|
const transformedChildren = await getTransformedChildren(); |
||||||
|
|
||||||
|
return transformedChildren.shapes || []; |
||||||
|
}, |
||||||
|
}, |
||||||
|
|
||||||
|
// Transformer from ShapeDecl to interface
|
||||||
|
ShapeDecl: { |
||||||
|
transformer: async (shapeDecl, getTransformedChildren) => { |
||||||
|
const schema = await getTransformedChildren(); |
||||||
|
|
||||||
|
return { ...schema.shapeExpr, schemaUri: shapeDecl.id } as CompactShape; |
||||||
|
}, |
||||||
|
}, |
||||||
|
|
||||||
|
// Transformer from Shape to interface
|
||||||
|
Shape: { |
||||||
|
transformer: async (_shape, getTransformedChildren, setReturnPointer) => { |
||||||
|
// TODO: We don't handles those
|
||||||
|
_shape.closed; |
||||||
|
_shape.extra; |
||||||
|
|
||||||
|
const transformedChildren = await getTransformedChildren(); |
||||||
|
// Return prelim or expression or assign things?
|
||||||
|
return transformedChildren.expression as CompactShape; |
||||||
|
}, |
||||||
|
}, |
||||||
|
|
||||||
|
// Transformer from EachOf to object type. EachOf contains the `expressions` array of properties (TripleConstraint)
|
||||||
|
EachOf: { |
||||||
|
transformer: async (eachOf, getTransformedChildren, setReturnPointer) => { |
||||||
|
const transformedChildren = await getTransformedChildren(); |
||||||
|
|
||||||
|
return { |
||||||
|
schemaUri: "", |
||||||
|
predicates: transformedChildren.expressions.map( |
||||||
|
// We disregard cases where properties are referenced (strings)
|
||||||
|
// or where they consist of Unions or Intersections (not supported).
|
||||||
|
(expr) => expr as CompactSchemaProperty, |
||||||
|
), |
||||||
|
}; |
||||||
|
}, |
||||||
|
}, |
||||||
|
|
||||||
|
// Transformer from triple constraints to type properties.
|
||||||
|
TripleConstraint: { |
||||||
|
transformer: async ( |
||||||
|
tripleConstraint, |
||||||
|
getTransformedChildren, |
||||||
|
_setReturnPointer, |
||||||
|
) => { |
||||||
|
const transformedChildren = await getTransformedChildren(); |
||||||
|
|
||||||
|
const commonProperties = { |
||||||
|
maxCardinality: tripleConstraint.max ?? 1, |
||||||
|
minCardinality: tripleConstraint.min ?? 1, |
||||||
|
predicateUri: tripleConstraint.predicate, |
||||||
|
readablePredicate: tripleConstraint.readablePredicate, |
||||||
|
}; |
||||||
|
// Make property based on object type which is either a parsed schema, literal or type.
|
||||||
|
if (typeof transformedChildren.valueExpr === "string") { |
||||||
|
// Reference to nested object
|
||||||
|
return { |
||||||
|
type: "nested", |
||||||
|
nestedSchema: transformedChildren.valueExpr, |
||||||
|
...commonProperties, |
||||||
|
} satisfies CompactSchemaProperty; |
||||||
|
} else if ( |
||||||
|
transformedChildren.valueExpr && |
||||||
|
(transformedChildren.valueExpr as CompactShape).predicates |
||||||
|
) { |
||||||
|
// Nested object
|
||||||
|
return { |
||||||
|
type: "nested", |
||||||
|
nestedSchema: transformedChildren.valueExpr as CompactShape, |
||||||
|
...commonProperties, |
||||||
|
} satisfies CompactSchemaProperty; |
||||||
|
} else { |
||||||
|
// type or literal
|
||||||
|
const nodeConstraint = |
||||||
|
transformedChildren.valueExpr as NodeConstraintRet; |
||||||
|
return { |
||||||
|
type: nodeConstraint.type, |
||||||
|
literalValue: nodeConstraint.literals, |
||||||
|
...commonProperties, |
||||||
|
} satisfies CompactSchemaProperty; |
||||||
|
} |
||||||
|
}, |
||||||
|
}, |
||||||
|
|
||||||
|
// Transformer from node constraint to type
|
||||||
|
NodeConstraint: { |
||||||
|
transformer: async (nodeConstraint) => { |
||||||
|
if (nodeConstraint.datatype) { |
||||||
|
switch (nodeConstraint.datatype) { |
||||||
|
case "http://www.w3.org/2001/XMLSchema#boolean": |
||||||
|
return { type: "boolean" }; |
||||||
|
case "http://www.w3.org/2001/XMLSchema#byte": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#decimal": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#double": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#float": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#int": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#integer": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#long": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#negativeInteger": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#nonNegativeInteger": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#nonPositiveInteger": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#positiveInteger": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#short": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#unsignedLong": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#unsignedInt": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#unsignedShort": |
||||||
|
case "http://www.w3.org/2001/XMLSchema#unsignedByte": |
||||||
|
return { type: "number" }; |
||||||
|
default: |
||||||
|
return { type: "string" }; // treat most as string
|
||||||
|
} |
||||||
|
} |
||||||
|
if (nodeConstraint.nodeKind) { |
||||||
|
// Something reference-like.
|
||||||
|
return { type: "string" }; |
||||||
|
} |
||||||
|
if (nodeConstraint.values) { |
||||||
|
return { |
||||||
|
type: "literal", |
||||||
|
literals: nodeConstraint.values.map( |
||||||
|
// TODO: We do not convert them to number or boolean or lang tag.
|
||||||
|
(valueRecord) => (valueRecord as ObjectLiteral).value, |
||||||
|
), |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
// Maybe we should throw instead...
|
||||||
|
throw { |
||||||
|
error: new Error("Could not parse Node Constraint"), |
||||||
|
nodeConstraint, |
||||||
|
}; |
||||||
|
}, |
||||||
|
}, |
||||||
|
|
||||||
|
// Transformer from ShapeOr to union type
|
||||||
|
ShapeOr: { |
||||||
|
transformer: async () => { |
||||||
|
throw new Error("ShapeOr not supported (compact)"); |
||||||
|
}, |
||||||
|
}, |
||||||
|
|
||||||
|
// Transformer from ShapeAnd to intersection type
|
||||||
|
ShapeAnd: { |
||||||
|
transformer: async () => { |
||||||
|
throw new Error("ShapeAnd not supported (compact)"); |
||||||
|
}, |
||||||
|
}, |
||||||
|
|
||||||
|
// Transformer from ShapeNot to type - not supported.
|
||||||
|
ShapeNot: { |
||||||
|
transformer: async () => { |
||||||
|
throw new Error("ShapeNot not supported (compact)"); |
||||||
|
}, |
||||||
|
}, |
||||||
|
|
||||||
|
// Transformer from ShapeExternal to type - not supported.
|
||||||
|
ShapeExternal: { |
||||||
|
transformer: async () => { |
||||||
|
throw new Error("ShapeExternal not supported (compact)"); |
||||||
|
}, |
||||||
|
}, |
||||||
|
}); |
Loading…
Reference in new issue