-
Notifications
You must be signed in to change notification settings - Fork 0
/
jtd-ast.ts
189 lines (168 loc) · 4.43 KB
/
jtd-ast.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
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
import {
IJtd,
IJtdElementsNode,
IJtdNullableNode,
IJtdObjectNode,
IJtdRoot,
IJtdUnionNode,
IJtdValuesNode,
JtdNode,
JtdNodeType,
} from '@jtdc/types';
import {createMap, die} from './misc';
/**
* Converts JTD and its dependencies to a map of nodes where key is `ref` and value is a parsed node.
*
* @param ref The ref of the root JTD.
* @param jtdRoot The JTD to parse.
*
* @returns The map from ref to a parsed node.
*/
export function parseJtdRoot<M>(ref: string, jtdRoot: IJtdRoot<M>): Record<string, JtdNode<M>> {
const nodes = jtdRoot.definitions ? parseJtdDefinitions(jtdRoot.definitions) : createMap();
nodes[ref] = parseJtd(jtdRoot);
return nodes;
}
/**
* Converts JTD dependencies to a map of nodes where key is `ref` and value is a parsed node.
*
* @param jtdDefinitions The dictionary of ref-JTD pairs.
*
* @returns The map from ref to a parsed node.
*/
export function parseJtdDefinitions<M>(jtdDefinitions: Record<string, IJtd<M>>): Record<string, JtdNode<M>> {
const nodes = createMap<JtdNode<M>>();
for (const [ref, jtd] of Object.entries(jtdDefinitions)) {
nodes[ref] = parseJtd(jtd);
}
return nodes;
}
/**
* Converts JTD to a corresponding node.
*
* @param jtd The JTD to parse.
*
* @returns A parsed node.
*
* @see https://tools.ietf.org/html/rfc8927 RFC8927
* @see https://jsontypedef.com/docs/jtd-in-5-minutes JTD in 5 minutes
*/
export function parseJtd<M>(jtd: IJtd<M>): JtdNode<M> {
const {
nullable: jtdNullable,
type: jtdType,
ref: jtdRef,
enum: jtdEnum,
elements: jtdElements,
values: jtdValues,
properties: jtdProperties,
optionalProperties: jtdOptionalProperties,
discriminator: jtdDiscriminator,
mapping: jtdMapping,
} = jtd;
if (jtdNullable) {
const node: IJtdNullableNode<M> = {
nodeType: JtdNodeType.NULLABLE,
valueNode: parseJtd({...jtd, nullable: undefined}),
parentNode: null,
jtd,
};
node.valueNode.parentNode = node;
return node;
}
if (jtdType) {
return {
nodeType: JtdNodeType.TYPE,
type: jtdType,
parentNode: null,
jtd,
};
}
if (jtdRef) {
return {
nodeType: JtdNodeType.REF,
ref: jtdRef,
parentNode: null,
jtd,
};
}
if (jtdEnum) {
return {
nodeType: JtdNodeType.ENUM,
values: jtdEnum,
parentNode: null,
jtd,
};
}
if (jtdElements) {
const node: IJtdElementsNode<M> = {
nodeType: JtdNodeType.ELEMENTS,
elementNode: parseJtd(jtdElements),
parentNode: null,
jtd,
};
node.elementNode.parentNode = node;
return node;
}
if (jtdValues) {
const node: IJtdValuesNode<M> = {
nodeType: JtdNodeType.VALUES,
valueNode: parseJtd(jtdValues),
parentNode: null,
jtd,
};
node.valueNode.parentNode = node;
return node;
}
if (jtdProperties || jtdOptionalProperties) {
const objectNode: IJtdObjectNode<M> = {
nodeType: JtdNodeType.OBJECT,
properties: createMap(),
optionalProperties: createMap(),
parentNode: null,
jtd,
};
if (jtdProperties) {
for (const [propKey, propJtd] of Object.entries(jtdProperties)) {
const propNode = objectNode.properties[propKey] = parseJtd(propJtd);
propNode.parentNode = objectNode;
}
}
if (jtdOptionalProperties) {
for (const [propKey, propJtd] of Object.entries(jtdOptionalProperties)) {
if (propKey in objectNode.properties) {
die('Duplicated property: ' + propKey);
}
const propNode = objectNode.optionalProperties[propKey] = parseJtd(propJtd);
propNode.parentNode = objectNode;
}
}
return objectNode;
}
if (jtdDiscriminator || jtdMapping) {
if (!jtdDiscriminator || !jtdMapping) {
die('Malformed discriminated union');
}
const unionNode: IJtdUnionNode<M> = {
nodeType: JtdNodeType.UNION,
discriminator: jtdDiscriminator,
mapping: createMap(),
parentNode: null,
jtd,
};
for (const [mappingKey, mappingJtd] of Object.entries(jtdMapping)) {
const objectNode = parseJtd(mappingJtd);
if (objectNode.nodeType !== JtdNodeType.OBJECT) {
die('Mappings must be object definitions: ' + mappingKey);
}
unionNode.mapping[mappingKey] = objectNode;
objectNode.parentNode = unionNode;
}
return unionNode;
}
return {
nodeType: JtdNodeType.ANY,
parentNode: null,
jtd,
};
}