-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypeschema.ts
81 lines (73 loc) · 1.92 KB
/
typeschema.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
export type TypeRefType =
| 'resource'
| 'nested'
| 'constraint'
| 'logical'
| 'complex-type'
| 'primitive-type'
| 'valueset'
| 'choice'
| 'unknown';
export interface TypeRef {
name: string;
package: string;
kind: TypeRefType;
version: string;
url: string;
}
export interface ClassField {
required: boolean;
array: boolean;
type: TypeRef;
enum?: string[];
choiceOf?: string;
choices?: string[];
reference?: TypeRef[];
// binding?: {
// valueSet?: TypeRef;
// strength?: 'required' | 'extensible' | 'preferred' | 'example';
// };
}
export interface ChoiceField {
required?: boolean;
choices?: string[];
}
export interface ITypeSchema {
identifier: TypeRef;
base?: TypeRef;
fields?: { [key: string]: ClassField };
choices?: { [key: string]: ChoiceField };
dependencies?: TypeRef[];
nested?: NestedTypeSchema[];
}
export interface NestedTypeSchema {
fields: { [key: string]: ClassField };
identifier: TypeRef;
base: TypeRef;
}
export class TypeSchema {
private depsIdx: { [key: string]: boolean };
identifier: TypeRef;
base?: TypeRef;
dependencies?: TypeRef[];
nested?: NestedTypeSchema[];
fields?: { [key: string]: ClassField };
choices?: { [key: string]: ChoiceField };
constructor(schema: ITypeSchema) {
this.depsIdx = {};
this.base = schema.base;
this.identifier = schema.identifier;
this.dependencies = schema.dependencies;
this.nested = schema.nested;
this.fields = schema.fields;
this.choices = schema.choices;
}
ensureDep(typeref: TypeRef) {
const typekey = `${typeref.package}/${typeref.name}`;
if (!this.depsIdx[typekey]) {
this.dependencies = this.dependencies || [];
this.dependencies.push(typeref);
this.depsIdx[typekey] = true;
}
}
}