@strictly/define 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +31 -0
- package/.out/index.d.ts +20 -0
- package/.out/index.js +20 -0
- package/.out/transformers/copies/copy.d.ts +4 -0
- package/.out/transformers/copies/copy.js +7 -0
- package/.out/transformers/copies/copy_to.d.ts +6 -0
- package/.out/transformers/copies/copy_to.js +91 -0
- package/.out/transformers/copies/mobx_copy.d.ts +5 -0
- package/.out/transformers/copies/mobx_copy.js +42 -0
- package/.out/transformers/copies/specs/copy_to.tests.d.ts +1 -0
- package/.out/transformers/copies/specs/copy_to.tests.js +97 -0
- package/.out/transformers/copies/specs/mobx_copy.tests.d.ts +1 -0
- package/.out/transformers/copies/specs/mobx_copy.tests.js +19 -0
- package/.out/transformers/flatteners/flatten_accessors_of.d.ts +5 -0
- package/.out/transformers/flatteners/flatten_accessors_of.js +11 -0
- package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.d.ts +3 -0
- package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.js +10 -0
- package/.out/transformers/flatteners/flatten_type_def_to.d.ts +4 -0
- package/.out/transformers/flatteners/flatten_type_def_to.js +49 -0
- package/.out/transformers/flatteners/flatten_type_defs_of.d.ts +3 -0
- package/.out/transformers/flatteners/flatten_type_defs_of.js +7 -0
- package/.out/transformers/flatteners/flatten_value_type_to.d.ts +11 -0
- package/.out/transformers/flatteners/flatten_value_type_to.js +79 -0
- package/.out/transformers/flatteners/flatten_value_types_of.d.ts +3 -0
- package/.out/transformers/flatteners/flatten_value_types_of.js +7 -0
- package/.out/transformers/flatteners/json_path.d.ts +2 -0
- package/.out/transformers/flatteners/json_path.js +14 -0
- package/.out/transformers/flatteners/specs/flatten_accessors_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_accessors_of.tests.js +90 -0
- package/.out/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.js +94 -0
- package/.out/transformers/flatteners/specs/flatten_type_def_to.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_type_def_to.tests.js +110 -0
- package/.out/transformers/flatteners/specs/flatten_type_defs_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_type_defs_of.tests.js +17 -0
- package/.out/transformers/flatteners/specs/flatten_value_type_to.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_value_type_to.tests.js +297 -0
- package/.out/transformers/flatteners/specs/flatten_value_types_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_value_types_of.tests.js +37 -0
- package/.out/transformers/flatteners/specs/value_path_to_type_path.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/value_path_to_type_path.tests.js +167 -0
- package/.out/transformers/flatteners/value_path_to_type_path.d.ts +2 -0
- package/.out/transformers/flatteners/value_path_to_type_path.js +96 -0
- package/.out/tsconfig.json +15 -0
- package/.out/tsconfig.tsbuildinfo +1 -0
- package/.out/tsup.config.d.ts +3 -0
- package/.out/tsup.config.js +12 -0
- package/.out/types/builders.d.ts +62 -0
- package/.out/types/builders.js +148 -0
- package/.out/types/definitions.d.ts +41 -0
- package/.out/types/definitions.js +26 -0
- package/.out/types/flattened.d.ts +2 -0
- package/.out/types/flattened.js +1 -0
- package/.out/types/flattened_accessors_of.d.ts +9 -0
- package/.out/types/flattened_accessors_of.js +1 -0
- package/.out/types/flattened_type_defs_of.d.ts +21 -0
- package/.out/types/flattened_type_defs_of.js +1 -0
- package/.out/types/flattened_value_types_of.d.ts +6 -0
- package/.out/types/flattened_value_types_of.js +1 -0
- package/.out/types/json_path_of.d.ts +1 -0
- package/.out/types/json_path_of.js +1 -0
- package/.out/types/json_paths_of.d.ts +16 -0
- package/.out/types/json_paths_of.js +1 -0
- package/.out/types/mobx_value_type_of.d.ts +11 -0
- package/.out/types/mobx_value_type_of.js +2 -0
- package/.out/types/partial_type_def_of.d.ts +40 -0
- package/.out/types/partial_type_def_of.js +1 -0
- package/.out/types/readonly_type_def_of.d.ts +29 -0
- package/.out/types/readonly_type_def_of.js +1 -0
- package/.out/types/specs/builder.tests.d.ts +1 -0
- package/.out/types/specs/builder.tests.js +93 -0
- package/.out/types/specs/flattened_accessors_of.tests.d.ts +1 -0
- package/.out/types/specs/flattened_accessors_of.tests.js +11 -0
- package/.out/types/specs/flattened_type_defs_of.tests.d.ts +1 -0
- package/.out/types/specs/flattened_type_defs_of.tests.js +71 -0
- package/.out/types/specs/flattened_value_types_of.tests.d.ts +1 -0
- package/.out/types/specs/flattened_value_types_of.tests.js +11 -0
- package/.out/types/specs/json_paths_of.tests.d.ts +1 -0
- package/.out/types/specs/json_paths_of.tests.js +199 -0
- package/.out/types/specs/partial_type_def_of.tests.d.ts +1 -0
- package/.out/types/specs/partial_type_def_of.tests.js +50 -0
- package/.out/types/specs/readonly_type_def_of.tests.d.ts +1 -0
- package/.out/types/specs/readonly_type_def_of.tests.js +55 -0
- package/.out/types/specs/strict_definitions.tests.d.ts +1 -0
- package/.out/types/specs/strict_definitions.tests.js +42 -0
- package/.out/types/specs/value_to_type_paths_of.tests.d.ts +1 -0
- package/.out/types/specs/value_to_type_paths_of.tests.js +90 -0
- package/.out/types/specs/value_type_of.tests.d.ts +1 -0
- package/.out/types/specs/value_type_of.tests.js +91 -0
- package/.out/types/specs/value_types_of_discriminated_union.tests.d.ts +1 -0
- package/.out/types/specs/value_types_of_discriminated_union.tests.js +9 -0
- package/.out/types/strict_definitions.d.ts +44 -0
- package/.out/types/strict_definitions.js +1 -0
- package/.out/types/value_to_type_paths_of.d.ts +22 -0
- package/.out/types/value_to_type_paths_of.js +1 -0
- package/.out/types/value_type_of.d.ts +24 -0
- package/.out/types/value_type_of.js +1 -0
- package/.out/types/value_types_of_discriminated_union.d.ts +9 -0
- package/.out/types/value_types_of_discriminated_union.js +1 -0
- package/.out/vitest.workspace.d.ts +2 -0
- package/.out/vitest.workspace.js +7 -0
- package/.turbo/turbo-build.log +18 -0
- package/.turbo/turbo-check-types.log +3 -0
- package/.turbo/turbo-release$colon$exports.log +3 -0
- package/README.md +12 -0
- package/dist/index.cjs +798 -0
- package/dist/index.d.cts +301 -0
- package/dist/index.d.ts +301 -0
- package/dist/index.js +775 -0
- package/index.ts +20 -0
- package/package.exports.json +18 -0
- package/package.json +45 -0
- package/transformers/copies/copy.ts +18 -0
- package/transformers/copies/copy_to.ts +226 -0
- package/transformers/copies/mobx_copy.ts +81 -0
- package/transformers/copies/specs/copy_to.tests.ts +143 -0
- package/transformers/copies/specs/mobx_copy.tests.ts +26 -0
- package/transformers/flatteners/flatten_accessors_of.ts +43 -0
- package/transformers/flatteners/flatten_json_value_to_type_paths_of.ts +39 -0
- package/transformers/flatteners/flatten_type_def_to.ts +127 -0
- package/transformers/flatteners/flatten_type_defs_of.ts +16 -0
- package/transformers/flatteners/flatten_value_type_to.ts +227 -0
- package/transformers/flatteners/flatten_value_types_of.ts +23 -0
- package/transformers/flatteners/json_path.ts +15 -0
- package/transformers/flatteners/specs/flatten_accessors_of.tests.ts +113 -0
- package/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.ts +115 -0
- package/transformers/flatteners/specs/flatten_type_def_to.tests.ts +146 -0
- package/transformers/flatteners/specs/flatten_type_defs_of.tests.ts +26 -0
- package/transformers/flatteners/specs/flatten_value_type_to.tests.ts +452 -0
- package/transformers/flatteners/specs/flatten_value_types_of.tests.ts +46 -0
- package/transformers/flatteners/specs/value_path_to_type_path.tests.ts +240 -0
- package/transformers/flatteners/value_path_to_type_path.ts +164 -0
- package/tsconfig.build.json +11 -0
- package/tsconfig.json +15 -0
- package/tsup.config.ts +16 -0
- package/types/builders.ts +284 -0
- package/types/definitions.ts +106 -0
- package/types/flattened.ts +5 -0
- package/types/flattened_accessors_of.ts +15 -0
- package/types/flattened_type_defs_of.ts +130 -0
- package/types/flattened_value_types_of.ts +11 -0
- package/types/json_path_of.ts +7 -0
- package/types/json_paths_of.ts +129 -0
- package/types/mobx_value_type_of.ts +16 -0
- package/types/partial_type_def_of.ts +64 -0
- package/types/readonly_type_def_of.ts +53 -0
- package/types/specs/builder.tests.ts +295 -0
- package/types/specs/flattened_accessors_of.tests.ts +27 -0
- package/types/specs/flattened_type_defs_of.tests.ts +212 -0
- package/types/specs/flattened_value_types_of.tests.ts +21 -0
- package/types/specs/json_paths_of.tests.ts +304 -0
- package/types/specs/partial_type_def_of.tests.ts +251 -0
- package/types/specs/readonly_type_def_of.tests.ts +158 -0
- package/types/specs/strict_definitions.tests.ts +184 -0
- package/types/specs/value_to_type_paths_of.tests.ts +181 -0
- package/types/specs/value_type_of.tests.ts +329 -0
- package/types/specs/value_types_of_discriminated_union.tests.ts +30 -0
- package/types/strict_definitions.ts +107 -0
- package/types/value_to_type_paths_of.ts +184 -0
- package/types/value_type_of.ts +84 -0
- package/types/value_types_of_discriminated_union.ts +14 -0
- package/vitest.workspace.ts +11 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
// transformers/copies/copy_to.ts
|
|
2
|
+
import {
|
|
3
|
+
map,
|
|
4
|
+
reduce,
|
|
5
|
+
UnexpectedImplementationError,
|
|
6
|
+
UnreachableError
|
|
7
|
+
} from "@strictly/base";
|
|
8
|
+
|
|
9
|
+
// types/definitions.ts
|
|
10
|
+
var TypeDefType = /* @__PURE__ */ ((TypeDefType2) => {
|
|
11
|
+
TypeDefType2[TypeDefType2["Literal"] = 1] = "Literal";
|
|
12
|
+
TypeDefType2[TypeDefType2["List"] = 2] = "List";
|
|
13
|
+
TypeDefType2[TypeDefType2["Record"] = 3] = "Record";
|
|
14
|
+
TypeDefType2[TypeDefType2["Object"] = 4] = "Object";
|
|
15
|
+
TypeDefType2[TypeDefType2["Union"] = 5] = "Union";
|
|
16
|
+
return TypeDefType2;
|
|
17
|
+
})(TypeDefType || {});
|
|
18
|
+
|
|
19
|
+
// transformers/copies/copy_to.ts
|
|
20
|
+
function copyTo({ definition }, value, copier) {
|
|
21
|
+
return internalCopyTo(
|
|
22
|
+
definition,
|
|
23
|
+
value,
|
|
24
|
+
copier
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
function internalCopyTo(definition, value, copier) {
|
|
28
|
+
switch (definition.type) {
|
|
29
|
+
case 1 /* Literal */:
|
|
30
|
+
return copyLiteral(
|
|
31
|
+
definition,
|
|
32
|
+
value,
|
|
33
|
+
copier
|
|
34
|
+
);
|
|
35
|
+
case 2 /* List */:
|
|
36
|
+
return copyList(
|
|
37
|
+
definition,
|
|
38
|
+
value,
|
|
39
|
+
copier
|
|
40
|
+
);
|
|
41
|
+
case 3 /* Record */:
|
|
42
|
+
return copyRecord(
|
|
43
|
+
definition,
|
|
44
|
+
value,
|
|
45
|
+
copier
|
|
46
|
+
);
|
|
47
|
+
case 4 /* Object */:
|
|
48
|
+
return copyObject(
|
|
49
|
+
definition,
|
|
50
|
+
value,
|
|
51
|
+
copier
|
|
52
|
+
);
|
|
53
|
+
case 5 /* Union */:
|
|
54
|
+
return copyUnion(
|
|
55
|
+
definition,
|
|
56
|
+
value,
|
|
57
|
+
copier
|
|
58
|
+
);
|
|
59
|
+
default:
|
|
60
|
+
throw new UnreachableError(definition);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function copyLiteral(typeDef, value, copier) {
|
|
64
|
+
return copier(value, typeDef);
|
|
65
|
+
}
|
|
66
|
+
function copyList(typeDef, arr, copier) {
|
|
67
|
+
const {
|
|
68
|
+
elements
|
|
69
|
+
} = typeDef;
|
|
70
|
+
const list2 = arr.map(function(value) {
|
|
71
|
+
return internalCopyTo(elements, value, copier);
|
|
72
|
+
});
|
|
73
|
+
return copier(
|
|
74
|
+
list2,
|
|
75
|
+
typeDef
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
function copyRecord(typeDef, value, copier) {
|
|
79
|
+
const {
|
|
80
|
+
valueTypeDef
|
|
81
|
+
} = typeDef;
|
|
82
|
+
const record2 = map(
|
|
83
|
+
value,
|
|
84
|
+
function(_key, value2) {
|
|
85
|
+
return internalCopyTo(valueTypeDef, value2, copier);
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
return copier(
|
|
89
|
+
record2,
|
|
90
|
+
typeDef
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
function copyObjectFields(fields, value, copier, extra) {
|
|
94
|
+
const record2 = reduce(fields, function(acc, key, field) {
|
|
95
|
+
const fieldValue = value[key];
|
|
96
|
+
acc[key] = fieldValue != null ? internalCopyTo(field, fieldValue, copier) : fieldValue;
|
|
97
|
+
return acc;
|
|
98
|
+
}, extra);
|
|
99
|
+
return record2;
|
|
100
|
+
}
|
|
101
|
+
function copyObject(typeDef, value, copier) {
|
|
102
|
+
const {
|
|
103
|
+
fields
|
|
104
|
+
} = typeDef;
|
|
105
|
+
const record2 = copyObjectFields(fields, value, copier, {});
|
|
106
|
+
return copier(record2, typeDef);
|
|
107
|
+
}
|
|
108
|
+
function copyUnion(typeDef, value, copier) {
|
|
109
|
+
const {
|
|
110
|
+
discriminator,
|
|
111
|
+
unions
|
|
112
|
+
} = typeDef;
|
|
113
|
+
if (discriminator != null) {
|
|
114
|
+
const discriminatorValue = value[discriminator];
|
|
115
|
+
const discriminatingUnion = {
|
|
116
|
+
...internalCopyTo(
|
|
117
|
+
unions[discriminatorValue],
|
|
118
|
+
value,
|
|
119
|
+
copier
|
|
120
|
+
),
|
|
121
|
+
[discriminator]: discriminatorValue
|
|
122
|
+
};
|
|
123
|
+
return copier(discriminatingUnion, typeDef);
|
|
124
|
+
}
|
|
125
|
+
const allTypeDefs = Object.values(unions);
|
|
126
|
+
const variableTypeDefs = allTypeDefs.filter(function(typeDef2) {
|
|
127
|
+
return typeDef2.type !== 1 /* Literal */ || typeDef2.valuePrototype == null;
|
|
128
|
+
});
|
|
129
|
+
if (variableTypeDefs.length <= 1) {
|
|
130
|
+
const targetTypeDef = allTypeDefs.find(function(typeDef2) {
|
|
131
|
+
return typeDef2.type === 1 /* Literal */ && typeDef2.valuePrototype != null && typeDef2.valuePrototype.indexOf(value) >= 0;
|
|
132
|
+
}) || variableTypeDefs[0];
|
|
133
|
+
return internalCopyTo(targetTypeDef, value, copier);
|
|
134
|
+
}
|
|
135
|
+
throw new UnexpectedImplementationError(
|
|
136
|
+
"unions must be strict in order to be copied"
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// transformers/copies/copy.ts
|
|
141
|
+
function identity(v) {
|
|
142
|
+
return v;
|
|
143
|
+
}
|
|
144
|
+
function copy(t, proto) {
|
|
145
|
+
return copyTo(t, proto, identity);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// transformers/copies/mobx_copy.ts
|
|
149
|
+
import {
|
|
150
|
+
reduce as reduce3,
|
|
151
|
+
UnreachableError as UnreachableError3
|
|
152
|
+
} from "@strictly/base";
|
|
153
|
+
import {
|
|
154
|
+
makeObservable,
|
|
155
|
+
observable
|
|
156
|
+
} from "mobx";
|
|
157
|
+
|
|
158
|
+
// transformers/flatteners/flatten_value_type_to.ts
|
|
159
|
+
import {
|
|
160
|
+
reduce as reduce2,
|
|
161
|
+
UnreachableError as UnreachableError2
|
|
162
|
+
} from "@strictly/base";
|
|
163
|
+
|
|
164
|
+
// transformers/flatteners/json_path.ts
|
|
165
|
+
function jsonPath(prefix, segment, qualifier = "") {
|
|
166
|
+
const s = `.${qualifier}${segment}`;
|
|
167
|
+
return `${prefix}${s}`;
|
|
168
|
+
}
|
|
169
|
+
function jsonPathPop(path) {
|
|
170
|
+
const parts = path.split(".");
|
|
171
|
+
if (parts.length <= 1) {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
return [
|
|
175
|
+
parts.slice(0, -1).join("."),
|
|
176
|
+
parts.pop()
|
|
177
|
+
];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// transformers/flatteners/flatten_value_type_to.ts
|
|
181
|
+
function flattenValueTypeTo({ definition }, v, setter, mapper) {
|
|
182
|
+
const r = {};
|
|
183
|
+
internalFlattenValue(
|
|
184
|
+
"$",
|
|
185
|
+
"$",
|
|
186
|
+
definition,
|
|
187
|
+
v,
|
|
188
|
+
setter,
|
|
189
|
+
mapper,
|
|
190
|
+
r
|
|
191
|
+
);
|
|
192
|
+
return r;
|
|
193
|
+
}
|
|
194
|
+
function internalFlattenValue(valuePath, typePath, typeDef, v, setter, mapper, r) {
|
|
195
|
+
r[valuePath] = mapper(typeDef, v, setter, typePath, valuePath);
|
|
196
|
+
return internalFlattenValueChildren(valuePath, typePath, "", typeDef, v, mapper, r);
|
|
197
|
+
}
|
|
198
|
+
function internalFlattenValueChildren(valuePath, typePath, qualifier, typeDef, v, mapper, r) {
|
|
199
|
+
switch (typeDef.type) {
|
|
200
|
+
case 1 /* Literal */:
|
|
201
|
+
return r;
|
|
202
|
+
case 2 /* List */:
|
|
203
|
+
return internalFlattenListChildren(valuePath, typePath, typeDef, v, mapper, r);
|
|
204
|
+
case 3 /* Record */:
|
|
205
|
+
return internalFlattenRecordChildren(valuePath, typePath, typeDef, v, mapper, r);
|
|
206
|
+
case 4 /* Object */:
|
|
207
|
+
return internalFlattenObjectChildren(valuePath, typePath, qualifier, typeDef, v, mapper, r);
|
|
208
|
+
case 5 /* Union */:
|
|
209
|
+
return internalFlattenUnionChildren(valuePath, typePath, qualifier, typeDef, v, mapper, r);
|
|
210
|
+
default:
|
|
211
|
+
throw new UnreachableError2(typeDef);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function internalFlattenListChildren(valuePath, typePath, { elements }, v, mapper, r) {
|
|
215
|
+
const newTypePath = jsonPath(typePath, "*");
|
|
216
|
+
return v.reduce(function(r2, e, i) {
|
|
217
|
+
return internalFlattenValue(
|
|
218
|
+
jsonPath(valuePath, i),
|
|
219
|
+
newTypePath,
|
|
220
|
+
elements,
|
|
221
|
+
e,
|
|
222
|
+
(e2) => {
|
|
223
|
+
v[i] = e2;
|
|
224
|
+
},
|
|
225
|
+
mapper,
|
|
226
|
+
r2
|
|
227
|
+
);
|
|
228
|
+
}, r);
|
|
229
|
+
}
|
|
230
|
+
function internalFlattenRecordChildren(valuePath, typePath, { valueTypeDef }, v, mapper, r) {
|
|
231
|
+
const newTypePath = jsonPath(typePath, "*");
|
|
232
|
+
return reduce2(
|
|
233
|
+
v,
|
|
234
|
+
function(r2, k, value) {
|
|
235
|
+
return internalFlattenValue(
|
|
236
|
+
jsonPath(valuePath, k),
|
|
237
|
+
newTypePath,
|
|
238
|
+
valueTypeDef,
|
|
239
|
+
value,
|
|
240
|
+
(value2) => {
|
|
241
|
+
v[k] = value2;
|
|
242
|
+
},
|
|
243
|
+
mapper,
|
|
244
|
+
r2
|
|
245
|
+
);
|
|
246
|
+
},
|
|
247
|
+
r
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
function internalFlattenObjectChildren(valuePath, typePath, qualifier, { fields }, v, mapper, r) {
|
|
251
|
+
return reduce2(
|
|
252
|
+
fields,
|
|
253
|
+
function(r2, k, fieldTypeDef) {
|
|
254
|
+
const fieldValue = v[k];
|
|
255
|
+
return internalFlattenValue(
|
|
256
|
+
jsonPath(valuePath, k, qualifier),
|
|
257
|
+
jsonPath(typePath, k, qualifier),
|
|
258
|
+
fieldTypeDef,
|
|
259
|
+
fieldValue,
|
|
260
|
+
(value) => {
|
|
261
|
+
v[k] = value;
|
|
262
|
+
},
|
|
263
|
+
mapper,
|
|
264
|
+
r2
|
|
265
|
+
);
|
|
266
|
+
},
|
|
267
|
+
r
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
function internalFlattenUnionChildren(valuePath, typePath, qualifier, typeDef, v, mapper, r) {
|
|
271
|
+
const childTypeDef = getUnionTypeDef(typeDef, v);
|
|
272
|
+
const newQualifier = typeDef.discriminator != null ? `${qualifier}${v[typeDef.discriminator]}:` : qualifier;
|
|
273
|
+
return internalFlattenValueChildren(
|
|
274
|
+
valuePath,
|
|
275
|
+
typePath,
|
|
276
|
+
newQualifier,
|
|
277
|
+
childTypeDef,
|
|
278
|
+
v,
|
|
279
|
+
mapper,
|
|
280
|
+
r
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
function getUnionTypeDef(typeDef, v) {
|
|
284
|
+
if (typeDef.discriminator == null) {
|
|
285
|
+
return reduce2(
|
|
286
|
+
typeDef.unions,
|
|
287
|
+
function(acc, _k, t) {
|
|
288
|
+
if (t.type === 1 /* Literal */ && t.valuePrototype) {
|
|
289
|
+
if (t.valuePrototype.indexOf(v) >= 0) {
|
|
290
|
+
return t;
|
|
291
|
+
}
|
|
292
|
+
} else {
|
|
293
|
+
if (acc == null) {
|
|
294
|
+
return t;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return acc;
|
|
298
|
+
},
|
|
299
|
+
null
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
return typeDef.unions[v[typeDef.discriminator]];
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// transformers/copies/mobx_copy.ts
|
|
306
|
+
function observeValue(v, def) {
|
|
307
|
+
if (v == null) {
|
|
308
|
+
return v;
|
|
309
|
+
}
|
|
310
|
+
switch (def.type) {
|
|
311
|
+
case 1 /* Literal */:
|
|
312
|
+
return v;
|
|
313
|
+
case 2 /* List */:
|
|
314
|
+
return observable.array(v, { deep: false });
|
|
315
|
+
case 3 /* Record */:
|
|
316
|
+
return observable(
|
|
317
|
+
v,
|
|
318
|
+
{},
|
|
319
|
+
{
|
|
320
|
+
deep: false
|
|
321
|
+
}
|
|
322
|
+
);
|
|
323
|
+
case 4 /* Object */:
|
|
324
|
+
return makeObservable(
|
|
325
|
+
v,
|
|
326
|
+
reduce3(
|
|
327
|
+
def.fields,
|
|
328
|
+
function(acc, k) {
|
|
329
|
+
acc[k] = observable;
|
|
330
|
+
return acc;
|
|
331
|
+
},
|
|
332
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
333
|
+
{}
|
|
334
|
+
),
|
|
335
|
+
{
|
|
336
|
+
deep: false
|
|
337
|
+
}
|
|
338
|
+
);
|
|
339
|
+
case 5 /* Union */:
|
|
340
|
+
return observeValue(v, getUnionTypeDef(def, v));
|
|
341
|
+
default:
|
|
342
|
+
throw new UnreachableError3(def);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
function mobxCopy(t, proto) {
|
|
346
|
+
return copyTo(t, proto, observeValue);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// transformers/flatteners/flatten_accessors_of.ts
|
|
350
|
+
function mapAccessor(_t, value, set) {
|
|
351
|
+
return {
|
|
352
|
+
value,
|
|
353
|
+
set
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
function flattenAccessorsOf(t, value, setValue) {
|
|
357
|
+
return flattenValueTypeTo(
|
|
358
|
+
t,
|
|
359
|
+
value,
|
|
360
|
+
setValue,
|
|
361
|
+
mapAccessor
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// transformers/flatteners/flatten_json_value_to_type_paths_of.ts
|
|
366
|
+
function mapTypePaths(_t, _value, _set, typePath) {
|
|
367
|
+
return typePath;
|
|
368
|
+
}
|
|
369
|
+
function flattenJsonValueToTypePathsOf(t, value) {
|
|
370
|
+
return flattenValueTypeTo(
|
|
371
|
+
t,
|
|
372
|
+
value,
|
|
373
|
+
function() {
|
|
374
|
+
},
|
|
375
|
+
mapTypePaths
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// transformers/flatteners/flatten_type_def_to.ts
|
|
380
|
+
import {
|
|
381
|
+
reduce as reduce4,
|
|
382
|
+
UnreachableError as UnreachableError4
|
|
383
|
+
} from "@strictly/base";
|
|
384
|
+
function flattenTypeDefTo({ definition }, mapper) {
|
|
385
|
+
const typeDefs = internalFlattenTypeDef("$", definition, {});
|
|
386
|
+
return reduce4(
|
|
387
|
+
typeDefs,
|
|
388
|
+
function(acc, key, typeDef) {
|
|
389
|
+
acc[key] = mapper(typeDef);
|
|
390
|
+
return acc;
|
|
391
|
+
},
|
|
392
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
393
|
+
{}
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
function internalFlattenTypeDef(path, t, r) {
|
|
397
|
+
r[path] = t;
|
|
398
|
+
return internalFlattenTypeDefChildren(path, "", t, r);
|
|
399
|
+
}
|
|
400
|
+
function internalFlattenTypeDefChildren(path, qualifier, t, r) {
|
|
401
|
+
switch (t.type) {
|
|
402
|
+
case 1 /* Literal */:
|
|
403
|
+
return r;
|
|
404
|
+
case 2 /* List */:
|
|
405
|
+
return internalFlattenedListTypeDefChildren(path, t, r);
|
|
406
|
+
case 3 /* Record */:
|
|
407
|
+
return internalFlattenRecordTypeDefChildren(path, t, r);
|
|
408
|
+
case 4 /* Object */:
|
|
409
|
+
return internalFlattenObjectTypeDefChildren(path, qualifier, t, r);
|
|
410
|
+
case 5 /* Union */:
|
|
411
|
+
return internalFlattenUnionTypeDefChildren(path, qualifier, t, r);
|
|
412
|
+
default:
|
|
413
|
+
throw new UnreachableError4(t);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
function internalFlattenedListTypeDefChildren(path, { elements }, r) {
|
|
417
|
+
return internalFlattenTypeDef(jsonPath(path, "*"), elements, r);
|
|
418
|
+
}
|
|
419
|
+
function internalFlattenRecordTypeDefChildren(path, { valueTypeDef }, r) {
|
|
420
|
+
return internalFlattenTypeDef(jsonPath(path, "*"), valueTypeDef, r);
|
|
421
|
+
}
|
|
422
|
+
function internalFlattenObjectTypeDefChildren(path, qualifier, { fields }, r) {
|
|
423
|
+
return reduce4(
|
|
424
|
+
fields,
|
|
425
|
+
function(acc, fieldName, fieldTypeDef) {
|
|
426
|
+
return internalFlattenTypeDef(
|
|
427
|
+
jsonPath(path, fieldName, qualifier),
|
|
428
|
+
fieldTypeDef,
|
|
429
|
+
acc
|
|
430
|
+
);
|
|
431
|
+
},
|
|
432
|
+
r
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
function internalFlattenUnionTypeDefChildren(path, qualifier, {
|
|
436
|
+
discriminator,
|
|
437
|
+
unions
|
|
438
|
+
}, r) {
|
|
439
|
+
return reduce4(
|
|
440
|
+
unions,
|
|
441
|
+
function(acc, key, typeDef) {
|
|
442
|
+
return internalFlattenTypeDefChildren(
|
|
443
|
+
path,
|
|
444
|
+
discriminator != null ? `${qualifier}${key}:` : qualifier,
|
|
445
|
+
typeDef,
|
|
446
|
+
acc
|
|
447
|
+
);
|
|
448
|
+
},
|
|
449
|
+
r
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// transformers/flatteners/flatten_type_defs_of.ts
|
|
454
|
+
function flattenTypeDefsOf(t) {
|
|
455
|
+
return flattenTypeDefTo(
|
|
456
|
+
t,
|
|
457
|
+
function(definition) {
|
|
458
|
+
return { definition };
|
|
459
|
+
}
|
|
460
|
+
);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// transformers/flatteners/value_path_to_type_path.ts
|
|
464
|
+
import {
|
|
465
|
+
assertEqual,
|
|
466
|
+
assertExists,
|
|
467
|
+
assertExistsAndReturn,
|
|
468
|
+
PreconditionFailedError,
|
|
469
|
+
reduce as reduce5,
|
|
470
|
+
UnreachableError as UnreachableError5
|
|
471
|
+
} from "@strictly/base";
|
|
472
|
+
function valuePathToTypePath({ definition: typeDef }, valuePath, allowMissingPaths = false) {
|
|
473
|
+
const valueSteps = valuePath.split(/\.|\[/g);
|
|
474
|
+
assertEqual(valueSteps[0], "$");
|
|
475
|
+
const typeSteps = internalJsonValuePathToTypePath(
|
|
476
|
+
typeDef,
|
|
477
|
+
valueSteps.slice(1),
|
|
478
|
+
allowMissingPaths,
|
|
479
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
480
|
+
valuePath
|
|
481
|
+
);
|
|
482
|
+
typeSteps.unshift("$");
|
|
483
|
+
return typeSteps.join(".");
|
|
484
|
+
}
|
|
485
|
+
function internalJsonValuePathToTypePath(typeDef, valueSteps, allowMissingPaths, originalValuePath) {
|
|
486
|
+
if (valueSteps.length === 0) {
|
|
487
|
+
return [];
|
|
488
|
+
}
|
|
489
|
+
const [
|
|
490
|
+
valueStep,
|
|
491
|
+
...remainingValueSteps
|
|
492
|
+
] = valueSteps;
|
|
493
|
+
switch (typeDef.type) {
|
|
494
|
+
case 1 /* Literal */:
|
|
495
|
+
if (allowMissingPaths) {
|
|
496
|
+
return valueSteps;
|
|
497
|
+
} else {
|
|
498
|
+
throw new PreconditionFailedError(
|
|
499
|
+
"literal should terminate path {} ({})",
|
|
500
|
+
originalValuePath,
|
|
501
|
+
valueStep
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
case 2 /* List */:
|
|
505
|
+
return [
|
|
506
|
+
"*",
|
|
507
|
+
...internalJsonValuePathToTypePath(
|
|
508
|
+
typeDef.elements,
|
|
509
|
+
remainingValueSteps,
|
|
510
|
+
allowMissingPaths,
|
|
511
|
+
originalValuePath
|
|
512
|
+
)
|
|
513
|
+
];
|
|
514
|
+
case 3 /* Record */:
|
|
515
|
+
return [
|
|
516
|
+
"*",
|
|
517
|
+
...internalJsonValuePathToTypePath(
|
|
518
|
+
typeDef.valueTypeDef,
|
|
519
|
+
remainingValueSteps,
|
|
520
|
+
allowMissingPaths,
|
|
521
|
+
originalValuePath
|
|
522
|
+
)
|
|
523
|
+
];
|
|
524
|
+
case 4 /* Object */:
|
|
525
|
+
if (allowMissingPaths) {
|
|
526
|
+
if (typeDef.fields[valueStep] == null) {
|
|
527
|
+
return valueSteps;
|
|
528
|
+
}
|
|
529
|
+
} else {
|
|
530
|
+
assertExists(typeDef.fields[valueStep], "missing field in {} ({})", originalValuePath, valueStep);
|
|
531
|
+
}
|
|
532
|
+
return [
|
|
533
|
+
valueStep,
|
|
534
|
+
...internalJsonValuePathToTypePath(
|
|
535
|
+
typeDef.fields[valueStep],
|
|
536
|
+
remainingValueSteps,
|
|
537
|
+
allowMissingPaths,
|
|
538
|
+
originalValuePath
|
|
539
|
+
)
|
|
540
|
+
];
|
|
541
|
+
case 5 /* Union */:
|
|
542
|
+
if (typeDef.discriminator == null) {
|
|
543
|
+
if (remainingValueSteps.length > 0) {
|
|
544
|
+
const union2 = reduce5(
|
|
545
|
+
typeDef.unions,
|
|
546
|
+
function(acc, _k, v) {
|
|
547
|
+
if (v.type !== 1 /* Literal */ || v.type === 1 /* Literal */ && v.valuePrototype == null) {
|
|
548
|
+
return v;
|
|
549
|
+
}
|
|
550
|
+
return acc;
|
|
551
|
+
},
|
|
552
|
+
null
|
|
553
|
+
);
|
|
554
|
+
assertExists(union2, "expected a complex union {}", originalValuePath);
|
|
555
|
+
return internalJsonValuePathToTypePath(
|
|
556
|
+
union2,
|
|
557
|
+
valueSteps,
|
|
558
|
+
allowMissingPaths,
|
|
559
|
+
originalValuePath
|
|
560
|
+
);
|
|
561
|
+
} else {
|
|
562
|
+
return [];
|
|
563
|
+
}
|
|
564
|
+
} else {
|
|
565
|
+
const qualifierIndex = valueStep.indexOf(":");
|
|
566
|
+
if (qualifierIndex < 0) {
|
|
567
|
+
if (allowMissingPaths) {
|
|
568
|
+
return valueSteps;
|
|
569
|
+
} else {
|
|
570
|
+
throw new PreconditionFailedError(
|
|
571
|
+
"mismatched qualifiers in {} (at {})",
|
|
572
|
+
originalValuePath,
|
|
573
|
+
valueStep
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
const qualifier = valueStep.substring(0, qualifierIndex);
|
|
578
|
+
const remainder = valueStep.substring(qualifierIndex + 1);
|
|
579
|
+
const union2 = assertExistsAndReturn(typeDef.unions[qualifier], "missing union {}", qualifier);
|
|
580
|
+
const [
|
|
581
|
+
typeStep,
|
|
582
|
+
...remainingTypeSteps
|
|
583
|
+
] = internalJsonValuePathToTypePath(
|
|
584
|
+
union2,
|
|
585
|
+
[
|
|
586
|
+
remainder,
|
|
587
|
+
...remainingValueSteps
|
|
588
|
+
],
|
|
589
|
+
allowMissingPaths,
|
|
590
|
+
originalValuePath
|
|
591
|
+
);
|
|
592
|
+
return [
|
|
593
|
+
`${qualifier}:${typeStep}`,
|
|
594
|
+
...remainingTypeSteps
|
|
595
|
+
];
|
|
596
|
+
}
|
|
597
|
+
default:
|
|
598
|
+
throw new UnreachableError5(typeDef);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// types/builders.ts
|
|
603
|
+
var TypeDefBuilder = class {
|
|
604
|
+
constructor(definition) {
|
|
605
|
+
this.definition = definition;
|
|
606
|
+
}
|
|
607
|
+
// returns just the relevant types, which can help typescript
|
|
608
|
+
// from complaining about infinitely deep data structures
|
|
609
|
+
get narrow() {
|
|
610
|
+
return {
|
|
611
|
+
definition: this.definition
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
};
|
|
615
|
+
var LiteralTypeDefBuilder = class extends TypeDefBuilder {
|
|
616
|
+
};
|
|
617
|
+
var ListTypeDefBuilder = class extends TypeDefBuilder {
|
|
618
|
+
readonly() {
|
|
619
|
+
return this;
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
var RecordTypeDefBuilder = class extends TypeDefBuilder {
|
|
623
|
+
partial() {
|
|
624
|
+
return this;
|
|
625
|
+
}
|
|
626
|
+
readonly() {
|
|
627
|
+
return this;
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
var ObjectTypeDefBuilder = class _ObjectTypeDefBuilder extends TypeDefBuilder {
|
|
631
|
+
set(name, { definition: typeDef }) {
|
|
632
|
+
const newFields = {
|
|
633
|
+
[name]: typeDef
|
|
634
|
+
};
|
|
635
|
+
return new _ObjectTypeDefBuilder({
|
|
636
|
+
type: 4 /* Object */,
|
|
637
|
+
fields: {
|
|
638
|
+
...this.definition.fields,
|
|
639
|
+
...newFields
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
setReadonly(name, { definition: typeDef }) {
|
|
644
|
+
const newFields = {
|
|
645
|
+
[name]: typeDef
|
|
646
|
+
};
|
|
647
|
+
return new _ObjectTypeDefBuilder({
|
|
648
|
+
type: 4 /* Object */,
|
|
649
|
+
fields: {
|
|
650
|
+
...this.definition.fields,
|
|
651
|
+
...newFields
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
setOptional(name, { definition: typeDef }) {
|
|
656
|
+
const newFields = {
|
|
657
|
+
[name]: typeDef
|
|
658
|
+
};
|
|
659
|
+
return new _ObjectTypeDefBuilder({
|
|
660
|
+
type: 4 /* Object */,
|
|
661
|
+
fields: {
|
|
662
|
+
...this.definition.fields,
|
|
663
|
+
...newFields
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
setReadonlyOptional(name, { definition: typeDef }) {
|
|
668
|
+
const newFields = {
|
|
669
|
+
[name]: typeDef
|
|
670
|
+
};
|
|
671
|
+
return new _ObjectTypeDefBuilder({
|
|
672
|
+
type: 4 /* Object */,
|
|
673
|
+
fields: {
|
|
674
|
+
...this.definition.fields,
|
|
675
|
+
...newFields
|
|
676
|
+
}
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
var UnionTypeDefBuilder = class _UnionTypeDefBuilder extends TypeDefBuilder {
|
|
681
|
+
add(k, {
|
|
682
|
+
definition: typeDef
|
|
683
|
+
}) {
|
|
684
|
+
const {
|
|
685
|
+
discriminator,
|
|
686
|
+
unions
|
|
687
|
+
} = this.definition;
|
|
688
|
+
return new _UnionTypeDefBuilder(
|
|
689
|
+
{
|
|
690
|
+
type: 5 /* Union */,
|
|
691
|
+
discriminator,
|
|
692
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
693
|
+
unions: {
|
|
694
|
+
...unions,
|
|
695
|
+
[k]: typeDef
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
};
|
|
701
|
+
function literal(value) {
|
|
702
|
+
return new LiteralTypeDefBuilder({
|
|
703
|
+
type: 1 /* Literal */,
|
|
704
|
+
valuePrototype: value
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
var stringType = literal();
|
|
708
|
+
var numberType = literal();
|
|
709
|
+
var booleanType = literal();
|
|
710
|
+
var nullType = literal([null]);
|
|
711
|
+
function nullable(nonNullable) {
|
|
712
|
+
return new UnionTypeDefBuilder(
|
|
713
|
+
{
|
|
714
|
+
type: 5 /* Union */,
|
|
715
|
+
discriminator: null,
|
|
716
|
+
unions: {
|
|
717
|
+
["0"]: nonNullable.definition,
|
|
718
|
+
["1"]: nullType.definition
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
);
|
|
722
|
+
}
|
|
723
|
+
function list(elements) {
|
|
724
|
+
return new ListTypeDefBuilder({
|
|
725
|
+
type: 2 /* List */,
|
|
726
|
+
elements: elements.definition
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
function record({ definition: typeDef }) {
|
|
730
|
+
return new RecordTypeDefBuilder({
|
|
731
|
+
type: 3 /* Record */,
|
|
732
|
+
// eslint-disable-next-line no-undefined
|
|
733
|
+
keyPrototype: void 0,
|
|
734
|
+
valueTypeDef: typeDef
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
function object() {
|
|
738
|
+
return new ObjectTypeDefBuilder({
|
|
739
|
+
type: 4 /* Object */,
|
|
740
|
+
fields: {}
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
function union(discriminator) {
|
|
744
|
+
return new UnionTypeDefBuilder(
|
|
745
|
+
{
|
|
746
|
+
type: 5 /* Union */,
|
|
747
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
748
|
+
discriminator: discriminator ?? null,
|
|
749
|
+
unions: {}
|
|
750
|
+
}
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
export {
|
|
754
|
+
TypeDefType,
|
|
755
|
+
booleanType,
|
|
756
|
+
copy,
|
|
757
|
+
flattenAccessorsOf,
|
|
758
|
+
flattenJsonValueToTypePathsOf,
|
|
759
|
+
flattenTypeDefsOf,
|
|
760
|
+
flattenValueTypeTo,
|
|
761
|
+
getUnionTypeDef,
|
|
762
|
+
jsonPath,
|
|
763
|
+
jsonPathPop,
|
|
764
|
+
list,
|
|
765
|
+
literal,
|
|
766
|
+
mobxCopy,
|
|
767
|
+
nullType,
|
|
768
|
+
nullable,
|
|
769
|
+
numberType,
|
|
770
|
+
object,
|
|
771
|
+
record,
|
|
772
|
+
stringType,
|
|
773
|
+
union,
|
|
774
|
+
valuePathToTypePath
|
|
775
|
+
};
|