typia 5.2.4 → 5.2.5
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/lib/factories/internal/metadata/emplace_metadata_object.js +24 -0
- package/lib/factories/internal/metadata/emplace_metadata_object.js.map +1 -1
- package/lib/functional/$stoll.d.ts +1 -0
- package/lib/functional/$stoll.js +14 -0
- package/lib/functional/$stoll.js.map +1 -0
- package/lib/functional/Namespace.js +4 -10
- package/lib/functional/Namespace.js.map +1 -1
- package/lib/programmers/AssertProgrammer.js +71 -63
- package/lib/programmers/AssertProgrammer.js.map +1 -1
- package/lib/programmers/IsProgrammer.d.ts +2 -2
- package/lib/programmers/IsProgrammer.js +66 -64
- package/lib/programmers/IsProgrammer.js.map +1 -1
- package/lib/programmers/ValidateProgrammer.js +50 -46
- package/lib/programmers/ValidateProgrammer.js.map +1 -1
- package/lib/programmers/internal/check_dynamic_key.d.ts +5 -0
- package/lib/programmers/internal/check_dynamic_key.js +158 -0
- package/lib/programmers/internal/check_dynamic_key.js.map +1 -0
- package/lib/programmers/internal/check_dynamic_properties.js +69 -61
- package/lib/programmers/internal/check_dynamic_properties.js.map +1 -1
- package/lib/programmers/internal/check_object.js +11 -9
- package/lib/programmers/internal/check_object.js.map +1 -1
- package/lib/programmers/json/JsonStringifyProgrammer.js +1 -1
- package/lib/programmers/json/JsonStringifyProgrammer.js.map +1 -1
- package/lib/programmers/misc/MiscCloneProgrammer.js +1 -1
- package/lib/programmers/misc/MiscCloneProgrammer.js.map +1 -1
- package/lib/programmers/misc/MiscPruneProgrammer.js +1 -1
- package/lib/programmers/misc/MiscPruneProgrammer.js.map +1 -1
- package/lib/programmers/notations/NotationGeneralProgrammer.js +1 -1
- package/lib/programmers/notations/NotationGeneralProgrammer.js.map +1 -1
- package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js +1 -1
- package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js.map +1 -1
- package/package.json +2 -2
- package/src/CamelCase.ts +116 -116
- package/src/PascalCase.ts +116 -116
- package/src/Primitive.ts +135 -135
- package/src/Resolved.ts +116 -116
- package/src/SnakeCase.ts +156 -156
- package/src/executable/TypiaSetupWizard.ts +142 -142
- package/src/executable/setup/CommandExecutor.ts +8 -8
- package/src/factories/JsonMetadataFactory.ts +50 -50
- package/src/factories/MetadataCollection.ts +282 -282
- package/src/factories/internal/metadata/emplace_metadata_object.ts +28 -0
- package/src/functional/$convention.ts +40 -40
- package/src/functional/$stoll.ts +8 -0
- package/src/functional/Namespace.ts +168 -164
- package/src/module.ts +662 -662
- package/src/notations.ts +855 -855
- package/src/programmers/AssertProgrammer.ts +36 -31
- package/src/programmers/IsProgrammer.ts +13 -7
- package/src/programmers/ValidateProgrammer.ts +34 -31
- package/src/programmers/helpers/AtomicPredicator.ts +31 -31
- package/src/programmers/helpers/NotationJoiner.ts +146 -146
- package/src/programmers/internal/check_dynamic_key.ts +178 -0
- package/src/programmers/internal/check_dynamic_properties.ts +19 -12
- package/src/programmers/internal/check_object.ts +8 -1
- package/src/programmers/json/JsonStringifyProgrammer.ts +1 -1
- package/src/programmers/misc/MiscCloneProgrammer.ts +790 -790
- package/src/programmers/misc/MiscPruneProgrammer.ts +552 -552
- package/src/programmers/notations/NotationAssertGeneralProgrammer.ts +72 -72
- package/src/programmers/notations/NotationGeneralProgrammer.ts +720 -720
- package/src/programmers/notations/NotationIsGeneralProgrammer.ts +79 -79
- package/src/programmers/notations/NotationValidateGeneralProgrammer.ts +88 -88
- package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +1 -1
- package/src/transformers/CallExpressionTransformer.ts +380 -380
- package/src/transformers/features/json/JsonAssertParseTransformer.ts +10 -10
- package/src/transformers/features/json/JsonAssertStringifyTransformer.ts +10 -10
- package/src/transformers/features/json/JsonCreateAssertParseTransformer.ts +12 -12
- package/src/transformers/features/json/JsonCreateAssertStringifyTransformer.ts +12 -12
- package/src/transformers/features/json/JsonCreateIsParseTransformer.ts +9 -9
- package/src/transformers/features/json/JsonCreateIsStringifyTransformer.ts +12 -12
- package/src/transformers/features/json/JsonCreateStringifyTransformer.ts +9 -9
- package/src/transformers/features/json/JsonCreateValidateParseTransformer.ts +12 -12
- package/src/transformers/features/json/JsonCreateValidateStringifyProgrammer.ts +12 -12
- package/src/transformers/features/json/JsonIsParseTransformer.ts +9 -9
- package/src/transformers/features/json/JsonIsStringifyTransformer.ts +10 -10
- package/src/transformers/features/json/JsonStringifyTransformer.ts +9 -9
- package/src/transformers/features/json/JsonValidateParseTransformer.ts +10 -10
- package/src/transformers/features/json/JsonValidateStringifyTransformer.ts +10 -10
- package/src/transformers/features/misc/MiscAssertCloneTransformer.ts +10 -10
- package/src/transformers/features/misc/MiscAssertPruneTransformer.ts +10 -10
- package/src/transformers/features/misc/MiscCloneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscCreateAssertCloneTransformer.ts +12 -12
- package/src/transformers/features/misc/MiscCreateAssertPruneTransformer.ts +12 -12
- package/src/transformers/features/misc/MiscCreateCloneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscCreateIsCloneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscCreateIsPruneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscCreatePruneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscCreateValidateCloneTransformer.ts +12 -12
- package/src/transformers/features/misc/MiscCreateValidatePruneTransformer.ts +12 -12
- package/src/transformers/features/misc/MiscIsCloneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscIsPruneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscPruneTransformer.ts +9 -9
- package/src/transformers/features/misc/MiscValidateCloneTransformer.ts +10 -10
- package/src/transformers/features/misc/MiscValidatePruneTransformer.ts +10 -10
- package/src/transformers/features/notations/NotationAssertGeneralTransformer.ts +15 -15
- package/src/transformers/features/notations/NotationCreateAssertGeneralTransformer.ts +15 -15
- package/src/transformers/features/notations/NotationCreateGeneralTransformer.ts +15 -15
- package/src/transformers/features/notations/NotationCreateIsGeneralTransformer.ts +15 -15
- package/src/transformers/features/notations/NotationCreateValidateGeneralTransformer.ts +17 -17
- package/src/transformers/features/notations/NotationGeneralTransformer.ts +11 -11
- package/src/transformers/features/notations/NotationIsGeneralTransformer.ts +15 -15
- package/src/transformers/features/notations/NotationValidateGeneralTransformer.ts +17 -17
- package/src/transformers/features/protobuf/ProtobufCreateAssertDecodeTransformer.ts +12 -12
- package/src/transformers/features/protobuf/ProtobufCreateAssertEncodeTransformer.ts +12 -12
- package/src/transformers/features/protobuf/ProtobufCreateDecodeTransformer.ts +9 -9
- package/src/transformers/features/protobuf/ProtobufCreateEncodeTransformer.ts +9 -9
- package/src/transformers/features/protobuf/ProtobufCreateIsDecodeTransformer.ts +12 -12
- package/src/transformers/features/protobuf/ProtobufCreateIsEncodeTransformer.ts +12 -12
- package/src/transformers/features/protobuf/ProtobufCreateValidateDecodeTransformer.ts +12 -12
- package/src/transformers/features/protobuf/ProtobufCreateValidateEncodeTransformer.ts +12 -12
- package/src/utils/NamingConvention.ts +91 -91
- package/src/utils/StringUtil.ts +4 -4
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { Metadata } from "../schemas/metadata/Metadata";
|
|
4
|
-
|
|
5
|
-
import { AtomicPredicator } from "../programmers/helpers/AtomicPredicator";
|
|
6
|
-
|
|
7
|
-
import { TransformerError } from "../transformers/TransformerError";
|
|
8
|
-
|
|
9
|
-
import { MetadataCollection } from "./MetadataCollection";
|
|
10
|
-
import { MetadataFactory } from "./MetadataFactory";
|
|
11
|
-
|
|
12
|
-
export namespace JsonMetadataFactory {
|
|
13
|
-
export const analyze =
|
|
14
|
-
(method: string) =>
|
|
15
|
-
(checker: ts.TypeChecker) =>
|
|
16
|
-
(type: ts.Type): [MetadataCollection, Metadata] => {
|
|
17
|
-
const collection = new MetadataCollection();
|
|
18
|
-
const result = MetadataFactory.analyze(checker)({
|
|
19
|
-
escape: true,
|
|
20
|
-
constant: true,
|
|
21
|
-
absorb: true,
|
|
22
|
-
validate,
|
|
23
|
-
})(collection)(type);
|
|
24
|
-
if (result.success === false)
|
|
25
|
-
throw TransformerError.from(method)(result.errors);
|
|
26
|
-
return [collection, result.data];
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export const validate = (meta: Metadata) => {
|
|
30
|
-
const output: string[] = [];
|
|
31
|
-
if (
|
|
32
|
-
meta.atomics.some((a) => a.type === "bigint") ||
|
|
33
|
-
meta.constants.some((c) => c.type === "bigint")
|
|
34
|
-
)
|
|
35
|
-
output.push("JSON does not support bigint type.");
|
|
36
|
-
if (
|
|
37
|
-
meta.tuples.some((t) =>
|
|
38
|
-
t.type.elements.some((e) => e.isRequired() === false),
|
|
39
|
-
) ||
|
|
40
|
-
meta.arrays.some((a) => a.type.value.isRequired() === false)
|
|
41
|
-
)
|
|
42
|
-
output.push("JSON does not support undefined type in array.");
|
|
43
|
-
if (meta.maps.length) output.push("JSON does not support Map type.");
|
|
44
|
-
if (meta.sets.length) output.push("JSON does not support Set type.");
|
|
45
|
-
for (const native of meta.natives)
|
|
46
|
-
if (AtomicPredicator.native(native) === false && native !== "Date")
|
|
47
|
-
output.push(`JSON does not support ${native} type.`);
|
|
48
|
-
return output;
|
|
49
|
-
};
|
|
50
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { Metadata } from "../schemas/metadata/Metadata";
|
|
4
|
+
|
|
5
|
+
import { AtomicPredicator } from "../programmers/helpers/AtomicPredicator";
|
|
6
|
+
|
|
7
|
+
import { TransformerError } from "../transformers/TransformerError";
|
|
8
|
+
|
|
9
|
+
import { MetadataCollection } from "./MetadataCollection";
|
|
10
|
+
import { MetadataFactory } from "./MetadataFactory";
|
|
11
|
+
|
|
12
|
+
export namespace JsonMetadataFactory {
|
|
13
|
+
export const analyze =
|
|
14
|
+
(method: string) =>
|
|
15
|
+
(checker: ts.TypeChecker) =>
|
|
16
|
+
(type: ts.Type): [MetadataCollection, Metadata] => {
|
|
17
|
+
const collection = new MetadataCollection();
|
|
18
|
+
const result = MetadataFactory.analyze(checker)({
|
|
19
|
+
escape: true,
|
|
20
|
+
constant: true,
|
|
21
|
+
absorb: true,
|
|
22
|
+
validate,
|
|
23
|
+
})(collection)(type);
|
|
24
|
+
if (result.success === false)
|
|
25
|
+
throw TransformerError.from(method)(result.errors);
|
|
26
|
+
return [collection, result.data];
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const validate = (meta: Metadata) => {
|
|
30
|
+
const output: string[] = [];
|
|
31
|
+
if (
|
|
32
|
+
meta.atomics.some((a) => a.type === "bigint") ||
|
|
33
|
+
meta.constants.some((c) => c.type === "bigint")
|
|
34
|
+
)
|
|
35
|
+
output.push("JSON does not support bigint type.");
|
|
36
|
+
if (
|
|
37
|
+
meta.tuples.some((t) =>
|
|
38
|
+
t.type.elements.some((e) => e.isRequired() === false),
|
|
39
|
+
) ||
|
|
40
|
+
meta.arrays.some((a) => a.type.value.isRequired() === false)
|
|
41
|
+
)
|
|
42
|
+
output.push("JSON does not support undefined type in array.");
|
|
43
|
+
if (meta.maps.length) output.push("JSON does not support Map type.");
|
|
44
|
+
if (meta.sets.length) output.push("JSON does not support Set type.");
|
|
45
|
+
for (const native of meta.natives)
|
|
46
|
+
if (AtomicPredicator.native(native) === false && native !== "Date")
|
|
47
|
+
output.push(`JSON does not support ${native} type.`);
|
|
48
|
+
return output;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -1,282 +1,282 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { IMetadataCollection } from "../schemas/metadata/IMetadataCollection";
|
|
4
|
-
import { Metadata } from "../schemas/metadata/Metadata";
|
|
5
|
-
import { MetadataAlias } from "../schemas/metadata/MetadataAlias";
|
|
6
|
-
import { MetadataArrayType } from "../schemas/metadata/MetadataArrayType";
|
|
7
|
-
import { MetadataObject } from "../schemas/metadata/MetadataObject";
|
|
8
|
-
import { MetadataTupleType } from "../schemas/metadata/MetadataTupleType";
|
|
9
|
-
|
|
10
|
-
import { Writable } from "../typings/Writable";
|
|
11
|
-
|
|
12
|
-
import { MapUtil } from "../utils/MapUtil";
|
|
13
|
-
|
|
14
|
-
import { CommentFactory } from "./CommentFactory";
|
|
15
|
-
import { TypeFactory } from "./TypeFactory";
|
|
16
|
-
|
|
17
|
-
export class MetadataCollection {
|
|
18
|
-
private readonly objects_: Map<ts.Type, MetadataObject>;
|
|
19
|
-
private readonly object_unions_: Map<string, MetadataObject[]>;
|
|
20
|
-
private readonly aliases_: Map<ts.Type, MetadataAlias>;
|
|
21
|
-
private readonly arrays_: Map<ts.Type, MetadataArrayType>;
|
|
22
|
-
private readonly tuples_: Map<ts.Type, MetadataTupleType>;
|
|
23
|
-
|
|
24
|
-
private readonly names_: Map<string, Map<ts.Type, string>>;
|
|
25
|
-
private object_index_: number;
|
|
26
|
-
private recursive_array_index_: number;
|
|
27
|
-
private recursive_tuple_index_: number;
|
|
28
|
-
|
|
29
|
-
public constructor(
|
|
30
|
-
private readonly options?: Partial<MetadataCollection.IOptions>,
|
|
31
|
-
) {
|
|
32
|
-
this.objects_ = new Map();
|
|
33
|
-
this.object_unions_ = new Map();
|
|
34
|
-
this.aliases_ = new Map();
|
|
35
|
-
this.arrays_ = new Map();
|
|
36
|
-
this.tuples_ = new Map();
|
|
37
|
-
|
|
38
|
-
this.names_ = new Map();
|
|
39
|
-
this.object_index_ = 0;
|
|
40
|
-
this.recursive_array_index_ = 0;
|
|
41
|
-
this.recursive_tuple_index_ = 0;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/* -----------------------------------------------------------
|
|
45
|
-
ACCESSORS
|
|
46
|
-
----------------------------------------------------------- */
|
|
47
|
-
public aliases(): MetadataAlias[] {
|
|
48
|
-
return [...this.aliases_.values()];
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
public objects(): MetadataObject[] {
|
|
52
|
-
return [...this.objects_.values()];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public unions(): MetadataObject[][] {
|
|
56
|
-
return [...this.object_unions_.values()];
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public arrays(): MetadataArrayType[] {
|
|
60
|
-
return [...this.arrays_.values()];
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public tuples(): MetadataTupleType[] {
|
|
64
|
-
return [...this.tuples_.values()];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
private getName(checker: ts.TypeChecker, type: ts.Type): string {
|
|
68
|
-
const name: string = (() => {
|
|
69
|
-
const str: string = TypeFactory.getFullName(checker)(type);
|
|
70
|
-
return this.options?.replace ? this.options.replace(str) : str;
|
|
71
|
-
})();
|
|
72
|
-
|
|
73
|
-
const duplicates: Map<ts.Type, string> = MapUtil.take(this.names_)(
|
|
74
|
-
name,
|
|
75
|
-
() => new Map(),
|
|
76
|
-
);
|
|
77
|
-
const oldbie: string | undefined = duplicates.get(type);
|
|
78
|
-
if (oldbie !== undefined) return oldbie;
|
|
79
|
-
|
|
80
|
-
const addicted: string = duplicates.size
|
|
81
|
-
? `${name}.o${duplicates.size}`
|
|
82
|
-
: name;
|
|
83
|
-
duplicates.set(type, addicted);
|
|
84
|
-
return addicted;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* @internal
|
|
89
|
-
*/
|
|
90
|
-
public getUnionIndex(meta: Metadata): number {
|
|
91
|
-
const key: string = meta.objects.map((obj) => obj.name).join(" | ");
|
|
92
|
-
MapUtil.take(this.object_unions_)(key, () => meta.objects);
|
|
93
|
-
return [...this.object_unions_.keys()].indexOf(key);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/* -----------------------------------------------------------
|
|
97
|
-
INSTANCES
|
|
98
|
-
----------------------------------------------------------- */
|
|
99
|
-
public emplace(
|
|
100
|
-
checker: ts.TypeChecker,
|
|
101
|
-
type: ts.Type,
|
|
102
|
-
): [MetadataObject, boolean] {
|
|
103
|
-
const oldbie = this.objects_.get(type);
|
|
104
|
-
if (oldbie !== undefined) return [oldbie, false];
|
|
105
|
-
|
|
106
|
-
// const displays = type.symbol.getDocumentationComment(checker);
|
|
107
|
-
// const tags = type.symbol.getJsDocTags(checker);
|
|
108
|
-
|
|
109
|
-
// console.log(
|
|
110
|
-
// ts.displayPartsToString(displays),
|
|
111
|
-
// tags.map((tag) => tag.name),
|
|
112
|
-
// tags.map((tag) => ts.displayPartsToString(tag.text)),
|
|
113
|
-
// );
|
|
114
|
-
|
|
115
|
-
const $id: string = this.getName(checker, type);
|
|
116
|
-
const obj: MetadataObject = MetadataObject.create({
|
|
117
|
-
name: $id,
|
|
118
|
-
properties: [],
|
|
119
|
-
description:
|
|
120
|
-
(type.aliasSymbol &&
|
|
121
|
-
CommentFactory.description(type.aliasSymbol)) ??
|
|
122
|
-
(type.symbol && CommentFactory.description(type.symbol)) ??
|
|
123
|
-
undefined,
|
|
124
|
-
jsDocTags:
|
|
125
|
-
type.aliasSymbol?.getJsDocTags() ??
|
|
126
|
-
type.symbol?.getJsDocTags() ??
|
|
127
|
-
[],
|
|
128
|
-
validated: false,
|
|
129
|
-
index: this.object_index_++,
|
|
130
|
-
recursive: null!,
|
|
131
|
-
nullables: [],
|
|
132
|
-
});
|
|
133
|
-
this.objects_.set(type, obj);
|
|
134
|
-
return [obj, true];
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
public emplaceAlias(
|
|
138
|
-
checker: ts.TypeChecker,
|
|
139
|
-
type: ts.Type,
|
|
140
|
-
symbol: ts.Symbol,
|
|
141
|
-
): [MetadataAlias, boolean, (meta: Metadata) => void] {
|
|
142
|
-
const oldbie = this.aliases_.get(type);
|
|
143
|
-
if (oldbie !== undefined) return [oldbie, false, () => {}];
|
|
144
|
-
|
|
145
|
-
const $id: string = this.getName(checker, type);
|
|
146
|
-
const alias: MetadataAlias = MetadataAlias.create({
|
|
147
|
-
name: $id,
|
|
148
|
-
value: null!,
|
|
149
|
-
description: CommentFactory.description(symbol) ?? null,
|
|
150
|
-
recursive: null!,
|
|
151
|
-
nullables: [],
|
|
152
|
-
jsDocTags: symbol.getJsDocTags() ?? [],
|
|
153
|
-
});
|
|
154
|
-
this.aliases_.set(type, alias);
|
|
155
|
-
return [alias, true, (meta) => (Writable(alias).value = meta)];
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
public emplaceArray(
|
|
159
|
-
checker: ts.TypeChecker,
|
|
160
|
-
type: ts.Type,
|
|
161
|
-
): [MetadataArrayType, boolean, (meta: Metadata) => void] {
|
|
162
|
-
const oldbie = this.arrays_.get(type);
|
|
163
|
-
if (oldbie !== undefined) return [oldbie, false, () => {}];
|
|
164
|
-
|
|
165
|
-
const $id = this.getName(checker, type);
|
|
166
|
-
const array: MetadataArrayType = MetadataArrayType.create({
|
|
167
|
-
name: $id,
|
|
168
|
-
value: null!,
|
|
169
|
-
index: null,
|
|
170
|
-
recursive: null!,
|
|
171
|
-
nullables: [],
|
|
172
|
-
});
|
|
173
|
-
this.arrays_.set(type, array);
|
|
174
|
-
return [array, true, (meta) => (Writable(array).value = meta)];
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
public emplaceTuple(
|
|
178
|
-
checker: ts.TypeChecker,
|
|
179
|
-
type: ts.TupleType,
|
|
180
|
-
): [MetadataTupleType, boolean, (elements: Metadata[]) => void] {
|
|
181
|
-
const oldbie = this.tuples_.get(type);
|
|
182
|
-
if (oldbie !== undefined) return [oldbie, false, () => {}];
|
|
183
|
-
|
|
184
|
-
const $id = this.getName(checker, type);
|
|
185
|
-
const tuple: MetadataTupleType = MetadataTupleType.create({
|
|
186
|
-
name: $id,
|
|
187
|
-
elements: null!,
|
|
188
|
-
index: null,
|
|
189
|
-
recursive: null!,
|
|
190
|
-
nullables: [],
|
|
191
|
-
});
|
|
192
|
-
this.tuples_.set(type, tuple);
|
|
193
|
-
return [
|
|
194
|
-
tuple,
|
|
195
|
-
true,
|
|
196
|
-
(elements) => (Writable(tuple).elements = elements),
|
|
197
|
-
];
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* @internal
|
|
202
|
-
*/
|
|
203
|
-
public setObjectRecursive(obj: MetadataObject, recursive: boolean): void {
|
|
204
|
-
Writable(obj).recursive = recursive;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* @internal
|
|
209
|
-
*/
|
|
210
|
-
public setAliasRecursive(alias: MetadataAlias, recursive: boolean): void {
|
|
211
|
-
Writable(alias).recursive = recursive;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* @internal
|
|
216
|
-
*/
|
|
217
|
-
public setArrayRecursive(
|
|
218
|
-
array: MetadataArrayType,
|
|
219
|
-
recursive: boolean,
|
|
220
|
-
): void {
|
|
221
|
-
Writable(array).recursive = recursive;
|
|
222
|
-
if (recursive) Writable(array).index = this.recursive_array_index_++;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
public setTupleRecursive(
|
|
226
|
-
tuple: MetadataTupleType,
|
|
227
|
-
recursive: boolean,
|
|
228
|
-
): void {
|
|
229
|
-
Writable(tuple).recursive = recursive;
|
|
230
|
-
if (recursive) Writable(tuple).index = this.recursive_tuple_index_++;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
public toJSON(): IMetadataCollection {
|
|
234
|
-
return {
|
|
235
|
-
objects: this.objects().map((o) => o.toJSON()),
|
|
236
|
-
aliases: this.aliases().map((d) => d.toJSON()),
|
|
237
|
-
arrays: [...this.arrays_.values()].map((a) => a.toJSON()),
|
|
238
|
-
tuples: [...this.tuples_.values()].map((t) => t.toJSON()),
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
export namespace MetadataCollection {
|
|
243
|
-
export interface IOptions {
|
|
244
|
-
replace?(str: string): string;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
export const replace = (str: string): string => {
|
|
248
|
-
let replaced: string = str;
|
|
249
|
-
for (const [before] of REPLACERS)
|
|
250
|
-
replaced = replaced.split(before).join("");
|
|
251
|
-
if (replaced.length !== 0) return replaced;
|
|
252
|
-
|
|
253
|
-
for (const [before, after] of REPLACERS)
|
|
254
|
-
str = str.split(before).join(after);
|
|
255
|
-
return str;
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
export const escape = (str: string): string => {
|
|
259
|
-
for (const [before, after] of REPLACERS)
|
|
260
|
-
if (after !== "") str = str.split(after).join(before);
|
|
261
|
-
return str;
|
|
262
|
-
};
|
|
263
|
-
}
|
|
264
|
-
const REPLACERS: [string, string][] = [
|
|
265
|
-
["$", "_dollar_"],
|
|
266
|
-
["&", "_and_"],
|
|
267
|
-
["|", "_or_"],
|
|
268
|
-
["{", "_blt_"],
|
|
269
|
-
["}", "_bgt_"],
|
|
270
|
-
["<", "_lt_"],
|
|
271
|
-
[">", "_gt_"],
|
|
272
|
-
["[", "_alt_"],
|
|
273
|
-
["]", "_agt_"],
|
|
274
|
-
[",", "_comma_"],
|
|
275
|
-
["`", "_backquote_"],
|
|
276
|
-
["'", "_singlequote_"],
|
|
277
|
-
['"', "_doublequote_"],
|
|
278
|
-
[" ", "_space_"],
|
|
279
|
-
["?", "_question_"],
|
|
280
|
-
[":", "_colon_"],
|
|
281
|
-
[";", "_semicolon_"],
|
|
282
|
-
];
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { IMetadataCollection } from "../schemas/metadata/IMetadataCollection";
|
|
4
|
+
import { Metadata } from "../schemas/metadata/Metadata";
|
|
5
|
+
import { MetadataAlias } from "../schemas/metadata/MetadataAlias";
|
|
6
|
+
import { MetadataArrayType } from "../schemas/metadata/MetadataArrayType";
|
|
7
|
+
import { MetadataObject } from "../schemas/metadata/MetadataObject";
|
|
8
|
+
import { MetadataTupleType } from "../schemas/metadata/MetadataTupleType";
|
|
9
|
+
|
|
10
|
+
import { Writable } from "../typings/Writable";
|
|
11
|
+
|
|
12
|
+
import { MapUtil } from "../utils/MapUtil";
|
|
13
|
+
|
|
14
|
+
import { CommentFactory } from "./CommentFactory";
|
|
15
|
+
import { TypeFactory } from "./TypeFactory";
|
|
16
|
+
|
|
17
|
+
export class MetadataCollection {
|
|
18
|
+
private readonly objects_: Map<ts.Type, MetadataObject>;
|
|
19
|
+
private readonly object_unions_: Map<string, MetadataObject[]>;
|
|
20
|
+
private readonly aliases_: Map<ts.Type, MetadataAlias>;
|
|
21
|
+
private readonly arrays_: Map<ts.Type, MetadataArrayType>;
|
|
22
|
+
private readonly tuples_: Map<ts.Type, MetadataTupleType>;
|
|
23
|
+
|
|
24
|
+
private readonly names_: Map<string, Map<ts.Type, string>>;
|
|
25
|
+
private object_index_: number;
|
|
26
|
+
private recursive_array_index_: number;
|
|
27
|
+
private recursive_tuple_index_: number;
|
|
28
|
+
|
|
29
|
+
public constructor(
|
|
30
|
+
private readonly options?: Partial<MetadataCollection.IOptions>,
|
|
31
|
+
) {
|
|
32
|
+
this.objects_ = new Map();
|
|
33
|
+
this.object_unions_ = new Map();
|
|
34
|
+
this.aliases_ = new Map();
|
|
35
|
+
this.arrays_ = new Map();
|
|
36
|
+
this.tuples_ = new Map();
|
|
37
|
+
|
|
38
|
+
this.names_ = new Map();
|
|
39
|
+
this.object_index_ = 0;
|
|
40
|
+
this.recursive_array_index_ = 0;
|
|
41
|
+
this.recursive_tuple_index_ = 0;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/* -----------------------------------------------------------
|
|
45
|
+
ACCESSORS
|
|
46
|
+
----------------------------------------------------------- */
|
|
47
|
+
public aliases(): MetadataAlias[] {
|
|
48
|
+
return [...this.aliases_.values()];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public objects(): MetadataObject[] {
|
|
52
|
+
return [...this.objects_.values()];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public unions(): MetadataObject[][] {
|
|
56
|
+
return [...this.object_unions_.values()];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public arrays(): MetadataArrayType[] {
|
|
60
|
+
return [...this.arrays_.values()];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public tuples(): MetadataTupleType[] {
|
|
64
|
+
return [...this.tuples_.values()];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private getName(checker: ts.TypeChecker, type: ts.Type): string {
|
|
68
|
+
const name: string = (() => {
|
|
69
|
+
const str: string = TypeFactory.getFullName(checker)(type);
|
|
70
|
+
return this.options?.replace ? this.options.replace(str) : str;
|
|
71
|
+
})();
|
|
72
|
+
|
|
73
|
+
const duplicates: Map<ts.Type, string> = MapUtil.take(this.names_)(
|
|
74
|
+
name,
|
|
75
|
+
() => new Map(),
|
|
76
|
+
);
|
|
77
|
+
const oldbie: string | undefined = duplicates.get(type);
|
|
78
|
+
if (oldbie !== undefined) return oldbie;
|
|
79
|
+
|
|
80
|
+
const addicted: string = duplicates.size
|
|
81
|
+
? `${name}.o${duplicates.size}`
|
|
82
|
+
: name;
|
|
83
|
+
duplicates.set(type, addicted);
|
|
84
|
+
return addicted;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @internal
|
|
89
|
+
*/
|
|
90
|
+
public getUnionIndex(meta: Metadata): number {
|
|
91
|
+
const key: string = meta.objects.map((obj) => obj.name).join(" | ");
|
|
92
|
+
MapUtil.take(this.object_unions_)(key, () => meta.objects);
|
|
93
|
+
return [...this.object_unions_.keys()].indexOf(key);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/* -----------------------------------------------------------
|
|
97
|
+
INSTANCES
|
|
98
|
+
----------------------------------------------------------- */
|
|
99
|
+
public emplace(
|
|
100
|
+
checker: ts.TypeChecker,
|
|
101
|
+
type: ts.Type,
|
|
102
|
+
): [MetadataObject, boolean] {
|
|
103
|
+
const oldbie = this.objects_.get(type);
|
|
104
|
+
if (oldbie !== undefined) return [oldbie, false];
|
|
105
|
+
|
|
106
|
+
// const displays = type.symbol.getDocumentationComment(checker);
|
|
107
|
+
// const tags = type.symbol.getJsDocTags(checker);
|
|
108
|
+
|
|
109
|
+
// console.log(
|
|
110
|
+
// ts.displayPartsToString(displays),
|
|
111
|
+
// tags.map((tag) => tag.name),
|
|
112
|
+
// tags.map((tag) => ts.displayPartsToString(tag.text)),
|
|
113
|
+
// );
|
|
114
|
+
|
|
115
|
+
const $id: string = this.getName(checker, type);
|
|
116
|
+
const obj: MetadataObject = MetadataObject.create({
|
|
117
|
+
name: $id,
|
|
118
|
+
properties: [],
|
|
119
|
+
description:
|
|
120
|
+
(type.aliasSymbol &&
|
|
121
|
+
CommentFactory.description(type.aliasSymbol)) ??
|
|
122
|
+
(type.symbol && CommentFactory.description(type.symbol)) ??
|
|
123
|
+
undefined,
|
|
124
|
+
jsDocTags:
|
|
125
|
+
type.aliasSymbol?.getJsDocTags() ??
|
|
126
|
+
type.symbol?.getJsDocTags() ??
|
|
127
|
+
[],
|
|
128
|
+
validated: false,
|
|
129
|
+
index: this.object_index_++,
|
|
130
|
+
recursive: null!,
|
|
131
|
+
nullables: [],
|
|
132
|
+
});
|
|
133
|
+
this.objects_.set(type, obj);
|
|
134
|
+
return [obj, true];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public emplaceAlias(
|
|
138
|
+
checker: ts.TypeChecker,
|
|
139
|
+
type: ts.Type,
|
|
140
|
+
symbol: ts.Symbol,
|
|
141
|
+
): [MetadataAlias, boolean, (meta: Metadata) => void] {
|
|
142
|
+
const oldbie = this.aliases_.get(type);
|
|
143
|
+
if (oldbie !== undefined) return [oldbie, false, () => {}];
|
|
144
|
+
|
|
145
|
+
const $id: string = this.getName(checker, type);
|
|
146
|
+
const alias: MetadataAlias = MetadataAlias.create({
|
|
147
|
+
name: $id,
|
|
148
|
+
value: null!,
|
|
149
|
+
description: CommentFactory.description(symbol) ?? null,
|
|
150
|
+
recursive: null!,
|
|
151
|
+
nullables: [],
|
|
152
|
+
jsDocTags: symbol.getJsDocTags() ?? [],
|
|
153
|
+
});
|
|
154
|
+
this.aliases_.set(type, alias);
|
|
155
|
+
return [alias, true, (meta) => (Writable(alias).value = meta)];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public emplaceArray(
|
|
159
|
+
checker: ts.TypeChecker,
|
|
160
|
+
type: ts.Type,
|
|
161
|
+
): [MetadataArrayType, boolean, (meta: Metadata) => void] {
|
|
162
|
+
const oldbie = this.arrays_.get(type);
|
|
163
|
+
if (oldbie !== undefined) return [oldbie, false, () => {}];
|
|
164
|
+
|
|
165
|
+
const $id = this.getName(checker, type);
|
|
166
|
+
const array: MetadataArrayType = MetadataArrayType.create({
|
|
167
|
+
name: $id,
|
|
168
|
+
value: null!,
|
|
169
|
+
index: null,
|
|
170
|
+
recursive: null!,
|
|
171
|
+
nullables: [],
|
|
172
|
+
});
|
|
173
|
+
this.arrays_.set(type, array);
|
|
174
|
+
return [array, true, (meta) => (Writable(array).value = meta)];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
public emplaceTuple(
|
|
178
|
+
checker: ts.TypeChecker,
|
|
179
|
+
type: ts.TupleType,
|
|
180
|
+
): [MetadataTupleType, boolean, (elements: Metadata[]) => void] {
|
|
181
|
+
const oldbie = this.tuples_.get(type);
|
|
182
|
+
if (oldbie !== undefined) return [oldbie, false, () => {}];
|
|
183
|
+
|
|
184
|
+
const $id = this.getName(checker, type);
|
|
185
|
+
const tuple: MetadataTupleType = MetadataTupleType.create({
|
|
186
|
+
name: $id,
|
|
187
|
+
elements: null!,
|
|
188
|
+
index: null,
|
|
189
|
+
recursive: null!,
|
|
190
|
+
nullables: [],
|
|
191
|
+
});
|
|
192
|
+
this.tuples_.set(type, tuple);
|
|
193
|
+
return [
|
|
194
|
+
tuple,
|
|
195
|
+
true,
|
|
196
|
+
(elements) => (Writable(tuple).elements = elements),
|
|
197
|
+
];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* @internal
|
|
202
|
+
*/
|
|
203
|
+
public setObjectRecursive(obj: MetadataObject, recursive: boolean): void {
|
|
204
|
+
Writable(obj).recursive = recursive;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* @internal
|
|
209
|
+
*/
|
|
210
|
+
public setAliasRecursive(alias: MetadataAlias, recursive: boolean): void {
|
|
211
|
+
Writable(alias).recursive = recursive;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* @internal
|
|
216
|
+
*/
|
|
217
|
+
public setArrayRecursive(
|
|
218
|
+
array: MetadataArrayType,
|
|
219
|
+
recursive: boolean,
|
|
220
|
+
): void {
|
|
221
|
+
Writable(array).recursive = recursive;
|
|
222
|
+
if (recursive) Writable(array).index = this.recursive_array_index_++;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
public setTupleRecursive(
|
|
226
|
+
tuple: MetadataTupleType,
|
|
227
|
+
recursive: boolean,
|
|
228
|
+
): void {
|
|
229
|
+
Writable(tuple).recursive = recursive;
|
|
230
|
+
if (recursive) Writable(tuple).index = this.recursive_tuple_index_++;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
public toJSON(): IMetadataCollection {
|
|
234
|
+
return {
|
|
235
|
+
objects: this.objects().map((o) => o.toJSON()),
|
|
236
|
+
aliases: this.aliases().map((d) => d.toJSON()),
|
|
237
|
+
arrays: [...this.arrays_.values()].map((a) => a.toJSON()),
|
|
238
|
+
tuples: [...this.tuples_.values()].map((t) => t.toJSON()),
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
export namespace MetadataCollection {
|
|
243
|
+
export interface IOptions {
|
|
244
|
+
replace?(str: string): string;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export const replace = (str: string): string => {
|
|
248
|
+
let replaced: string = str;
|
|
249
|
+
for (const [before] of REPLACERS)
|
|
250
|
+
replaced = replaced.split(before).join("");
|
|
251
|
+
if (replaced.length !== 0) return replaced;
|
|
252
|
+
|
|
253
|
+
for (const [before, after] of REPLACERS)
|
|
254
|
+
str = str.split(before).join(after);
|
|
255
|
+
return str;
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
export const escape = (str: string): string => {
|
|
259
|
+
for (const [before, after] of REPLACERS)
|
|
260
|
+
if (after !== "") str = str.split(after).join(before);
|
|
261
|
+
return str;
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
const REPLACERS: [string, string][] = [
|
|
265
|
+
["$", "_dollar_"],
|
|
266
|
+
["&", "_and_"],
|
|
267
|
+
["|", "_or_"],
|
|
268
|
+
["{", "_blt_"],
|
|
269
|
+
["}", "_bgt_"],
|
|
270
|
+
["<", "_lt_"],
|
|
271
|
+
[">", "_gt_"],
|
|
272
|
+
["[", "_alt_"],
|
|
273
|
+
["]", "_agt_"],
|
|
274
|
+
[",", "_comma_"],
|
|
275
|
+
["`", "_backquote_"],
|
|
276
|
+
["'", "_singlequote_"],
|
|
277
|
+
['"', "_doublequote_"],
|
|
278
|
+
[" ", "_space_"],
|
|
279
|
+
["?", "_question_"],
|
|
280
|
+
[":", "_colon_"],
|
|
281
|
+
[";", "_semicolon_"],
|
|
282
|
+
];
|