|
|
@ -13,11 +13,11 @@ export interface CompactTransformerContext { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function commentFromAnnotations( |
|
|
|
function commentFromAnnotations( |
|
|
|
annotations?: Annotation[] |
|
|
|
annotations?: Annotation[], |
|
|
|
): string | undefined { |
|
|
|
): string | undefined { |
|
|
|
const commentAnnotationObject = annotations?.find( |
|
|
|
const commentAnnotationObject = annotations?.find( |
|
|
|
(annotation) => |
|
|
|
(annotation) => |
|
|
|
annotation.predicate === "http://www.w3.org/2000/01/rdf-schema#comment" |
|
|
|
annotation.predicate === "http://www.w3.org/2000/01/rdf-schema#comment", |
|
|
|
)?.object; |
|
|
|
)?.object; |
|
|
|
if (typeof commentAnnotationObject === "string") |
|
|
|
if (typeof commentAnnotationObject === "string") |
|
|
|
return commentAnnotationObject; |
|
|
|
return commentAnnotationObject; |
|
|
@ -73,7 +73,7 @@ function resolveCollisions(props: dom.PropertyDeclaration[]): void { |
|
|
|
if (list.length < 2) return; |
|
|
|
if (list.length < 2) return; |
|
|
|
|
|
|
|
|
|
|
|
const predicateIris = new Set( |
|
|
|
const predicateIris = new Set( |
|
|
|
list.map((p) => predicateIriByProp.get(p)).filter(Boolean) |
|
|
|
list.map((p) => predicateIriByProp.get(p)).filter(Boolean), |
|
|
|
); |
|
|
|
); |
|
|
|
if (predicateIris.size < 2) { |
|
|
|
if (predicateIris.size < 2) { |
|
|
|
return; |
|
|
|
return; |
|
|
@ -92,7 +92,7 @@ function resolveCollisions(props: dom.PropertyDeclaration[]): void { |
|
|
|
// Detect any remaining duplicates after first pass
|
|
|
|
// Detect any remaining duplicates after first pass
|
|
|
|
const nameCounts = new Map<string, number>(); |
|
|
|
const nameCounts = new Map<string, number>(); |
|
|
|
list.forEach((p) => |
|
|
|
list.forEach((p) => |
|
|
|
nameCounts.set(p.name, (nameCounts.get(p.name) || 0) + 1) |
|
|
|
nameCounts.set(p.name, (nameCounts.get(p.name) || 0) + 1), |
|
|
|
); |
|
|
|
); |
|
|
|
list.forEach((p) => { |
|
|
|
list.forEach((p) => { |
|
|
|
if (nameCounts.get(p.name)! > 1) { |
|
|
|
if (nameCounts.get(p.name)! > 1) { |
|
|
@ -110,7 +110,7 @@ function resolveCollisions(props: dom.PropertyDeclaration[]): void { |
|
|
|
// - if both are Set<A>, Set<B> -> Set<A|B>
|
|
|
|
// - if both are Set<A>, Set<B> -> Set<A|B>
|
|
|
|
// - preserve optional flag if any occurrence optional
|
|
|
|
// - preserve optional flag if any occurrence optional
|
|
|
|
function dedupeCompactProperties( |
|
|
|
function dedupeCompactProperties( |
|
|
|
props: dom.PropertyDeclaration[] |
|
|
|
props: dom.PropertyDeclaration[], |
|
|
|
): dom.PropertyDeclaration[] { |
|
|
|
): dom.PropertyDeclaration[] { |
|
|
|
const byName: Record<string, dom.PropertyDeclaration> = {}; |
|
|
|
const byName: Record<string, dom.PropertyDeclaration> = {}; |
|
|
|
const isSetRef = (t: dom.Type): t is dom.NamedTypeReference => |
|
|
|
const isSetRef = (t: dom.Type): t is dom.NamedTypeReference => |
|
|
@ -169,7 +169,7 @@ function dedupeCompactProperties( |
|
|
|
let mergedType: dom.Type; |
|
|
|
let mergedType: dom.Type; |
|
|
|
if (existingSet && newSet) { |
|
|
|
if (existingSet && newSet) { |
|
|
|
mergedType = toSet( |
|
|
|
mergedType = toSet( |
|
|
|
makeUnion(getSetInner(existing.type), getSetInner(p.type)) |
|
|
|
makeUnion(getSetInner(existing.type), getSetInner(p.type)), |
|
|
|
); |
|
|
|
); |
|
|
|
} else if (existingSet && !newSet) { |
|
|
|
} else if (existingSet && !newSet) { |
|
|
|
mergedType = toSet(makeUnion(getSetInner(existing.type), p.type)); |
|
|
|
mergedType = toSet(makeUnion(getSetInner(existing.type), p.type)); |
|
|
@ -241,7 +241,7 @@ export const ShexJTypingTransformerCompact = ShexJTraverser.createTransformer< |
|
|
|
shapeInterface.shapeId = shapeDecl.id; |
|
|
|
shapeInterface.shapeId = shapeDecl.id; |
|
|
|
if ( |
|
|
|
if ( |
|
|
|
!shapeInterface.members.find( |
|
|
|
!shapeInterface.members.find( |
|
|
|
(m) => m.kind === "property" && m.name === "id" |
|
|
|
(m) => m.kind === "property" && m.name === "id", |
|
|
|
) |
|
|
|
) |
|
|
|
) { |
|
|
|
) { |
|
|
|
shapeInterface.members.unshift( |
|
|
|
shapeInterface.members.unshift( |
|
|
@ -249,14 +249,14 @@ export const ShexJTypingTransformerCompact = ShexJTraverser.createTransformer< |
|
|
|
"id", |
|
|
|
"id", |
|
|
|
dom.create.namedTypeReference("IRI"), |
|
|
|
dom.create.namedTypeReference("IRI"), |
|
|
|
// Root interfaces should have mandatory id
|
|
|
|
// Root interfaces should have mandatory id
|
|
|
|
dom.DeclarationFlags.None |
|
|
|
dom.DeclarationFlags.None, |
|
|
|
) |
|
|
|
), |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
return shapeInterface; |
|
|
|
return shapeInterface; |
|
|
|
} |
|
|
|
} |
|
|
|
throw new Error( |
|
|
|
throw new Error( |
|
|
|
"Unsupported direct shape expression on ShapeDecl for compact format." |
|
|
|
"Unsupported direct shape expression on ShapeDecl for compact format.", |
|
|
|
); |
|
|
|
); |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
@ -275,14 +275,14 @@ export const ShexJTypingTransformerCompact = ShexJTraverser.createTransformer< |
|
|
|
"interface") |
|
|
|
"interface") |
|
|
|
) { |
|
|
|
) { |
|
|
|
newInterface.members.push( |
|
|
|
newInterface.members.push( |
|
|
|
...(transformedChildren.expression as dom.ObjectType).members |
|
|
|
...(transformedChildren.expression as dom.ObjectType).members, |
|
|
|
); |
|
|
|
); |
|
|
|
} else if ( |
|
|
|
} else if ( |
|
|
|
(transformedChildren.expression as dom.PropertyDeclaration)?.kind === |
|
|
|
(transformedChildren.expression as dom.PropertyDeclaration)?.kind === |
|
|
|
"property" |
|
|
|
"property" |
|
|
|
) { |
|
|
|
) { |
|
|
|
newInterface.members.push( |
|
|
|
newInterface.members.push( |
|
|
|
transformedChildren.expression as dom.PropertyDeclaration |
|
|
|
transformedChildren.expression as dom.PropertyDeclaration, |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
if (transformedChildren.extends) { |
|
|
|
if (transformedChildren.extends) { |
|
|
@ -291,11 +291,11 @@ export const ShexJTypingTransformerCompact = ShexJTraverser.createTransformer< |
|
|
|
if (extInt.kind === "interface") { |
|
|
|
if (extInt.kind === "interface") { |
|
|
|
const merged = [ |
|
|
|
const merged = [ |
|
|
|
...extInt.members.filter( |
|
|
|
...extInt.members.filter( |
|
|
|
(m) => !(m.kind === "property" && m.name === "id") |
|
|
|
(m) => !(m.kind === "property" && m.name === "id"), |
|
|
|
), |
|
|
|
), |
|
|
|
...newInterface.members, |
|
|
|
...newInterface.members, |
|
|
|
].filter( |
|
|
|
].filter( |
|
|
|
(m): m is dom.PropertyDeclaration => m.kind === "property" |
|
|
|
(m): m is dom.PropertyDeclaration => m.kind === "property", |
|
|
|
); |
|
|
|
); |
|
|
|
newInterface.members = dedupeCompactProperties(merged); |
|
|
|
newInterface.members = dedupeCompactProperties(merged); |
|
|
|
} |
|
|
|
} |
|
|
@ -339,7 +339,7 @@ export const ShexJTypingTransformerCompact = ShexJTraverser.createTransformer< |
|
|
|
if ((m as any).kind === "property") { |
|
|
|
if ((m as any).kind === "property") { |
|
|
|
inputProps.push(m as dom.PropertyDeclaration); |
|
|
|
inputProps.push(m as dom.PropertyDeclaration); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
@ -357,13 +357,13 @@ export const ShexJTypingTransformerCompact = ShexJTraverser.createTransformer< |
|
|
|
getTransformedChildren, |
|
|
|
getTransformedChildren, |
|
|
|
_setReturnPointer, |
|
|
|
_setReturnPointer, |
|
|
|
node, |
|
|
|
node, |
|
|
|
context |
|
|
|
context, |
|
|
|
) => { |
|
|
|
) => { |
|
|
|
const transformedChildren = await getTransformedChildren(); |
|
|
|
const transformedChildren = await getTransformedChildren(); |
|
|
|
const rdfTypes = getRdfTypesForTripleConstraint(node); |
|
|
|
const rdfTypes = getRdfTypesForTripleConstraint(node); |
|
|
|
const baseName = context.getNameFromIri( |
|
|
|
const baseName = context.getNameFromIri( |
|
|
|
tripleConstraint.predicate, |
|
|
|
tripleConstraint.predicate, |
|
|
|
rdfTypes[0] |
|
|
|
rdfTypes[0], |
|
|
|
); |
|
|
|
); |
|
|
|
const max = tripleConstraint.max; |
|
|
|
const max = tripleConstraint.max; |
|
|
|
const isPlural = max === -1 || (max !== undefined && max !== 1); |
|
|
|
const isPlural = max === -1 || (max !== undefined && max !== 1); |
|
|
@ -379,7 +379,7 @@ export const ShexJTypingTransformerCompact = ShexJTraverser.createTransformer< |
|
|
|
) { |
|
|
|
) { |
|
|
|
valueType = dom.create.objectType( |
|
|
|
valueType = dom.create.objectType( |
|
|
|
(valueType as dom.InterfaceDeclaration) |
|
|
|
(valueType as dom.InterfaceDeclaration) |
|
|
|
.members as dom.PropertyDeclaration[] |
|
|
|
.members as dom.PropertyDeclaration[], |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -407,7 +407,7 @@ export const ShexJTypingTransformerCompact = ShexJTraverser.createTransformer< |
|
|
|
const hasPrim = u.members.some(isPrimitiveLike); |
|
|
|
const hasPrim = u.members.some(isPrimitiveLike); |
|
|
|
if (isPlural && hasObj && hasPrim) { |
|
|
|
if (isPlural && hasObj && hasPrim) { |
|
|
|
throw new Error( |
|
|
|
throw new Error( |
|
|
|
`Mixed plural union (object + primitive) not supported for predicate ${tripleConstraint.predicate}` |
|
|
|
`Mixed plural union (object + primitive) not supported for predicate ${tripleConstraint.predicate}`, |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -444,15 +444,15 @@ export const ShexJTypingTransformerCompact = ShexJTraverser.createTransformer< |
|
|
|
m as unknown as { members?: dom.PropertyDeclaration[] } |
|
|
|
m as unknown as { members?: dom.PropertyDeclaration[] } |
|
|
|
).members; |
|
|
|
).members; |
|
|
|
const hasId = (anonMembers || []).some( |
|
|
|
const hasId = (anonMembers || []).some( |
|
|
|
(mm) => mm.name === "id" |
|
|
|
(mm) => mm.name === "id", |
|
|
|
); |
|
|
|
); |
|
|
|
if (!hasId && anonMembers) { |
|
|
|
if (!hasId && anonMembers) { |
|
|
|
anonMembers.unshift( |
|
|
|
anonMembers.unshift( |
|
|
|
dom.create.property( |
|
|
|
dom.create.property( |
|
|
|
"id", |
|
|
|
"id", |
|
|
|
dom.create.namedTypeReference("IRI"), |
|
|
|
dom.create.namedTypeReference("IRI"), |
|
|
|
dom.DeclarationFlags.None |
|
|
|
dom.DeclarationFlags.None, |
|
|
|
) |
|
|
|
), |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -470,8 +470,8 @@ export const ShexJTypingTransformerCompact = ShexJTraverser.createTransformer< |
|
|
|
dom.create.property( |
|
|
|
dom.create.property( |
|
|
|
"id", |
|
|
|
"id", |
|
|
|
dom.create.namedTypeReference("IRI"), |
|
|
|
dom.create.namedTypeReference("IRI"), |
|
|
|
dom.DeclarationFlags.None |
|
|
|
dom.DeclarationFlags.None, |
|
|
|
) |
|
|
|
), |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
valueForRecord = anon as dom.Type; |
|
|
|
valueForRecord = anon as dom.Type; |
|
|
@ -508,8 +508,8 @@ export const ShexJTypingTransformerCompact = ShexJTraverser.createTransformer< |
|
|
|
dom.create.property( |
|
|
|
dom.create.property( |
|
|
|
"id", |
|
|
|
"id", |
|
|
|
dom.create.namedTypeReference("IRI"), |
|
|
|
dom.create.namedTypeReference("IRI"), |
|
|
|
dom.DeclarationFlags.None |
|
|
|
dom.DeclarationFlags.None, |
|
|
|
) |
|
|
|
), |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -527,8 +527,8 @@ export const ShexJTypingTransformerCompact = ShexJTraverser.createTransformer< |
|
|
|
dom.create.property( |
|
|
|
dom.create.property( |
|
|
|
"id", |
|
|
|
"id", |
|
|
|
dom.create.namedTypeReference("IRI"), |
|
|
|
dom.create.namedTypeReference("IRI"), |
|
|
|
dom.DeclarationFlags.None |
|
|
|
dom.DeclarationFlags.None, |
|
|
|
) |
|
|
|
), |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -541,7 +541,7 @@ export const ShexJTypingTransformerCompact = ShexJTraverser.createTransformer< |
|
|
|
(valueType as dom.InterfaceDeclaration).name |
|
|
|
(valueType as dom.InterfaceDeclaration).name |
|
|
|
) { |
|
|
|
) { |
|
|
|
finalType = dom.create.namedTypeReference( |
|
|
|
finalType = dom.create.namedTypeReference( |
|
|
|
(valueType as dom.InterfaceDeclaration).name |
|
|
|
(valueType as dom.InterfaceDeclaration).name, |
|
|
|
); |
|
|
|
); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
finalType = valueType; |
|
|
|
finalType = valueType; |
|
|
@ -551,7 +551,7 @@ export const ShexJTypingTransformerCompact = ShexJTraverser.createTransformer< |
|
|
|
const prop = dom.create.property( |
|
|
|
const prop = dom.create.property( |
|
|
|
baseName, |
|
|
|
baseName, |
|
|
|
finalType, |
|
|
|
finalType, |
|
|
|
isOptional ? dom.DeclarationFlags.Optional : dom.DeclarationFlags.None |
|
|
|
isOptional ? dom.DeclarationFlags.Optional : dom.DeclarationFlags.None, |
|
|
|
); |
|
|
|
); |
|
|
|
predicateIriByProp.set(prop, tripleConstraint.predicate); |
|
|
|
predicateIriByProp.set(prop, tripleConstraint.predicate); |
|
|
|
prop.jsDocComment = |
|
|
|
prop.jsDocComment = |
|
|
|