typia 9.7.2 → 10.0.0-dev.20251107

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.
Files changed (116) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +153 -153
  3. package/lib/factories/ProtobufFactory.js +1 -1
  4. package/lib/factories/ProtobufFactory.mjs +1 -1
  5. package/lib/programmers/internal/json_schema_station.d.mts +2 -2
  6. package/lib/programmers/internal/json_schema_station.d.ts +2 -2
  7. package/lib/programmers/llm/LlmApplicationProgrammer.js +5 -1
  8. package/lib/programmers/llm/LlmApplicationProgrammer.js.map +1 -1
  9. package/lib/programmers/llm/LlmApplicationProgrammer.mjs +5 -1
  10. package/lib/programmers/llm/LlmSchemaProgrammer.js +1 -4
  11. package/lib/programmers/llm/LlmSchemaProgrammer.js.map +1 -1
  12. package/lib/programmers/llm/LlmSchemaProgrammer.mjs +1 -35
  13. package/package.json +121 -121
  14. package/src/AssertionGuard.ts +41 -41
  15. package/src/CamelCase.ts +75 -75
  16. package/src/IRandomGenerator.ts +337 -337
  17. package/src/IReadableURLSearchParams.ts +9 -9
  18. package/src/PascalCase.ts +71 -71
  19. package/src/Primitive.ts +90 -90
  20. package/src/Resolved.ts +72 -72
  21. package/src/SnakeCase.ts +127 -127
  22. package/src/TypeGuardError.ts +216 -216
  23. package/src/factories/MetadataCollection.ts +270 -270
  24. package/src/factories/MetadataCommentTagFactory.ts +632 -632
  25. package/src/factories/MetadataFactory.ts +402 -402
  26. package/src/factories/ProtobufFactory.ts +873 -873
  27. package/src/functional.ts +705 -705
  28. package/src/http.ts +972 -972
  29. package/src/internal/_ProtobufReader.ts +188 -188
  30. package/src/internal/_ProtobufSizer.ts +137 -137
  31. package/src/internal/_ProtobufWriter.ts +135 -135
  32. package/src/internal/_jsonStringifyString.ts +42 -42
  33. package/src/json.ts +643 -643
  34. package/src/llm.ts +615 -615
  35. package/src/misc.ts +594 -594
  36. package/src/module.ts +889 -889
  37. package/src/notations.ts +751 -751
  38. package/src/programmers/FeatureProgrammer.ts +605 -605
  39. package/src/programmers/ImportProgrammer.ts +179 -179
  40. package/src/programmers/RandomProgrammer.ts +1195 -1195
  41. package/src/programmers/helpers/ProtobufWire.ts +34 -34
  42. package/src/programmers/internal/check_array_length.ts +43 -43
  43. package/src/programmers/internal/check_bigint.ts +46 -46
  44. package/src/programmers/internal/check_dynamic_key.ts +197 -197
  45. package/src/programmers/internal/check_dynamic_properties.ts +231 -231
  46. package/src/programmers/internal/check_everything.ts +21 -21
  47. package/src/programmers/internal/check_native.ts +23 -23
  48. package/src/programmers/internal/check_number.ts +108 -108
  49. package/src/programmers/internal/check_object.ts +72 -72
  50. package/src/programmers/internal/check_string.ts +46 -46
  51. package/src/programmers/internal/check_template.ts +46 -46
  52. package/src/programmers/internal/check_union_array_like.ts +331 -331
  53. package/src/programmers/internal/decode_union_object.ts +110 -110
  54. package/src/programmers/internal/feature_object_entries.ts +59 -59
  55. package/src/programmers/internal/json_schema_escaped.ts +78 -78
  56. package/src/programmers/internal/json_schema_object.ts +150 -150
  57. package/src/programmers/internal/json_schema_station.ts +2 -2
  58. package/src/programmers/internal/metadata_to_pattern.ts +40 -40
  59. package/src/programmers/internal/postfix_of_tuple.ts +3 -3
  60. package/src/programmers/internal/prune_object_properties.ts +69 -69
  61. package/src/programmers/internal/stringify_dynamic_properties.ts +158 -158
  62. package/src/programmers/internal/stringify_native.ts +5 -5
  63. package/src/programmers/internal/stringify_regular_properties.ts +77 -77
  64. package/src/programmers/internal/template_to_pattern.ts +21 -21
  65. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +21 -21
  66. package/src/programmers/json/JsonStringifyProgrammer.ts +1124 -1124
  67. package/src/programmers/llm/LlmApplicationProgrammer.ts +10 -1
  68. package/src/programmers/llm/LlmSchemaProgrammer.ts +2 -7
  69. package/src/protobuf.ts +820 -820
  70. package/src/reflect.ts +46 -46
  71. package/src/schemas/json/IJsonApplication.ts +77 -77
  72. package/src/schemas/json/IJsonSchemaCollection.ts +212 -212
  73. package/src/schemas/json/IJsonSchemaUnit.ts +263 -263
  74. package/src/schemas/metadata/IMetadataTypeTag.ts +14 -14
  75. package/src/schemas/metadata/Metadata.ts +669 -669
  76. package/src/schemas/metadata/MetadataAliasType.ts +57 -57
  77. package/src/schemas/metadata/MetadataApplication.ts +40 -40
  78. package/src/schemas/metadata/MetadataArray.ts +47 -47
  79. package/src/schemas/metadata/MetadataArrayType.ts +51 -51
  80. package/src/schemas/metadata/MetadataAtomic.ts +85 -85
  81. package/src/schemas/metadata/MetadataEscaped.ts +45 -45
  82. package/src/schemas/metadata/MetadataFunction.ts +45 -45
  83. package/src/schemas/metadata/MetadataObject.ts +46 -46
  84. package/src/schemas/metadata/MetadataObjectType.ts +137 -137
  85. package/src/schemas/metadata/MetadataParameter.ts +52 -52
  86. package/src/schemas/metadata/MetadataProperty.ts +53 -53
  87. package/src/schemas/metadata/MetadataTemplate.ts +78 -78
  88. package/src/schemas/metadata/MetadataTuple.ts +28 -28
  89. package/src/schemas/metadata/MetadataTupleType.ts +61 -61
  90. package/src/tags/Constant.ts +47 -47
  91. package/src/tags/ContentMediaType.ts +27 -27
  92. package/src/tags/Default.ts +52 -52
  93. package/src/tags/Example.ts +56 -56
  94. package/src/tags/Examples.ts +56 -56
  95. package/src/tags/ExclusiveMaximum.ts +44 -44
  96. package/src/tags/ExclusiveMinimum.ts +44 -44
  97. package/src/tags/Format.ts +78 -78
  98. package/src/tags/JsonSchemaPlugin.ts +36 -36
  99. package/src/tags/MaxItems.ts +31 -31
  100. package/src/tags/MaxLength.ts +25 -25
  101. package/src/tags/Maximum.ts +39 -39
  102. package/src/tags/MinItems.ts +31 -31
  103. package/src/tags/MinLength.ts +25 -25
  104. package/src/tags/Minimum.ts +39 -39
  105. package/src/tags/MultipleOf.ts +42 -42
  106. package/src/tags/Pattern.ts +49 -49
  107. package/src/tags/Sequence.ts +37 -37
  108. package/src/tags/TagBase.ts +102 -102
  109. package/src/tags/Type.ts +64 -64
  110. package/src/tags/UniqueItems.ts +34 -34
  111. package/src/tags/internal/FormatCheatSheet.ts +71 -71
  112. package/src/transformers/ITransformOptions.ts +70 -70
  113. package/src/transformers/ImportTransformer.ts +253 -253
  114. package/src/transformers/NoTransformConfigurationError.ts +16 -16
  115. package/src/transformers/features/llm/LlmApplicationTransformer.ts +224 -224
  116. package/src/typings/Equal.ts +18 -18
@@ -1,669 +1,669 @@
1
- import { ClassProperties } from "../../typings/ClassProperties";
2
-
3
- import { ArrayUtil } from "../../utils/ArrayUtil";
4
-
5
- import { IMetadata } from "./IMetadata";
6
- import { IMetadataDictionary } from "./IMetadataDictionary";
7
- import { IMetadataTypeTag } from "./IMetadataTypeTag";
8
- import { MetadataAlias } from "./MetadataAlias";
9
- import { MetadataArray } from "./MetadataArray";
10
- import { MetadataAtomic } from "./MetadataAtomic";
11
- import { MetadataConstant } from "./MetadataConstant";
12
- import { MetadataEscaped } from "./MetadataEscaped";
13
- import { MetadataFunction } from "./MetadataFunction";
14
- import { MetadataMap } from "./MetadataMap";
15
- import { MetadataNative } from "./MetadataNative";
16
- import { MetadataObject } from "./MetadataObject";
17
- import { MetadataObjectType } from "./MetadataObjectType";
18
- import { MetadataSet } from "./MetadataSet";
19
- import { MetadataTemplate } from "./MetadataTemplate";
20
- import { MetadataTuple } from "./MetadataTuple";
21
-
22
- export class Metadata {
23
- public any: boolean;
24
- public required: boolean;
25
- public optional: boolean;
26
- public nullable: boolean;
27
-
28
- public escaped: MetadataEscaped | null;
29
- public atomics: MetadataAtomic[];
30
- public constants: MetadataConstant[];
31
- public templates: MetadataTemplate[];
32
-
33
- public rest: Metadata | null;
34
- public aliases: MetadataAlias[];
35
- public arrays: MetadataArray[];
36
- public tuples: MetadataTuple[];
37
- public objects: MetadataObject[];
38
- public functions: MetadataFunction[];
39
-
40
- public natives: MetadataNative[];
41
- public sets: MetadataSet[];
42
- public maps: MetadataMap[];
43
-
44
- /** @internal */ private name_?: string;
45
- /** @internal */ private parent_resolved_: boolean = false;
46
- /** @internal */ public union_index?: number;
47
- /** @internal */ public fixed_?: number | null;
48
- /** @internal */ public boolean_literal_intersected_?: boolean;
49
- /** @internal */ private is_sequence_?: boolean;
50
-
51
- /* -----------------------------------------------------------
52
- CONSTRUCTORS
53
- ----------------------------------------------------------- */
54
- /** @ignore */
55
- private constructor(props: ClassProperties<Metadata>) {
56
- this.any = props.any;
57
- this.required = props.required;
58
- this.optional = props.optional;
59
- this.nullable = props.nullable;
60
- this.functions = props.functions;
61
-
62
- this.escaped = props.escaped;
63
- this.atomics = props.atomics;
64
- this.constants = props.constants;
65
- this.templates = props.templates;
66
-
67
- this.rest = props.rest;
68
- this.arrays = props.arrays;
69
- this.tuples = props.tuples;
70
- this.objects = props.objects;
71
- this.aliases = props.aliases;
72
-
73
- this.natives = props.natives;
74
- this.sets = props.sets;
75
- this.maps = props.maps;
76
- }
77
-
78
- /** @internal */
79
- public static create(props: ClassProperties<Metadata>): Metadata {
80
- return new Metadata(props);
81
- }
82
-
83
- /** @internal */
84
- public static initialize(parentResolved: boolean = false): Metadata {
85
- const meta: Metadata = Metadata.create({
86
- any: false,
87
- nullable: false,
88
- required: true,
89
- optional: false,
90
-
91
- escaped: null,
92
- constants: [],
93
- atomics: [],
94
- templates: [],
95
- arrays: [],
96
- tuples: [],
97
- objects: [],
98
- aliases: [],
99
- functions: [],
100
-
101
- rest: null,
102
- natives: [],
103
- sets: [],
104
- maps: [],
105
- });
106
- meta.parent_resolved_ = parentResolved;
107
- return meta;
108
- }
109
-
110
- public toJSON(): IMetadata {
111
- return {
112
- any: this.any,
113
- required: this.required,
114
- optional: this.optional,
115
- nullable: this.nullable,
116
- functions: this.functions.map((f) => f.toJSON()),
117
-
118
- atomics: this.atomics.map((a) => a.toJSON()),
119
- constants: this.constants.map((c) => c.toJSON()),
120
- templates: this.templates.map((tpl) => tpl.toJSON()),
121
- escaped: this.escaped ? this.escaped.toJSON() : null,
122
-
123
- rest: this.rest ? this.rest.toJSON() : null,
124
- arrays: this.arrays.map((array) => array.toJSON()),
125
- tuples: this.tuples.map((tuple) => tuple.toJSON()),
126
- objects: this.objects.map((obj) => obj.toJSON()),
127
- aliases: this.aliases.map((alias) => alias.toJSON()),
128
-
129
- natives: this.natives.map((native) => native.toJSON()),
130
- sets: this.sets.map((set) => set.toJSON()),
131
- maps: this.maps.map((map) => map.toJSON()),
132
- };
133
- }
134
-
135
- public static from(meta: IMetadata, dict: IMetadataDictionary): Metadata {
136
- return Metadata.create({
137
- any: meta.any,
138
- required: meta.required,
139
- optional: meta.optional,
140
- nullable: meta.nullable,
141
- functions: meta.functions.map((f) => MetadataFunction.from(f, dict)),
142
-
143
- constants: meta.constants.map(MetadataConstant.from),
144
- atomics: meta.atomics.map(MetadataAtomic.from),
145
- templates: meta.templates.map((tpl) => MetadataTemplate.from(tpl, dict)),
146
- escaped: meta.escaped ? MetadataEscaped.from(meta.escaped, dict) : null,
147
-
148
- rest: meta.rest ? this.from(meta.rest, dict) : null,
149
- arrays: meta.arrays.map((ref) => {
150
- const type = dict.arrays.get(ref.name);
151
- if (type === undefined)
152
- throw new RangeError(
153
- `Error on Metadata.from(): failed to find array "${ref.name}".`,
154
- );
155
- return MetadataArray.create({
156
- type,
157
- tags: ref.tags.map((row) => row.slice()),
158
- });
159
- }),
160
- tuples: meta.tuples.map((t) => {
161
- const type = dict.tuples.get(t.name);
162
- if (type === undefined)
163
- throw new RangeError(
164
- `Error on Metadata.from(): failed to find tuple "${t.name}".`,
165
- );
166
- return MetadataTuple.create({
167
- type,
168
- tags: t.tags.map((r) => r.slice()),
169
- });
170
- }),
171
- objects: meta.objects.map((obj) => {
172
- const found = dict.objects.get(obj.name);
173
- if (found === undefined)
174
- throw new RangeError(
175
- `Error on Metadata.from(): failed to find object "${name}".`,
176
- );
177
- return MetadataObject.create({
178
- type: found,
179
- tags: obj.tags.map((r) => r.slice()),
180
- });
181
- }),
182
- aliases: meta.aliases.map((alias) => {
183
- const type = dict.aliases.get(alias.name);
184
- if (type === undefined)
185
- throw new RangeError(
186
- `Error on Metadata.from(): failed to find alias "${alias}".`,
187
- );
188
- return MetadataAlias.create({
189
- type,
190
- tags: alias.tags.map((r) => r.slice()),
191
- });
192
- }),
193
- natives: meta.natives.map((native) => MetadataNative.create(native)),
194
- sets: meta.sets.map((set) =>
195
- MetadataSet.create({
196
- value: Metadata.from(set.value, dict),
197
- tags: set.tags.map((r) => r.slice()),
198
- }),
199
- ),
200
- maps: meta.maps.map((map) =>
201
- MetadataMap.create({
202
- key: this.from(map.key, dict),
203
- value: this.from(map.value, dict),
204
- tags: map.tags.map((r) => r.slice()),
205
- }),
206
- ),
207
- });
208
- }
209
-
210
- /* -----------------------------------------------------------
211
- ACCESSORS
212
- ----------------------------------------------------------- */
213
- public getName(): string {
214
- return (this.name_ ??= getName(this));
215
- }
216
-
217
- public empty(): boolean {
218
- return this.bucket() === 0 || this.size() === 0;
219
- }
220
-
221
- public size(): number {
222
- return (
223
- (this.any ? 1 : 0) +
224
- (this.escaped ? 1 : 0) +
225
- (this.rest ? this.rest.size() : 0) +
226
- this.templates.length +
227
- this.atomics.length +
228
- this.constants.map((c) => c.values.length).reduce((x, y) => x + y, 0) +
229
- this.arrays.length +
230
- this.tuples.length +
231
- this.natives.length +
232
- this.maps.length +
233
- this.sets.length +
234
- this.objects.length +
235
- this.functions.length +
236
- this.aliases.length
237
- );
238
- }
239
-
240
- public bucket(): number {
241
- return (
242
- (this.any ? 1 : 0) +
243
- (this.escaped ? 1 : 0) +
244
- (this.templates.length ? 1 : 0) +
245
- (this.atomics.length ? 1 : 0) +
246
- (this.constants.length ? 1 : 0) +
247
- (this.rest ? this.rest.size() : 0) +
248
- (this.arrays.length ? 1 : 0) +
249
- (this.tuples.length ? 1 : 0) +
250
- (this.natives.length ? 1 : 0) +
251
- (this.sets.length ? 1 : 0) +
252
- (this.maps.length ? 1 : 0) +
253
- (this.objects.length ? 1 : 0) +
254
- (this.functions.length ? 1 : 0) +
255
- (this.aliases.length ? 1 : 0)
256
- );
257
- }
258
-
259
- /** @internal */
260
- public isSequence(): boolean {
261
- return (this.is_sequence_ ??= (() => {
262
- const exists = (tags: IMetadataTypeTag[][]): boolean =>
263
- tags.some((row) => {
264
- const sequence = row.find(
265
- (t) =>
266
- t.kind === "sequence" &&
267
- typeof (t.schema as any)?.["x-protobuf-sequence"] === "number",
268
- );
269
- if (sequence === undefined) return false;
270
- const value: number = Number(
271
- (sequence.schema as any)["x-protobuf-sequence"],
272
- );
273
- return !Number.isNaN(value);
274
- });
275
- return (
276
- this.atomics.some((atomic) => exists(atomic.tags)) &&
277
- this.constants.some((c) =>
278
- c.values.some((v) => exists(v.tags ?? [])),
279
- ) &&
280
- this.templates.some((tpl) => exists(tpl.tags)) &&
281
- this.arrays.some((array) => exists(array.tags)) &&
282
- this.objects.some((object) => exists(object.tags)) &&
283
- this.natives.some(
284
- (native) => native.name === "Uint8Array" && exists(native.tags),
285
- )
286
- );
287
- })());
288
- }
289
-
290
- public isConstant(): boolean {
291
- return this.bucket() === (this.constants.length ? 1 : 0);
292
- }
293
-
294
- public isRequired(): boolean {
295
- return this.required === true && this.optional === false;
296
- }
297
-
298
- /** @internal */
299
- public isUnionBucket(): boolean {
300
- const size: number = this.bucket();
301
- const emended: number =
302
- !!this.atomics.length && !!this.constants.length ? size - 1 : size;
303
- return emended > 1;
304
- }
305
-
306
- /** @internal */
307
- public getSoleLiteral(): string | null {
308
- if (
309
- this.size() === 1 &&
310
- this.constants.length === 1 &&
311
- this.constants[0]!.type === "string" &&
312
- this.constants[0]!.values.length === 1
313
- )
314
- return this.constants[0]!.values[0]!.value as string;
315
- else return null;
316
- }
317
-
318
- public isSoleLiteral(): boolean {
319
- return this.getSoleLiteral() !== null;
320
- }
321
-
322
- /** @internal */
323
- public isParentResolved(): boolean {
324
- return this.parent_resolved_;
325
- }
326
- }
327
- export namespace Metadata {
328
- export const intersects = (x: Metadata, y: Metadata): boolean => {
329
- // CHECK ANY & OPTIONAL
330
- if (x.any || y.any) return true;
331
- if (x.isRequired() === false && false === y.isRequired()) return true;
332
- if (x.nullable === true && true === y.nullable) return true;
333
- if (!!x.functions.length && !!y.functions.length === true) return true;
334
-
335
- //----
336
- // INSTANCES
337
- //----
338
- // ARRAYS
339
- if (x.arrays.length && y.arrays.length) return true;
340
- if (x.tuples.length && y.tuples.length) return true;
341
- if (x.objects.length && y.objects.length) return true;
342
- if (x.aliases.length && y.aliases.length) return true;
343
-
344
- // NATIVES
345
- if (x.natives.length && y.natives.length)
346
- if (x.natives.some((xn) => y.natives.some((yn) => xn === yn)))
347
- return true;
348
-
349
- // ESCAPED
350
- if (x.escaped && y.escaped)
351
- return (
352
- intersects(x.escaped.original, y.escaped.original) ||
353
- intersects(x.escaped.returns, y.escaped.returns)
354
- );
355
-
356
- //----
357
- // VALUES
358
- //----
359
- // ATOMICS
360
- for (const atomic of x.atomics) {
361
- if (y.atomics.some((ya) => atomic.type === ya.type)) return true;
362
- if (y.constants.some((yc) => atomic.type === yc.type)) return true;
363
- }
364
-
365
- // CONSTANTS
366
- for (const constant of x.constants) {
367
- const atomic: MetadataAtomic | undefined = y.atomics.find(
368
- (elem) => elem.type === constant.type,
369
- );
370
- if (atomic !== undefined) return true;
371
-
372
- const opposite: MetadataConstant | undefined = y.constants.find(
373
- (elem) => elem.type === constant.type,
374
- );
375
- if (opposite === undefined) continue;
376
-
377
- const values: Set<any> = new Set([
378
- ...constant.values.map((e) => e.value),
379
- ...opposite.values.map((e) => e.value),
380
- ]);
381
- if (values.size !== constant.values.length + opposite.values.length)
382
- return true;
383
- }
384
-
385
- // TEMPLATES
386
- if (!!x.templates.length && y.atomics.some((ya) => ya.type === "string"))
387
- return true;
388
- else if (
389
- !!y.templates.length &&
390
- x.atomics.some((xa) => xa.type === "string")
391
- )
392
- return true;
393
- return false;
394
- };
395
-
396
- export const covers = (
397
- x: Metadata,
398
- y: Metadata,
399
- level: number = 0,
400
- escaped: boolean = false,
401
- ): boolean => {
402
- // CHECK ANY
403
- if (x === y) return false;
404
- else if (x.any) return true;
405
- else if (y.any) return false;
406
-
407
- if (escaped === false) {
408
- if (x.escaped === null && y.escaped !== null) return false;
409
- else if (
410
- x.escaped !== null &&
411
- y.escaped !== null &&
412
- (!covers(x.escaped.original, y.escaped.original, level, true) ||
413
- !covers(x.escaped.returns, y.escaped.returns, level, true))
414
- )
415
- return false;
416
- }
417
-
418
- //----
419
- // INSTANCES
420
- //----
421
- if (level === 0) {
422
- // ARRAYS
423
- for (const ya of y.arrays)
424
- if (
425
- !x.arrays.some((xa) =>
426
- covers(xa.type.value, ya.type.value, level + 1),
427
- )
428
- ) {
429
- return false;
430
- }
431
-
432
- // TUPLES
433
- for (const yt of y.tuples)
434
- if (
435
- yt.type.elements.length !== 0 &&
436
- x.tuples.some(
437
- (xt) =>
438
- xt.type.elements.length >= yt.type.elements.length &&
439
- xt.type.elements
440
- .slice(yt.type.elements.length)
441
- .every((xv, i) => covers(xv, yt.type.elements[i]!, level + 1)),
442
- ) === false
443
- )
444
- return false;
445
- }
446
-
447
- // OBJECTS
448
- for (const yo of y.objects)
449
- if (
450
- x.objects.some((xo) => MetadataObjectType.covers(xo.type, yo.type)) ===
451
- false
452
- )
453
- return false;
454
-
455
- // ALIASES
456
- for (const yd of y.aliases)
457
- if (x.aliases.some((xd) => xd.type.name === yd.type.name) === false)
458
- return false;
459
-
460
- // NATIVES
461
- for (const yn of y.natives)
462
- if (x.natives.some((xn) => xn === yn) === false) return false;
463
-
464
- // SETS
465
- for (const ys of y.sets)
466
- if (x.sets.some((xs) => covers(xs.value, ys.value)) === false)
467
- return false;
468
-
469
- //----
470
- // VALUES
471
- //----
472
- // ATOMICS
473
- if (
474
- y.atomics.some(
475
- (ya) => x.atomics.some((xa) => xa.type === ya.type) === false,
476
- )
477
- )
478
- return false;
479
-
480
- // CONSTANTS
481
- for (const yc of y.constants) {
482
- if (x.atomics.some((atom) => yc.type === atom.type)) continue;
483
- const xc: MetadataConstant | undefined = x.constants.find(
484
- (elem) => elem.type === yc.type,
485
- );
486
- if (xc === undefined) return false;
487
- else if (
488
- (yc.values.map((e) => e.value) as number[]).some(
489
- (yv) => xc.values.includes(yv as never) === false,
490
- )
491
- )
492
- return false;
493
- }
494
-
495
- // FUNCTIONAL
496
- if (!!x.functions.length === false && !!y.functions.length) return false;
497
-
498
- // SUCCESS
499
- return true;
500
- };
501
-
502
- /** @internal */
503
- export const merge = (x: Metadata, y: Metadata): Metadata => {
504
- const output: Metadata = Metadata.create({
505
- any: x.any || y.any,
506
- nullable: x.nullable || y.nullable,
507
- required: x.required && y.required,
508
- optional: x.optional || y.optional,
509
- functions: x.functions.length ? x.functions : y.functions, // @todo
510
- escaped:
511
- x.escaped !== null && y.escaped !== null
512
- ? MetadataEscaped.create({
513
- original: merge(x.escaped.original, y.escaped.original),
514
- returns: merge(x.escaped.returns, y.escaped.returns),
515
- })
516
- : (x.escaped ?? y.escaped),
517
- atomics: mergeTaggedTypes({
518
- container: x.atomics,
519
- equals: (x, y) => x.type === y.type,
520
- getter: (x) => x.tags,
521
- })(y.atomics),
522
- constants: [...x.constants],
523
- templates: x.templates.slice(),
524
- rest:
525
- x.rest !== null && y.rest !== null
526
- ? merge(x.rest, y.rest)
527
- : (x.rest ?? y.rest),
528
- arrays: mergeTaggedTypes({
529
- container: x.arrays,
530
- equals: (x, y) => x.type.name === y.type.name,
531
- getter: (x) => x.tags,
532
- })(y.arrays),
533
- tuples: mergeTaggedTypes({
534
- container: x.tuples,
535
- equals: (x, y) => x.type.name === y.type.name,
536
- getter: (x) => x.tags,
537
- })(y.tuples),
538
- objects: mergeTaggedTypes({
539
- container: x.objects,
540
- equals: (x, y) => x.type.name === y.type.name,
541
- getter: (x) => x.tags,
542
- })(y.objects),
543
- aliases: mergeTaggedTypes({
544
- container: x.aliases,
545
- equals: (x, y) => x.type.name === y.type.name,
546
- getter: (x) => x.tags,
547
- })(y.aliases),
548
- natives: mergeTaggedTypes({
549
- container: x.natives,
550
- equals: (x, y) => x.name === y.name,
551
- getter: (x) => x.tags,
552
- })(y.natives),
553
- sets: mergeTaggedTypes({
554
- container: x.sets,
555
- equals: (x, y) => x.value.getName() === y.value.getName(),
556
- getter: (x) => x.tags,
557
- })(y.sets),
558
- maps: mergeTaggedTypes({
559
- container: x.maps,
560
- equals: (x, y) =>
561
- x.key.getName() === y.key.getName() &&
562
- x.value.getName() === y.value.getName(),
563
- getter: (x) => x.tags,
564
- })(y.maps),
565
- });
566
- for (const constant of y.constants) {
567
- const target: MetadataConstant = ArrayUtil.take(
568
- output.constants,
569
- (elem) => elem.type === constant.type,
570
- () =>
571
- MetadataConstant.create({
572
- type: constant.type,
573
- values: [],
574
- }),
575
- );
576
- for (const value of constant.values)
577
- ArrayUtil.add(target.values, value, (a, b) => a.value === b.value);
578
- }
579
- return output;
580
- };
581
-
582
- /** @internal */
583
- export const unalias = (w: Metadata) => {
584
- const visited: Set<Metadata> = new Set();
585
- while (
586
- w.size() === 1 &&
587
- w.nullable === false &&
588
- w.isRequired() === true &&
589
- w.aliases.length === 1
590
- ) {
591
- if (visited.has(w)) break;
592
- w = w.aliases[0]!.type.value;
593
- visited.add(w);
594
- }
595
- return w;
596
- };
597
- }
598
-
599
- const getName = (metadata: Metadata): string => {
600
- if (metadata.any === true) return "any";
601
-
602
- const elements: string[] = [];
603
-
604
- // OPTIONAL
605
- if (metadata.nullable === true) elements.push("null");
606
- if (metadata.isRequired() === false) elements.push("undefined");
607
-
608
- // ATOMIC
609
- for (const atom of metadata.atomics) {
610
- elements.push(atom.getName());
611
- }
612
- for (const constant of metadata.constants)
613
- for (const value of constant.values) elements.push(value.getName());
614
- for (const template of metadata.templates) elements.push(template.getName());
615
-
616
- // NATIVES
617
- for (const native of metadata.natives) elements.push(native.getName());
618
- for (const set of metadata.sets) elements.push(set.getName());
619
- for (const map of metadata.maps) elements.push(map.getName());
620
-
621
- // INSTANCES
622
- if (metadata.rest !== null) elements.push(`...${metadata.rest.getName()}`);
623
- for (const tuple of metadata.tuples) elements.push(tuple.type.name);
624
- for (const array of metadata.arrays) elements.push(array.getName());
625
- for (const object of metadata.objects) elements.push(object.getName());
626
- for (const alias of metadata.aliases) elements.push(alias.getName());
627
- if (metadata.escaped !== null) elements.push(metadata.escaped.getName());
628
-
629
- // RETURNS
630
- if (elements.length === 0) return "unknown";
631
- else if (elements.length === 1) return elements[0]!;
632
-
633
- elements.sort();
634
- return `(${elements.join(" | ")})`;
635
- };
636
-
637
- const mergeTaggedTypes =
638
- <T>(props: {
639
- container: T[];
640
- equals: (x: T, y: T) => boolean;
641
- getter: (x: T) => IMetadataTypeTag[][];
642
- }) =>
643
- (opposite: T[]) => {
644
- const output: T[] = [...props.container];
645
- for (const elem of opposite) {
646
- const equal = props.container.find((x) => props.equals(x, elem));
647
- if (equal === undefined) {
648
- output.push(elem);
649
- continue;
650
- }
651
-
652
- const matrix: string[][] = props
653
- .getter(equal)
654
- .map((tags) => tags.map((t) => t.name))
655
- .sort();
656
- for (const tags of props.getter(elem)) {
657
- const names: string[] = tags.map((t) => t.name).sort();
658
- if (
659
- matrix.some(
660
- (m) =>
661
- m.length === names.length && m.every((s, i) => s === names[i]),
662
- )
663
- )
664
- continue;
665
- props.getter(equal).push(tags);
666
- }
667
- }
668
- return output;
669
- };
1
+ import { ClassProperties } from "../../typings/ClassProperties";
2
+
3
+ import { ArrayUtil } from "../../utils/ArrayUtil";
4
+
5
+ import { IMetadata } from "./IMetadata";
6
+ import { IMetadataDictionary } from "./IMetadataDictionary";
7
+ import { IMetadataTypeTag } from "./IMetadataTypeTag";
8
+ import { MetadataAlias } from "./MetadataAlias";
9
+ import { MetadataArray } from "./MetadataArray";
10
+ import { MetadataAtomic } from "./MetadataAtomic";
11
+ import { MetadataConstant } from "./MetadataConstant";
12
+ import { MetadataEscaped } from "./MetadataEscaped";
13
+ import { MetadataFunction } from "./MetadataFunction";
14
+ import { MetadataMap } from "./MetadataMap";
15
+ import { MetadataNative } from "./MetadataNative";
16
+ import { MetadataObject } from "./MetadataObject";
17
+ import { MetadataObjectType } from "./MetadataObjectType";
18
+ import { MetadataSet } from "./MetadataSet";
19
+ import { MetadataTemplate } from "./MetadataTemplate";
20
+ import { MetadataTuple } from "./MetadataTuple";
21
+
22
+ export class Metadata {
23
+ public any: boolean;
24
+ public required: boolean;
25
+ public optional: boolean;
26
+ public nullable: boolean;
27
+
28
+ public escaped: MetadataEscaped | null;
29
+ public atomics: MetadataAtomic[];
30
+ public constants: MetadataConstant[];
31
+ public templates: MetadataTemplate[];
32
+
33
+ public rest: Metadata | null;
34
+ public aliases: MetadataAlias[];
35
+ public arrays: MetadataArray[];
36
+ public tuples: MetadataTuple[];
37
+ public objects: MetadataObject[];
38
+ public functions: MetadataFunction[];
39
+
40
+ public natives: MetadataNative[];
41
+ public sets: MetadataSet[];
42
+ public maps: MetadataMap[];
43
+
44
+ /** @internal */ private name_?: string;
45
+ /** @internal */ private parent_resolved_: boolean = false;
46
+ /** @internal */ public union_index?: number;
47
+ /** @internal */ public fixed_?: number | null;
48
+ /** @internal */ public boolean_literal_intersected_?: boolean;
49
+ /** @internal */ private is_sequence_?: boolean;
50
+
51
+ /* -----------------------------------------------------------
52
+ CONSTRUCTORS
53
+ ----------------------------------------------------------- */
54
+ /** @ignore */
55
+ private constructor(props: ClassProperties<Metadata>) {
56
+ this.any = props.any;
57
+ this.required = props.required;
58
+ this.optional = props.optional;
59
+ this.nullable = props.nullable;
60
+ this.functions = props.functions;
61
+
62
+ this.escaped = props.escaped;
63
+ this.atomics = props.atomics;
64
+ this.constants = props.constants;
65
+ this.templates = props.templates;
66
+
67
+ this.rest = props.rest;
68
+ this.arrays = props.arrays;
69
+ this.tuples = props.tuples;
70
+ this.objects = props.objects;
71
+ this.aliases = props.aliases;
72
+
73
+ this.natives = props.natives;
74
+ this.sets = props.sets;
75
+ this.maps = props.maps;
76
+ }
77
+
78
+ /** @internal */
79
+ public static create(props: ClassProperties<Metadata>): Metadata {
80
+ return new Metadata(props);
81
+ }
82
+
83
+ /** @internal */
84
+ public static initialize(parentResolved: boolean = false): Metadata {
85
+ const meta: Metadata = Metadata.create({
86
+ any: false,
87
+ nullable: false,
88
+ required: true,
89
+ optional: false,
90
+
91
+ escaped: null,
92
+ constants: [],
93
+ atomics: [],
94
+ templates: [],
95
+ arrays: [],
96
+ tuples: [],
97
+ objects: [],
98
+ aliases: [],
99
+ functions: [],
100
+
101
+ rest: null,
102
+ natives: [],
103
+ sets: [],
104
+ maps: [],
105
+ });
106
+ meta.parent_resolved_ = parentResolved;
107
+ return meta;
108
+ }
109
+
110
+ public toJSON(): IMetadata {
111
+ return {
112
+ any: this.any,
113
+ required: this.required,
114
+ optional: this.optional,
115
+ nullable: this.nullable,
116
+ functions: this.functions.map((f) => f.toJSON()),
117
+
118
+ atomics: this.atomics.map((a) => a.toJSON()),
119
+ constants: this.constants.map((c) => c.toJSON()),
120
+ templates: this.templates.map((tpl) => tpl.toJSON()),
121
+ escaped: this.escaped ? this.escaped.toJSON() : null,
122
+
123
+ rest: this.rest ? this.rest.toJSON() : null,
124
+ arrays: this.arrays.map((array) => array.toJSON()),
125
+ tuples: this.tuples.map((tuple) => tuple.toJSON()),
126
+ objects: this.objects.map((obj) => obj.toJSON()),
127
+ aliases: this.aliases.map((alias) => alias.toJSON()),
128
+
129
+ natives: this.natives.map((native) => native.toJSON()),
130
+ sets: this.sets.map((set) => set.toJSON()),
131
+ maps: this.maps.map((map) => map.toJSON()),
132
+ };
133
+ }
134
+
135
+ public static from(meta: IMetadata, dict: IMetadataDictionary): Metadata {
136
+ return Metadata.create({
137
+ any: meta.any,
138
+ required: meta.required,
139
+ optional: meta.optional,
140
+ nullable: meta.nullable,
141
+ functions: meta.functions.map((f) => MetadataFunction.from(f, dict)),
142
+
143
+ constants: meta.constants.map(MetadataConstant.from),
144
+ atomics: meta.atomics.map(MetadataAtomic.from),
145
+ templates: meta.templates.map((tpl) => MetadataTemplate.from(tpl, dict)),
146
+ escaped: meta.escaped ? MetadataEscaped.from(meta.escaped, dict) : null,
147
+
148
+ rest: meta.rest ? this.from(meta.rest, dict) : null,
149
+ arrays: meta.arrays.map((ref) => {
150
+ const type = dict.arrays.get(ref.name);
151
+ if (type === undefined)
152
+ throw new RangeError(
153
+ `Error on Metadata.from(): failed to find array "${ref.name}".`,
154
+ );
155
+ return MetadataArray.create({
156
+ type,
157
+ tags: ref.tags.map((row) => row.slice()),
158
+ });
159
+ }),
160
+ tuples: meta.tuples.map((t) => {
161
+ const type = dict.tuples.get(t.name);
162
+ if (type === undefined)
163
+ throw new RangeError(
164
+ `Error on Metadata.from(): failed to find tuple "${t.name}".`,
165
+ );
166
+ return MetadataTuple.create({
167
+ type,
168
+ tags: t.tags.map((r) => r.slice()),
169
+ });
170
+ }),
171
+ objects: meta.objects.map((obj) => {
172
+ const found = dict.objects.get(obj.name);
173
+ if (found === undefined)
174
+ throw new RangeError(
175
+ `Error on Metadata.from(): failed to find object "${name}".`,
176
+ );
177
+ return MetadataObject.create({
178
+ type: found,
179
+ tags: obj.tags.map((r) => r.slice()),
180
+ });
181
+ }),
182
+ aliases: meta.aliases.map((alias) => {
183
+ const type = dict.aliases.get(alias.name);
184
+ if (type === undefined)
185
+ throw new RangeError(
186
+ `Error on Metadata.from(): failed to find alias "${alias}".`,
187
+ );
188
+ return MetadataAlias.create({
189
+ type,
190
+ tags: alias.tags.map((r) => r.slice()),
191
+ });
192
+ }),
193
+ natives: meta.natives.map((native) => MetadataNative.create(native)),
194
+ sets: meta.sets.map((set) =>
195
+ MetadataSet.create({
196
+ value: Metadata.from(set.value, dict),
197
+ tags: set.tags.map((r) => r.slice()),
198
+ }),
199
+ ),
200
+ maps: meta.maps.map((map) =>
201
+ MetadataMap.create({
202
+ key: this.from(map.key, dict),
203
+ value: this.from(map.value, dict),
204
+ tags: map.tags.map((r) => r.slice()),
205
+ }),
206
+ ),
207
+ });
208
+ }
209
+
210
+ /* -----------------------------------------------------------
211
+ ACCESSORS
212
+ ----------------------------------------------------------- */
213
+ public getName(): string {
214
+ return (this.name_ ??= getName(this));
215
+ }
216
+
217
+ public empty(): boolean {
218
+ return this.bucket() === 0 || this.size() === 0;
219
+ }
220
+
221
+ public size(): number {
222
+ return (
223
+ (this.any ? 1 : 0) +
224
+ (this.escaped ? 1 : 0) +
225
+ (this.rest ? this.rest.size() : 0) +
226
+ this.templates.length +
227
+ this.atomics.length +
228
+ this.constants.map((c) => c.values.length).reduce((x, y) => x + y, 0) +
229
+ this.arrays.length +
230
+ this.tuples.length +
231
+ this.natives.length +
232
+ this.maps.length +
233
+ this.sets.length +
234
+ this.objects.length +
235
+ this.functions.length +
236
+ this.aliases.length
237
+ );
238
+ }
239
+
240
+ public bucket(): number {
241
+ return (
242
+ (this.any ? 1 : 0) +
243
+ (this.escaped ? 1 : 0) +
244
+ (this.templates.length ? 1 : 0) +
245
+ (this.atomics.length ? 1 : 0) +
246
+ (this.constants.length ? 1 : 0) +
247
+ (this.rest ? this.rest.size() : 0) +
248
+ (this.arrays.length ? 1 : 0) +
249
+ (this.tuples.length ? 1 : 0) +
250
+ (this.natives.length ? 1 : 0) +
251
+ (this.sets.length ? 1 : 0) +
252
+ (this.maps.length ? 1 : 0) +
253
+ (this.objects.length ? 1 : 0) +
254
+ (this.functions.length ? 1 : 0) +
255
+ (this.aliases.length ? 1 : 0)
256
+ );
257
+ }
258
+
259
+ /** @internal */
260
+ public isSequence(): boolean {
261
+ return (this.is_sequence_ ??= (() => {
262
+ const exists = (tags: IMetadataTypeTag[][]): boolean =>
263
+ tags.some((row) => {
264
+ const sequence = row.find(
265
+ (t) =>
266
+ t.kind === "sequence" &&
267
+ typeof (t.schema as any)?.["x-protobuf-sequence"] === "number",
268
+ );
269
+ if (sequence === undefined) return false;
270
+ const value: number = Number(
271
+ (sequence.schema as any)["x-protobuf-sequence"],
272
+ );
273
+ return !Number.isNaN(value);
274
+ });
275
+ return (
276
+ this.atomics.some((atomic) => exists(atomic.tags)) &&
277
+ this.constants.some((c) =>
278
+ c.values.some((v) => exists(v.tags ?? [])),
279
+ ) &&
280
+ this.templates.some((tpl) => exists(tpl.tags)) &&
281
+ this.arrays.some((array) => exists(array.tags)) &&
282
+ this.objects.some((object) => exists(object.tags)) &&
283
+ this.natives.some(
284
+ (native) => native.name === "Uint8Array" && exists(native.tags),
285
+ )
286
+ );
287
+ })());
288
+ }
289
+
290
+ public isConstant(): boolean {
291
+ return this.bucket() === (this.constants.length ? 1 : 0);
292
+ }
293
+
294
+ public isRequired(): boolean {
295
+ return this.required === true && this.optional === false;
296
+ }
297
+
298
+ /** @internal */
299
+ public isUnionBucket(): boolean {
300
+ const size: number = this.bucket();
301
+ const emended: number =
302
+ !!this.atomics.length && !!this.constants.length ? size - 1 : size;
303
+ return emended > 1;
304
+ }
305
+
306
+ /** @internal */
307
+ public getSoleLiteral(): string | null {
308
+ if (
309
+ this.size() === 1 &&
310
+ this.constants.length === 1 &&
311
+ this.constants[0]!.type === "string" &&
312
+ this.constants[0]!.values.length === 1
313
+ )
314
+ return this.constants[0]!.values[0]!.value as string;
315
+ else return null;
316
+ }
317
+
318
+ public isSoleLiteral(): boolean {
319
+ return this.getSoleLiteral() !== null;
320
+ }
321
+
322
+ /** @internal */
323
+ public isParentResolved(): boolean {
324
+ return this.parent_resolved_;
325
+ }
326
+ }
327
+ export namespace Metadata {
328
+ export const intersects = (x: Metadata, y: Metadata): boolean => {
329
+ // CHECK ANY & OPTIONAL
330
+ if (x.any || y.any) return true;
331
+ if (x.isRequired() === false && false === y.isRequired()) return true;
332
+ if (x.nullable === true && true === y.nullable) return true;
333
+ if (!!x.functions.length && !!y.functions.length === true) return true;
334
+
335
+ //----
336
+ // INSTANCES
337
+ //----
338
+ // ARRAYS
339
+ if (x.arrays.length && y.arrays.length) return true;
340
+ if (x.tuples.length && y.tuples.length) return true;
341
+ if (x.objects.length && y.objects.length) return true;
342
+ if (x.aliases.length && y.aliases.length) return true;
343
+
344
+ // NATIVES
345
+ if (x.natives.length && y.natives.length)
346
+ if (x.natives.some((xn) => y.natives.some((yn) => xn === yn)))
347
+ return true;
348
+
349
+ // ESCAPED
350
+ if (x.escaped && y.escaped)
351
+ return (
352
+ intersects(x.escaped.original, y.escaped.original) ||
353
+ intersects(x.escaped.returns, y.escaped.returns)
354
+ );
355
+
356
+ //----
357
+ // VALUES
358
+ //----
359
+ // ATOMICS
360
+ for (const atomic of x.atomics) {
361
+ if (y.atomics.some((ya) => atomic.type === ya.type)) return true;
362
+ if (y.constants.some((yc) => atomic.type === yc.type)) return true;
363
+ }
364
+
365
+ // CONSTANTS
366
+ for (const constant of x.constants) {
367
+ const atomic: MetadataAtomic | undefined = y.atomics.find(
368
+ (elem) => elem.type === constant.type,
369
+ );
370
+ if (atomic !== undefined) return true;
371
+
372
+ const opposite: MetadataConstant | undefined = y.constants.find(
373
+ (elem) => elem.type === constant.type,
374
+ );
375
+ if (opposite === undefined) continue;
376
+
377
+ const values: Set<any> = new Set([
378
+ ...constant.values.map((e) => e.value),
379
+ ...opposite.values.map((e) => e.value),
380
+ ]);
381
+ if (values.size !== constant.values.length + opposite.values.length)
382
+ return true;
383
+ }
384
+
385
+ // TEMPLATES
386
+ if (!!x.templates.length && y.atomics.some((ya) => ya.type === "string"))
387
+ return true;
388
+ else if (
389
+ !!y.templates.length &&
390
+ x.atomics.some((xa) => xa.type === "string")
391
+ )
392
+ return true;
393
+ return false;
394
+ };
395
+
396
+ export const covers = (
397
+ x: Metadata,
398
+ y: Metadata,
399
+ level: number = 0,
400
+ escaped: boolean = false,
401
+ ): boolean => {
402
+ // CHECK ANY
403
+ if (x === y) return false;
404
+ else if (x.any) return true;
405
+ else if (y.any) return false;
406
+
407
+ if (escaped === false) {
408
+ if (x.escaped === null && y.escaped !== null) return false;
409
+ else if (
410
+ x.escaped !== null &&
411
+ y.escaped !== null &&
412
+ (!covers(x.escaped.original, y.escaped.original, level, true) ||
413
+ !covers(x.escaped.returns, y.escaped.returns, level, true))
414
+ )
415
+ return false;
416
+ }
417
+
418
+ //----
419
+ // INSTANCES
420
+ //----
421
+ if (level === 0) {
422
+ // ARRAYS
423
+ for (const ya of y.arrays)
424
+ if (
425
+ !x.arrays.some((xa) =>
426
+ covers(xa.type.value, ya.type.value, level + 1),
427
+ )
428
+ ) {
429
+ return false;
430
+ }
431
+
432
+ // TUPLES
433
+ for (const yt of y.tuples)
434
+ if (
435
+ yt.type.elements.length !== 0 &&
436
+ x.tuples.some(
437
+ (xt) =>
438
+ xt.type.elements.length >= yt.type.elements.length &&
439
+ xt.type.elements
440
+ .slice(yt.type.elements.length)
441
+ .every((xv, i) => covers(xv, yt.type.elements[i]!, level + 1)),
442
+ ) === false
443
+ )
444
+ return false;
445
+ }
446
+
447
+ // OBJECTS
448
+ for (const yo of y.objects)
449
+ if (
450
+ x.objects.some((xo) => MetadataObjectType.covers(xo.type, yo.type)) ===
451
+ false
452
+ )
453
+ return false;
454
+
455
+ // ALIASES
456
+ for (const yd of y.aliases)
457
+ if (x.aliases.some((xd) => xd.type.name === yd.type.name) === false)
458
+ return false;
459
+
460
+ // NATIVES
461
+ for (const yn of y.natives)
462
+ if (x.natives.some((xn) => xn === yn) === false) return false;
463
+
464
+ // SETS
465
+ for (const ys of y.sets)
466
+ if (x.sets.some((xs) => covers(xs.value, ys.value)) === false)
467
+ return false;
468
+
469
+ //----
470
+ // VALUES
471
+ //----
472
+ // ATOMICS
473
+ if (
474
+ y.atomics.some(
475
+ (ya) => x.atomics.some((xa) => xa.type === ya.type) === false,
476
+ )
477
+ )
478
+ return false;
479
+
480
+ // CONSTANTS
481
+ for (const yc of y.constants) {
482
+ if (x.atomics.some((atom) => yc.type === atom.type)) continue;
483
+ const xc: MetadataConstant | undefined = x.constants.find(
484
+ (elem) => elem.type === yc.type,
485
+ );
486
+ if (xc === undefined) return false;
487
+ else if (
488
+ (yc.values.map((e) => e.value) as number[]).some(
489
+ (yv) => xc.values.includes(yv as never) === false,
490
+ )
491
+ )
492
+ return false;
493
+ }
494
+
495
+ // FUNCTIONAL
496
+ if (!!x.functions.length === false && !!y.functions.length) return false;
497
+
498
+ // SUCCESS
499
+ return true;
500
+ };
501
+
502
+ /** @internal */
503
+ export const merge = (x: Metadata, y: Metadata): Metadata => {
504
+ const output: Metadata = Metadata.create({
505
+ any: x.any || y.any,
506
+ nullable: x.nullable || y.nullable,
507
+ required: x.required && y.required,
508
+ optional: x.optional || y.optional,
509
+ functions: x.functions.length ? x.functions : y.functions, // @todo
510
+ escaped:
511
+ x.escaped !== null && y.escaped !== null
512
+ ? MetadataEscaped.create({
513
+ original: merge(x.escaped.original, y.escaped.original),
514
+ returns: merge(x.escaped.returns, y.escaped.returns),
515
+ })
516
+ : (x.escaped ?? y.escaped),
517
+ atomics: mergeTaggedTypes({
518
+ container: x.atomics,
519
+ equals: (x, y) => x.type === y.type,
520
+ getter: (x) => x.tags,
521
+ })(y.atomics),
522
+ constants: [...x.constants],
523
+ templates: x.templates.slice(),
524
+ rest:
525
+ x.rest !== null && y.rest !== null
526
+ ? merge(x.rest, y.rest)
527
+ : (x.rest ?? y.rest),
528
+ arrays: mergeTaggedTypes({
529
+ container: x.arrays,
530
+ equals: (x, y) => x.type.name === y.type.name,
531
+ getter: (x) => x.tags,
532
+ })(y.arrays),
533
+ tuples: mergeTaggedTypes({
534
+ container: x.tuples,
535
+ equals: (x, y) => x.type.name === y.type.name,
536
+ getter: (x) => x.tags,
537
+ })(y.tuples),
538
+ objects: mergeTaggedTypes({
539
+ container: x.objects,
540
+ equals: (x, y) => x.type.name === y.type.name,
541
+ getter: (x) => x.tags,
542
+ })(y.objects),
543
+ aliases: mergeTaggedTypes({
544
+ container: x.aliases,
545
+ equals: (x, y) => x.type.name === y.type.name,
546
+ getter: (x) => x.tags,
547
+ })(y.aliases),
548
+ natives: mergeTaggedTypes({
549
+ container: x.natives,
550
+ equals: (x, y) => x.name === y.name,
551
+ getter: (x) => x.tags,
552
+ })(y.natives),
553
+ sets: mergeTaggedTypes({
554
+ container: x.sets,
555
+ equals: (x, y) => x.value.getName() === y.value.getName(),
556
+ getter: (x) => x.tags,
557
+ })(y.sets),
558
+ maps: mergeTaggedTypes({
559
+ container: x.maps,
560
+ equals: (x, y) =>
561
+ x.key.getName() === y.key.getName() &&
562
+ x.value.getName() === y.value.getName(),
563
+ getter: (x) => x.tags,
564
+ })(y.maps),
565
+ });
566
+ for (const constant of y.constants) {
567
+ const target: MetadataConstant = ArrayUtil.take(
568
+ output.constants,
569
+ (elem) => elem.type === constant.type,
570
+ () =>
571
+ MetadataConstant.create({
572
+ type: constant.type,
573
+ values: [],
574
+ }),
575
+ );
576
+ for (const value of constant.values)
577
+ ArrayUtil.add(target.values, value, (a, b) => a.value === b.value);
578
+ }
579
+ return output;
580
+ };
581
+
582
+ /** @internal */
583
+ export const unalias = (w: Metadata) => {
584
+ const visited: Set<Metadata> = new Set();
585
+ while (
586
+ w.size() === 1 &&
587
+ w.nullable === false &&
588
+ w.isRequired() === true &&
589
+ w.aliases.length === 1
590
+ ) {
591
+ if (visited.has(w)) break;
592
+ w = w.aliases[0]!.type.value;
593
+ visited.add(w);
594
+ }
595
+ return w;
596
+ };
597
+ }
598
+
599
+ const getName = (metadata: Metadata): string => {
600
+ if (metadata.any === true) return "any";
601
+
602
+ const elements: string[] = [];
603
+
604
+ // OPTIONAL
605
+ if (metadata.nullable === true) elements.push("null");
606
+ if (metadata.isRequired() === false) elements.push("undefined");
607
+
608
+ // ATOMIC
609
+ for (const atom of metadata.atomics) {
610
+ elements.push(atom.getName());
611
+ }
612
+ for (const constant of metadata.constants)
613
+ for (const value of constant.values) elements.push(value.getName());
614
+ for (const template of metadata.templates) elements.push(template.getName());
615
+
616
+ // NATIVES
617
+ for (const native of metadata.natives) elements.push(native.getName());
618
+ for (const set of metadata.sets) elements.push(set.getName());
619
+ for (const map of metadata.maps) elements.push(map.getName());
620
+
621
+ // INSTANCES
622
+ if (metadata.rest !== null) elements.push(`...${metadata.rest.getName()}`);
623
+ for (const tuple of metadata.tuples) elements.push(tuple.type.name);
624
+ for (const array of metadata.arrays) elements.push(array.getName());
625
+ for (const object of metadata.objects) elements.push(object.getName());
626
+ for (const alias of metadata.aliases) elements.push(alias.getName());
627
+ if (metadata.escaped !== null) elements.push(metadata.escaped.getName());
628
+
629
+ // RETURNS
630
+ if (elements.length === 0) return "unknown";
631
+ else if (elements.length === 1) return elements[0]!;
632
+
633
+ elements.sort();
634
+ return `(${elements.join(" | ")})`;
635
+ };
636
+
637
+ const mergeTaggedTypes =
638
+ <T>(props: {
639
+ container: T[];
640
+ equals: (x: T, y: T) => boolean;
641
+ getter: (x: T) => IMetadataTypeTag[][];
642
+ }) =>
643
+ (opposite: T[]) => {
644
+ const output: T[] = [...props.container];
645
+ for (const elem of opposite) {
646
+ const equal = props.container.find((x) => props.equals(x, elem));
647
+ if (equal === undefined) {
648
+ output.push(elem);
649
+ continue;
650
+ }
651
+
652
+ const matrix: string[][] = props
653
+ .getter(equal)
654
+ .map((tags) => tags.map((t) => t.name))
655
+ .sort();
656
+ for (const tags of props.getter(elem)) {
657
+ const names: string[] = tags.map((t) => t.name).sort();
658
+ if (
659
+ matrix.some(
660
+ (m) =>
661
+ m.length === names.length && m.every((s, i) => s === names[i]),
662
+ )
663
+ )
664
+ continue;
665
+ props.getter(equal).push(tags);
666
+ }
667
+ }
668
+ return output;
669
+ };