-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
97 lines (78 loc) · 3.78 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { ParserOutput, ShaclProperty } from 'https://deno.land/x/[email protected]/types.ts'
import { Parser } from 'https://deno.land/x/[email protected]/mod.ts'
import { ContextParser, JsonLdContextNormalized } from 'npm:jsonld-context-parser'
export class Converter {
/**
* Converts a SHACL shape to TypeScript types.
*/
async transform (shaclString: string, vocabAlias?: string, context: { [key: string]: string } = {}) {
const parser = new Parser()
const metas = await parser.parse(shaclString)
const prefixes = { ...parser.shaclParser._prefixes, ...context }
if (vocabAlias) prefixes['@vocab'] = parser.shaclParser._prefixes![vocabAlias]
const contextParser = new ContextParser({
skipValidation: true,
expandContentTypeToBase: true,
})
return this.stringify(metas, await contextParser.parse(prefixes))
}
/**
* Converts the metas to TypeScript types.
*/
stringify (shapeMetas: ParserOutput, context: JsonLdContextNormalized) {
const types: Array<{ text: string, iri: string, name: string }> = []
for (const [iri, shapeMeta] of Object.entries(shapeMetas)) {
let typeString = ''
for (const shapeProperty of shapeMeta.properties) {
typeString += '\n' + this.processPropery(shapeProperty, context)
}
const name = this.createTypeName(iri, context)
types.push({
text: `export type ${name} = {${typeString}\n}\n`,
iri,
name
})
}
return types
}
/**
* The type names themselves should not have a colon.
*/
createTypeName (iri: string, context: JsonLdContextNormalized) {
const compactedIri = context.compactIri(iri, true)
return compactedIri.replaceAll(':', '')
}
rdfToJsType = (iri: string) => {
if (iri === 'http://www.w3.org/2001/XMLSchema#date') return 'Date'
if (iri === 'http://www.w3.org/2001/XMLSchema#integer') return 'number'
if (iri === 'http://www.w3.org/2001/XMLSchema#string') return 'string'
if (iri === 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString') return 'string'
}
/**
* Processes one shacl property.
*/
processPropery (shaclPropery: ShaclProperty, context: JsonLdContextNormalized, indent = ' ', typeOnly = false) {
const name = shaclPropery.predicate ? context.compactIri(shaclPropery.predicate as string, true) : ''
const required = !!shaclPropery.required as boolean
const multiple = !!shaclPropery.multiple as boolean
const nodeType = shaclPropery.nodeType as string ?? null
const nodeTypeCompacted = nodeType ? this.createTypeName(nodeType, context) : null
const dataType = shaclPropery.dataType ? this.rdfToJsType(shaclPropery.dataType as string) : null
const strictType = nodeTypeCompacted ? nodeTypeCompacted : (dataType ?? 'string')
let type = `${multiple ? 'Array<' : ''}${strictType}${multiple ? '>' : ''}`
/**
* At the moment this OR (sh:or) logic only supports alternative types.
* It is not possible to make more complex alternative nested types.
*/
if (shaclPropery.or) {
const nestedProperties = shaclPropery.or as unknown as Array<any>
// We do not allow nested sh:or
const nestedTypes: Array<string> = nestedProperties
.map(nestedProperty => this.processPropery(nestedProperty, context, indent, true)) as Array<string>
const nestedTypesSet = new Set(nestedTypes)
type = [...nestedTypesSet.values()].join(' | ')
}
if (typeOnly) return type
return `${indent}${name}${required ? '' : '?'}: ${type};`
}
}