-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathgenerate-schema.ts
More file actions
493 lines (419 loc) · 13.4 KB
/
generate-schema.ts
File metadata and controls
493 lines (419 loc) · 13.4 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import path from 'node:path';
import { mkdir, writeFile } from 'node:fs/promises';
import { rimraf } from 'rimraf';
import type { MappingSpec, Schema, SpecJSON } from './types';
import { ValueSetDef } from './fetch-spec';
// TODO should this go on disk?
const valueSetCache: Record<string, any> = {};
/**
* This file will generate a simple schema representation of a FHIR spec
*/
export type PropDef = {
/** Typescript name of the type */
type?: string;
/** Human readable description */
desc?: string;
/** list of allowed values */
values?: string[];
/** Indicates that this property maps to an extension */
extension?: {
url: string;
// maybe a system too?
defaultSystem?: string;
};
/** A default value which will be used if none is provided */
default?: any;
/** This is a composite resource type (joe's words), like value[x] */
isComposite: boolean;
};
// here's a lookup of common type defs we can reuse
const typeDefs = {
'http://hl7.org/fhirpath/System.String': 'string',
// TODO some types, like `status`, have an enum. Can we use it for validation?
// For now, count all codes as strings
code: 'string',
// TODO should I capture this like a generic type, or is a name easier to map to?
// because with a name I can probably do more
//Coding: { system: 'string', coding: 'string ' },
Coding: 'Coding',
positiveInt: 'number',
};
const typeMappings = {
'http://hl7.org/fhirpath/System.String': 'string',
// TODO some types, like `status`, have an enum. Can we use it for validation?
// For now, count all codes as strings
code: 'string',
uri: 'string',
positiveInt: 'number',
};
const generate = async (
specPath: string,
mappings: MappingSpec = {},
options: { clean?: false; debugOutput?: false; isBase?: boolean } = {},
) => {
console.log('Generating schemas from ', specPath);
const outputDir = path.resolve(path.dirname(specPath), '../schema');
if (options.clean) {
console.log('Cleaning output dir: ', outputDir);
await rimraf(outputDir);
}
await mkdir(outputDir, { recursive: true });
const fullSpec = (await import(path.resolve(specPath), {
assert: { type: 'json' },
})) as SpecJSON;
const result: Record<string, Schema[]> = {};
const rawValuesets: any = await import(
path.resolve(path.dirname(specPath), 'valuesets.json'),
{
assert: { type: 'json' },
}
);
const regexes = mappings.valueSets?.map(e => new RegExp(e)) ?? [];
// remove all valueSets that don't match the mapping criteria
// TODO: this is hard because you have to handle extensions too
// const valuesets = Object.keys(rawValuesets)
// .filter(url => regexes.find(re => re.test(url)))
// .reduce((obj, url) => {
// obj[url] = rawValuesets[url];
// return obj;
// }, {});
const valuesets = rawValuesets.default;
// write all valuesets to a single file
const allValueSets = {};
const extractValues = (parent: string, url: string) => {
allValueSets[parent] ??= {};
const def = valuesets[url];
if (!def) {
return;
}
for (const value of def.values) {
// TODO is code always the correct key here?
allValueSets[parent][value.code] = value;
// force the system to be the parent's value set so that
// the mapped system is consistent
// (needed for fhir-eswatini at least)
if (!value.system) {
value.system = parent;
}
}
for (const ex of def.extends) {
extractValues(url, ex);
}
};
for (const url in valuesets) {
extractValues(url, url);
}
await writeFile(
path.resolve(outputDir, `valuesets.json`),
JSON.stringify(allValueSets, null, 2),
);
const counts = {};
const codes = {};
for (const profileId in fullSpec) {
const profile = fullSpec[profileId];
// Ignore inactive profiles
if (profile.active === false) {
console.log('ignoring inactive profile', profileId);
continue;
}
// TODO is it useful to output this or not?
if (mappings.exclude?.includes(profile.type)) {
console.log('ignoring excluded profile', profileId);
continue;
}
if (mappings.include?.length && !mappings.include.includes(profile.type)) {
// console.log('ignoring not included profile', profileId);
continue;
}
if (
profile.resourceType !== 'StructureDefinition' ||
// if not generating a base adaptor, don't process extension types explicitly - they'll be handled later
(!options.isBase && profile.type === 'Extension')
) {
continue;
}
const category = profile.extension?.find(
e =>
e.url ===
'http://hl7.org/fhir/StructureDefinition/structuredefinition-category',
);
if (category?.valueString?.startsWith('Foundation.')) {
console.log('ignoring Foundation profile', profileId);
continue;
}
const resourceType = profile.type;
counts[resourceType] = (counts[resourceType] ?? 0) + 1;
const spec = profile;
const props = {};
const schema = {
id: profileId,
type: resourceType,
url: spec.url,
props,
};
for (const el of spec.snapshot.element) {
let isComposite = false;
// if (prop.path.endsWith('[x]')) {
// isComposite = true;
// prop.path = prop.path.substring(0, prop.path.length - 3);
// } else if (/\[x\]/.test(prop.path)) {
// // TODO this isn't great
// // because we won't build a typedef for the composite value
// // Maybe later I can work it out
// continue;
// }
// Actually I think this works?
if (/\[x\]/.test(el.path)) {
isComposite = true;
el.path = el.path.replace('[x]', '');
}
if (el.path === resourceType) {
continue;
}
let path = el.path.replace(`${resourceType}\.`, '');
if (path.includes('.')) {
await parseProp(fullSpec, valuesets, schema, path, el);
continue;
}
let defaults: Record<string, any> = {};
let type = getSimpleType(el);
const isArray = el.base.max === '*';
// TODO may need to map other pattern types
// TODO how do we know if a pattern is mandatory? Is this OK to do?
if (el.patternCodeableConcept) {
defaults = isArray
? [el.patternCodeableConcept]
: el.patternCodeableConcept;
}
let extUrl;
let valueSet = el.binding?.valueSet;
if (type.includes('Extension') && el.sliceName) {
// If this is an extension property, override the path and
// type definition
path = el.sliceName;
extUrl = el.type[0].profile?.[0].split('|')[0];
if (extUrl) {
const ext = Object.values(fullSpec).find(s => s.url === extUrl);
// find the value
const value = ext?.snapshot.element.find(el =>
el.path.endsWith('.value[x]'),
);
if (value) {
type = getSimpleType(value);
// isComposite = true; // maybe better without this?
// TODO how do I get the value map out?
// If the type is a concept, should I cheat somehow?
if (value.binding) {
valueSet = value.binding.valueSet;
}
}
}
} else if (type.includes('Identifier')) {
// TODO this isn't robust because it assumes a particular slicing
// It's the schema's job to unpick this slicing
// This is a quick fix - let's see how well it stands up!
const slicedValue = spec.snapshot.element.find(
e => e.path === `${el.path}.system`,
);
if (slicedValue && slicedValue.patternUri) {
defaults.system = slicedValue.patternUri;
}
}
// TODO if this is a value set, I want to reference it globally
// const values = await extractValueSet(valuesets, el);
props[path] = {
// TODO type may only be useful if it uses a vanilla fhir type
type,
isArray,
desc: el.short || el.definition,
isComposite,
valueSet,
};
// if (values) {
// props[path].values = values;
// }
if (Object.keys(defaults).length) {
props[path].defaults = defaults;
}
if (path.endsWith('.system')) {
props[path].hasSystem = true;
}
if (extUrl) {
props[path].extension = extUrl;
}
}
// Output for debug
// TODO maybe make optional?
if (true) {
await writeFile(
path.resolve(outputDir, `${resourceType}_${profileId}.json`),
JSON.stringify(schema, null, 2),
);
}
result[resourceType] ??= [];
result[resourceType].push(schema);
}
console.log({ counts });
console.log({ codes });
return result;
};
async function extractValueSet(valuesets: any, element) {
if (element.binding?.valueSet) {
const results = new Set<string>();
const urls = [element.binding?.valueSet];
while (urls.length) {
const url = urls.shift();
const vs = (valuesets[url] as ValueSetDef) ?? { values: [], extends: [] };
for (const v of vs.values) {
results.add(v);
}
for (const e of vs.extends) {
urls.push(e);
}
}
return Array.from(results);
}
}
//
async function loadValueSet() {}
// Parse a property of a resource, like address or id
// TODO really not enjoying the duplication of parseProp
async function parseProp(
fullSpec,
valuesets,
schema: ElementSpec,
path: string,
data,
) {
let [parent, prop] = path.split('.');
const isExtensionPath = prop === 'extension';
// TODO skip if multiple dots
if (/\[x\]/.test(prop)) {
// TODO warn?
return;
}
// if the parent is a primitive type, ignore the prop
// (later we'll support extenstion types)
if (prop === 'extension') {
if (data.sliceName) {
prop = data.sliceName[0].toLowerCase() + data.sliceName.substring(1);
} else {
// extensions are bit different - we map each to a prop
return;
}
}
// TODO
if (schema.props[parent]) {
const def: PropDef = {};
// Keep primitive props
const isExtensionChild = isExtensionPath;
const hasSlice = !!data.sliceName;
const parentTypes = schema.props[parent].type || [];
const isPrimitiveParent =
!schema.props[parent].typeDef &&
parentTypes.length > 0 &&
parentTypes.every(type => type[0] === type[0]?.toLowerCase());
if (!data.type || (isPrimitiveParent && !(isExtensionChild && hasSlice))) {
return;
}
// Now work out the type of the prop
// if (data?.type?.length > 1) {
// TODO maybe restore this
// console.log('WARNING: MULTIPLE TYPES DETECTED FOR', path);
// }
let [type] = data.type;
let simpleType;
if (
type.code === 'Extension' &&
type.profile &&
type.profile.length &&
type.profile[0].match(/\/StructureDefinition/)
) {
const extensionUrl = type.profile[0].split('|')[0];
const typeId = extensionUrl.split('/').at(-1);
const spec = fullSpec[typeId];
if (spec) {
// this tells us we need to map the incoming
// prop to an extension
def.extension = {
url: spec.url,
// TODO later we may be able to pull out code mappings
// look for extension.value[x] in the spec
};
} else {
// Some extension profiles are not in the downloaded spec
// The profile URL is still enough for codegen
def.extension = {
url: extensionUrl,
};
}
} else {
simpleType = typeDefs[type.code] || type.code;
}
def.type = simpleType;
def.desc = data.short || data.definition;
// const values = await extractValueSet(valuesets, data);
// if (values) {
// def.values = values;
// }
if (data.binding) {
def.valueSet = data.binding.valueSet;
}
// TODO is there a better formalism for this?
if (prop === 'system') {
schema.props[parent].hasSystem = true;
}
// TODO: maybe lookup enum values. Not priority right now
// if (data.binding?.valueSet) {
// /// see if we can look up the values
// let [url, version] = def.valueSet.spit('|');
// // we know that we want v4, so hard code the URL
// if (url.startsWith('http://hl7.org/fhir/')) {
// url = url.replace('http://hl7.org/fhir/', 'http://hl7.org/fhir/R4');
// }
// fetch;
// def.values = [];
// }
if (Object.keys(def).length) {
schema.props[parent].typeDef ??= {};
schema.props[parent].typeDef[prop] = def;
}
}
}
/**
* Work out a simple js type from the prop definition
* This will feed type docs and auto mappings
* will return a simple string type or an object type def
*/
function getSimpleType(prop: any): string[] {
return (
prop.type?.map((t: any) => {
if (t.code in typeMappings) {
return typeMappings[t.code];
}
return t.code;
}) ?? ['any']
);
// // TODO maybe restore this
// if (prop.type.length > 1) {
// console.log('WARNING: multiple types found on ', prop.path);
// // return prop.type.map((t: any) => {
// // getSimpleType(t)[0];
// // });
// }
// if (prop.type) {
// try {
// for (const type of prop.type) {
// if (type.code in typeMappings) {
// return [typeMappings[type.code]];
// }
// return [type.code];
// }
// } catch (e) {
// console.log('ERROR extracting type for prop ', prop.path);
// console.log(prop);
// throw e;
// }
// }
}
export default generate;