This repository was archived by the owner on Jan 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathschemaTracker.ts
More file actions
175 lines (153 loc) · 6.05 KB
/
Copy pathschemaTracker.ts
File metadata and controls
175 lines (153 loc) · 6.05 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env node
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import * as ajv from 'ajv';
import * as fs from 'fs-extra';
import * as path from 'path';
const http = require('http')
const https = require('https')
export class SchemaTracker {
// Map from type name to information about that type.
typeToType: Map<string, Type>
private readonly validator: ajv.Ajv
constructor() {
this.typeToType = new Map<string, Type>()
this.validator = new ajv()
}
async getValidator(schemaPath: string): Promise<[ajv.ValidateFunction, boolean]> {
let validator = this.validator.getSchema(schemaPath)
let added = false
if (!validator) {
let schemaObject = await fs.readJSON(schemaPath)
added = true
if (schemaObject.oneOf) {
const defRef = '#/definitions/'
const implementsRole = 'implements('
let processRole = (role: string, type: Type) => {
if (role.startsWith(implementsRole)) {
role = role.substring(implementsRole.length, role.length - 1)
let interfaceDefinition = this.typeToType.get(role)
if (!interfaceDefinition) {
interfaceDefinition = new Type(role)
this.typeToType.set(role, interfaceDefinition)
}
interfaceDefinition.addImplementation(type)
}
}
for (let one of schemaObject.oneOf) {
let ref = one.$ref
// NOTE: Assuming schema file format is from httpSchema or we will ignore.
// Assumption is that a given type name is the same across different schemas.
// All .dialog in one app should use the same app.schema, but if you are using
// a .dialog from another app then it will use its own schema which if it follows the rules
// should have globally unique type names.
if (ref.startsWith(defRef)) {
ref = ref.substring(defRef.length)
if (!this.typeToType.get(ref)) {
let def = schemaObject.definitions[ref]
if (def) {
let type = new Type(ref, def)
this.typeToType.set(ref, type)
if (def.$role) {
if (typeof def.$role === 'string') {
processRole(def.$role, type)
} else {
for (let role of def.$role) {
processRole(role, type)
}
}
}
}
}
}
}
}
let metaSchemaName = schemaObject.$schema
let metaSchemaCache = path.join(__dirname, path.basename(metaSchemaName))
let metaSchema: any
if (!await fs.pathExists(metaSchemaCache)) {
try {
let metaSchemaDefinition = await this.getURL(metaSchemaName)
metaSchema = JSON.parse(metaSchemaDefinition)
} catch {
throw new Error(`Could not parse ${metaSchemaName}`)
}
await fs.writeJSON(metaSchemaCache, metaSchema, { spaces: 4 })
} else {
metaSchema = await fs.readJSON(metaSchemaCache)
}
if (!this.validator.getSchema(metaSchemaName)) {
this.validator.addSchema(metaSchema, metaSchemaName)
}
this.validator.addSchema(schemaObject, schemaPath)
validator = this.validator.getSchema(schemaPath)
}
if (!validator) {
throw new Error('Could not find schema validator.')
}
return [validator, added]
}
private async getURL(url: string): Promise<any> {
return new Promise((resolve, reject) => {
let client = http
if (url.toString().indexOf('https') === 0) {
client = https
}
client.get(url, (resp: any) => {
let data = ''
// A chunk of data has been received.
resp.on('data', (chunk: any) => {
data += chunk
})
// The whole response has been received.
resp.on('end', () => {
resolve(data)
})
}).on('error', (err: any) => {
reject(err)
})
})
}
}
// Information about a type.
export class Type {
// Name of the type.
name: string
// Paths to lg properties for concrete types.
lgProperties: string[]
// Possible types for an interface type.
implementations: Type[]
// Interface types this type implements.
interfaces: Type[]
constructor(name: string, schema?: any) {
this.name = name
this.lgProperties = []
this.implementations = []
this.interfaces = []
if (schema) {
this.walkProps(schema, name)
}
}
addImplementation(type: Type) {
this.implementations.push(type)
type.interfaces.push(this)
}
toString(): string {
return this.name
}
private walkProps(val: any, path: string) {
if (val.properties) {
for (let propName in val.properties) {
let prop = val.properties[propName]
let newPath = `${path}/${propName}`
if (prop.$role === 'lg') {
this.lgProperties.push(newPath)
} else if (prop.properties) {
this.walkProps(prop, newPath)
}
}
}
}
}