typia 3.8.4 → 4.0.0-dev.20230502
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/README.md +9 -1
- package/lib/factories/ProtocolFactory.d.ts +8 -0
- package/lib/factories/ProtocolFactory.js +112 -0
- package/lib/factories/ProtocolFactory.js.map +1 -0
- package/lib/factories/internal/protocols/ProtocolMetadataUtil.d.ts +16 -0
- package/lib/factories/internal/protocols/ProtocolMetadataUtil.js +160 -0
- package/lib/factories/internal/protocols/ProtocolMetadataUtil.js.map +1 -0
- package/lib/factories/internal/protocols/emplace_protocol_object.d.ts +3 -0
- package/lib/factories/internal/protocols/emplace_protocol_object.js +47 -0
- package/lib/factories/internal/protocols/emplace_protocol_object.js.map +1 -0
- package/lib/factories/internal/protocols/emplace_protocol_property.d.ts +4 -0
- package/lib/factories/internal/protocols/emplace_protocol_property.js +20 -0
- package/lib/factories/internal/protocols/emplace_protocol_property.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_atomic.d.ts +3 -0
- package/lib/factories/internal/protocols/iterate_protocol_atomic.js +69 -0
- package/lib/factories/internal/protocols/iterate_protocol_atomic.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_constant.d.ts +2 -0
- package/lib/factories/internal/protocols/iterate_protocol_constant.js +30 -0
- package/lib/factories/internal/protocols/iterate_protocol_constant.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_main.d.ts +3 -0
- package/lib/factories/internal/protocols/iterate_protocol_main.js +17 -0
- package/lib/factories/internal/protocols/iterate_protocol_main.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_map.d.ts +4 -0
- package/lib/factories/internal/protocols/iterate_protocol_map.js +75 -0
- package/lib/factories/internal/protocols/iterate_protocol_map.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_metadata.d.ts +5 -0
- package/lib/factories/internal/protocols/iterate_protocol_metadata.js +190 -0
- package/lib/factories/internal/protocols/iterate_protocol_metadata.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_native.d.ts +2 -0
- package/lib/factories/internal/protocols/iterate_protocol_native.js +33 -0
- package/lib/factories/internal/protocols/iterate_protocol_native.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_never.d.ts +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_never.js +6 -0
- package/lib/factories/internal/protocols/iterate_protocol_never.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_object.d.ts +4 -0
- package/lib/factories/internal/protocols/iterate_protocol_object.js +157 -0
- package/lib/factories/internal/protocols/iterate_protocol_object.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_repeated.d.ts +5 -0
- package/lib/factories/internal/protocols/iterate_protocol_repeated.js +25 -0
- package/lib/factories/internal/protocols/iterate_protocol_repeated.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_tuple.d.ts +3 -0
- package/lib/factories/internal/protocols/iterate_protocol_tuple.js +46 -0
- package/lib/factories/internal/protocols/iterate_protocol_tuple.js.map +1 -0
- package/lib/functional/$proto_bytes.d.ts +2 -0
- package/lib/functional/$proto_bytes.js +37 -0
- package/lib/functional/$proto_bytes.js.map +1 -0
- package/lib/functional/$proto_field.d.ts +10 -0
- package/lib/functional/$proto_field.js +42 -0
- package/lib/functional/$proto_field.js.map +1 -0
- package/lib/functional/$proto_float.d.ts +4 -0
- package/lib/functional/$proto_float.js +28 -0
- package/lib/functional/$proto_float.js.map +1 -0
- package/lib/functional/$proto_i32.d.ts +2 -0
- package/lib/functional/$proto_i32.js +23 -0
- package/lib/functional/$proto_i32.js.map +1 -0
- package/lib/functional/$proto_i64.d.ts +2 -0
- package/lib/functional/$proto_i64.js +31 -0
- package/lib/functional/$proto_i64.js.map +1 -0
- package/lib/functional/$proto_size.d.ts +6 -0
- package/lib/functional/$proto_size.js +76 -0
- package/lib/functional/$proto_size.js.map +1 -0
- package/lib/functional/$proto_string.d.ts +2 -0
- package/lib/functional/$proto_string.js +34 -0
- package/lib/functional/$proto_string.js.map +1 -0
- package/lib/functional/$varint.d.ts +6 -0
- package/lib/functional/$varint.js +99 -0
- package/lib/functional/$varint.js.map +1 -0
- package/lib/functional/$zigzag.d.ts +4 -0
- package/lib/functional/$zigzag.js +34 -0
- package/lib/functional/$zigzag.js.map +1 -0
- package/lib/messages/IProtocolMap.d.ts +5 -0
- package/lib/messages/IProtocolMap.js +3 -0
- package/lib/messages/IProtocolMap.js.map +1 -0
- package/lib/messages/IProtocolMessage.d.ts +5 -0
- package/lib/messages/IProtocolMessage.js +3 -0
- package/lib/messages/IProtocolMessage.js.map +1 -0
- package/lib/messages/IProtocolProperty.d.ts +12 -0
- package/lib/messages/IProtocolProperty.js +3 -0
- package/lib/messages/IProtocolProperty.js.map +1 -0
- package/lib/metadata/IMetadataTag.d.ts +3 -3
- package/lib/metadata/Metadata.js +1 -1
- package/lib/metadata/Metadata.js.map +1 -1
- package/lib/module.d.ts +2 -0
- package/lib/module.js +6 -1
- package/lib/module.js.map +1 -1
- package/lib/programmers/MessageProgrammer.d.ts +5 -0
- package/lib/programmers/MessageProgrammer.js +141 -0
- package/lib/programmers/MessageProgrammer.js.map +1 -0
- package/lib/transformers/CallExpressionTransformer.js +5 -3
- package/lib/transformers/CallExpressionTransformer.js.map +1 -1
- package/lib/transformers/features/miscellaneous/ApplicationTransformer.js +1 -3
- package/lib/transformers/features/miscellaneous/ApplicationTransformer.js.map +1 -1
- package/lib/transformers/features/protocols/MessageTransformer.d.ts +5 -0
- package/lib/transformers/features/protocols/MessageTransformer.js +20 -0
- package/lib/transformers/features/protocols/MessageTransformer.js.map +1 -0
- package/lib/utils/NameEncoder.d.ts +4 -0
- package/lib/utils/NameEncoder.js +89 -0
- package/lib/utils/NameEncoder.js.map +1 -0
- package/package.json +1 -1
- package/src/Primitive.ts +123 -123
- package/src/factories/MetadataFactory.ts +62 -62
- package/src/factories/ProtocolFactory.ts +79 -0
- package/src/factories/internal/metadata/emplace_metadata_object.ts +143 -143
- package/src/factories/internal/metadata/iterate_metadata_tuple.ts +48 -48
- package/src/factories/internal/protocols/ProtocolMetadataUtil.ts +167 -0
- package/src/factories/internal/protocols/emplace_protocol_object.ts +25 -0
- package/src/factories/internal/protocols/emplace_protocol_property.ts +12 -0
- package/src/factories/internal/protocols/iterate_protocol_atomic.ts +34 -0
- package/src/factories/internal/protocols/iterate_protocol_constant.ts +27 -0
- package/src/factories/internal/protocols/iterate_protocol_main.ts +19 -0
- package/src/factories/internal/protocols/iterate_protocol_map.ts +58 -0
- package/src/factories/internal/protocols/iterate_protocol_metadata.ts +96 -0
- package/src/factories/internal/protocols/iterate_protocol_native.ts +34 -0
- package/src/factories/internal/protocols/iterate_protocol_never.ts +1 -0
- package/src/factories/internal/protocols/iterate_protocol_object.ts +110 -0
- package/src/factories/internal/protocols/iterate_protocol_repeated.ts +34 -0
- package/src/factories/internal/protocols/iterate_protocol_tuple.ts +29 -0
- package/src/functional/$proto_bytes.ts +25 -0
- package/src/functional/$proto_field.ts +30 -0
- package/src/functional/$proto_float.ts +37 -0
- package/src/functional/$proto_i32.ts +29 -0
- package/src/functional/$proto_i64.ts +37 -0
- package/src/functional/$proto_size.ts +82 -0
- package/src/functional/$proto_string.ts +24 -0
- package/src/functional/$varint.ts +130 -0
- package/src/functional/$zigzag.ts +39 -0
- package/src/messages/IProtocolMap.ts +5 -0
- package/src/messages/IProtocolMessage.ts +6 -0
- package/src/messages/IProtocolProperty.ts +13 -0
- package/src/metadata/IMetadata.ts +26 -26
- package/src/metadata/IMetadataTag.ts +11 -3
- package/src/metadata/Metadata.ts +543 -539
- package/src/module.ts +11 -0
- package/src/programmers/CheckerProgrammer.ts +901 -901
- package/src/programmers/MessageProgrammer.ts +126 -0
- package/src/programmers/internal/application_object.ts +155 -155
- package/src/programmers/internal/application_tuple.ts +31 -31
- package/src/schemas/IJsonSchema.ts +130 -130
- package/src/transformers/CallExpressionTransformer.ts +17 -10
- package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +1 -3
- package/src/transformers/features/protocols/MessageTransformer.ts +31 -0
- package/src/utils/NameEncoder.ts +32 -0
package/src/Primitive.ts
CHANGED
|
@@ -1,123 +1,123 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Primitive type.
|
|
3
|
-
*
|
|
4
|
-
* `Primitive` is a type of TMP (Type Meta Programming) type who converts its argument as a
|
|
5
|
-
* primitive type.
|
|
6
|
-
*
|
|
7
|
-
* If the target argument is a built-in class who returns its origin primitive type through
|
|
8
|
-
* the `valueOf()` method like the `String` or `Number`, its return type would be the
|
|
9
|
-
* `string` or `number`.
|
|
10
|
-
*
|
|
11
|
-
* Otherwise, the target argument is a type of custom class, all of its custom method would
|
|
12
|
-
* be erased and its prototype would be changed to the primitive `object`. Therefore, return
|
|
13
|
-
* type of the TMP type finally be the primitive object.
|
|
14
|
-
*
|
|
15
|
-
* In addition, if the target argument is a type of custom class and it has a special
|
|
16
|
-
* method `toJSON()`, return type of this `Primitive` would be not `Primitive<Instance>`
|
|
17
|
-
* but `Primitive<ReturnType<Instance.toJSON>>`.
|
|
18
|
-
*
|
|
19
|
-
* Before | After
|
|
20
|
-
* ------------------------|----------------------------------------
|
|
21
|
-
* `Boolean` | `boolean`
|
|
22
|
-
* `Number` | `number`
|
|
23
|
-
* `String` | `string`
|
|
24
|
-
* `Class` | `object`
|
|
25
|
-
* `Class` with `toJSON()` | `Primitive<ReturnType<Class.toJSON>>`
|
|
26
|
-
* Others | No change
|
|
27
|
-
*
|
|
28
|
-
* @template Instance Target argument type.
|
|
29
|
-
* @author Jenogho Nam - https://github.com/samchon
|
|
30
|
-
*/
|
|
31
|
-
export type Primitive<T> = Equal<T, PrimitiveMain<T>> extends true
|
|
32
|
-
? T
|
|
33
|
-
: PrimitiveMain<T>;
|
|
34
|
-
|
|
35
|
-
type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;
|
|
36
|
-
|
|
37
|
-
type PrimitiveMain<Instance> = ValueOf<Instance> extends object
|
|
38
|
-
? Instance extends object
|
|
39
|
-
? Instance extends NativeClass
|
|
40
|
-
? {}
|
|
41
|
-
: Instance extends IJsonable<infer Raw>
|
|
42
|
-
? ValueOf<Raw> extends object
|
|
43
|
-
? Raw extends object
|
|
44
|
-
? PrimitiveObject<Raw> // object would be primitified
|
|
45
|
-
: never // cannot be
|
|
46
|
-
: ValueOf<Raw> // atomic value
|
|
47
|
-
: PrimitiveObject<Instance> // object would be primitified
|
|
48
|
-
: never // cannot be
|
|
49
|
-
: ValueOf<Instance>;
|
|
50
|
-
|
|
51
|
-
type PrimitiveObject<Instance extends object> = Instance extends Array<infer T>
|
|
52
|
-
? IsTuple<Instance> extends true
|
|
53
|
-
? PrimitiveTuple<Instance>
|
|
54
|
-
: PrimitiveMain<T>[]
|
|
55
|
-
: {
|
|
56
|
-
[P in keyof Instance]: Instance[P] extends Function
|
|
57
|
-
? never
|
|
58
|
-
: PrimitiveMain<Instance[P]>;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
type PrimitiveTuple<T extends readonly any[]> = T extends [infer F]
|
|
62
|
-
? [PrimitiveMain<F>]
|
|
63
|
-
: T extends [infer F, ...infer Rest extends readonly any[]]
|
|
64
|
-
? [PrimitiveMain<F>, ...PrimitiveTuple<Rest>]
|
|
65
|
-
: T extends [(infer F)?]
|
|
66
|
-
? [PrimitiveMain<F>?]
|
|
67
|
-
: T extends [(infer F)?, ...infer Rest extends readonly any[]]
|
|
68
|
-
? [PrimitiveMain<F>?, ...PrimitiveTuple<Rest>]
|
|
69
|
-
: [];
|
|
70
|
-
|
|
71
|
-
type ValueOf<Instance> = IsValueOf<Instance, Boolean> extends true
|
|
72
|
-
? boolean
|
|
73
|
-
: IsValueOf<Instance, Number> extends true
|
|
74
|
-
? number
|
|
75
|
-
: IsValueOf<Instance, String> extends true
|
|
76
|
-
? string
|
|
77
|
-
: Instance;
|
|
78
|
-
|
|
79
|
-
type NativeClass =
|
|
80
|
-
| Set<any>
|
|
81
|
-
| Map<any, any>
|
|
82
|
-
| WeakSet<any>
|
|
83
|
-
| WeakMap<any, any>
|
|
84
|
-
| Uint8Array
|
|
85
|
-
| Uint8ClampedArray
|
|
86
|
-
| Uint16Array
|
|
87
|
-
| Uint32Array
|
|
88
|
-
| BigUint64Array
|
|
89
|
-
| Int8Array
|
|
90
|
-
| Int16Array
|
|
91
|
-
| Int32Array
|
|
92
|
-
| BigInt64Array
|
|
93
|
-
| Float32Array
|
|
94
|
-
| Float64Array
|
|
95
|
-
| ArrayBuffer
|
|
96
|
-
| SharedArrayBuffer
|
|
97
|
-
| DataView;
|
|
98
|
-
|
|
99
|
-
type IsTuple<T extends readonly any[] | { length: number }> = [T] extends [
|
|
100
|
-
never,
|
|
101
|
-
]
|
|
102
|
-
? false
|
|
103
|
-
: T extends readonly any[]
|
|
104
|
-
? number extends T["length"]
|
|
105
|
-
? false
|
|
106
|
-
: true
|
|
107
|
-
: false;
|
|
108
|
-
|
|
109
|
-
type IsValueOf<Instance, Object extends IValueOf<any>> = Instance extends Object
|
|
110
|
-
? Object extends IValueOf<infer Primitive>
|
|
111
|
-
? Instance extends Primitive
|
|
112
|
-
? false
|
|
113
|
-
: true // not Primitive, but Object
|
|
114
|
-
: false // cannot be
|
|
115
|
-
: false;
|
|
116
|
-
|
|
117
|
-
interface IValueOf<T> {
|
|
118
|
-
valueOf(): T;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
interface IJsonable<T> {
|
|
122
|
-
toJSON(): T;
|
|
123
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Primitive type.
|
|
3
|
+
*
|
|
4
|
+
* `Primitive` is a type of TMP (Type Meta Programming) type who converts its argument as a
|
|
5
|
+
* primitive type.
|
|
6
|
+
*
|
|
7
|
+
* If the target argument is a built-in class who returns its origin primitive type through
|
|
8
|
+
* the `valueOf()` method like the `String` or `Number`, its return type would be the
|
|
9
|
+
* `string` or `number`.
|
|
10
|
+
*
|
|
11
|
+
* Otherwise, the target argument is a type of custom class, all of its custom method would
|
|
12
|
+
* be erased and its prototype would be changed to the primitive `object`. Therefore, return
|
|
13
|
+
* type of the TMP type finally be the primitive object.
|
|
14
|
+
*
|
|
15
|
+
* In addition, if the target argument is a type of custom class and it has a special
|
|
16
|
+
* method `toJSON()`, return type of this `Primitive` would be not `Primitive<Instance>`
|
|
17
|
+
* but `Primitive<ReturnType<Instance.toJSON>>`.
|
|
18
|
+
*
|
|
19
|
+
* Before | After
|
|
20
|
+
* ------------------------|----------------------------------------
|
|
21
|
+
* `Boolean` | `boolean`
|
|
22
|
+
* `Number` | `number`
|
|
23
|
+
* `String` | `string`
|
|
24
|
+
* `Class` | `object`
|
|
25
|
+
* `Class` with `toJSON()` | `Primitive<ReturnType<Class.toJSON>>`
|
|
26
|
+
* Others | No change
|
|
27
|
+
*
|
|
28
|
+
* @template Instance Target argument type.
|
|
29
|
+
* @author Jenogho Nam - https://github.com/samchon
|
|
30
|
+
*/
|
|
31
|
+
export type Primitive<T> = Equal<T, PrimitiveMain<T>> extends true
|
|
32
|
+
? T
|
|
33
|
+
: PrimitiveMain<T>;
|
|
34
|
+
|
|
35
|
+
type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;
|
|
36
|
+
|
|
37
|
+
type PrimitiveMain<Instance> = ValueOf<Instance> extends object
|
|
38
|
+
? Instance extends object
|
|
39
|
+
? Instance extends NativeClass
|
|
40
|
+
? {}
|
|
41
|
+
: Instance extends IJsonable<infer Raw>
|
|
42
|
+
? ValueOf<Raw> extends object
|
|
43
|
+
? Raw extends object
|
|
44
|
+
? PrimitiveObject<Raw> // object would be primitified
|
|
45
|
+
: never // cannot be
|
|
46
|
+
: ValueOf<Raw> // atomic value
|
|
47
|
+
: PrimitiveObject<Instance> // object would be primitified
|
|
48
|
+
: never // cannot be
|
|
49
|
+
: ValueOf<Instance>;
|
|
50
|
+
|
|
51
|
+
type PrimitiveObject<Instance extends object> = Instance extends Array<infer T>
|
|
52
|
+
? IsTuple<Instance> extends true
|
|
53
|
+
? PrimitiveTuple<Instance>
|
|
54
|
+
: PrimitiveMain<T>[]
|
|
55
|
+
: {
|
|
56
|
+
[P in keyof Instance]: Instance[P] extends Function
|
|
57
|
+
? never
|
|
58
|
+
: PrimitiveMain<Instance[P]>;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
type PrimitiveTuple<T extends readonly any[]> = T extends [infer F]
|
|
62
|
+
? [PrimitiveMain<F>]
|
|
63
|
+
: T extends [infer F, ...infer Rest extends readonly any[]]
|
|
64
|
+
? [PrimitiveMain<F>, ...PrimitiveTuple<Rest>]
|
|
65
|
+
: T extends [(infer F)?]
|
|
66
|
+
? [PrimitiveMain<F>?]
|
|
67
|
+
: T extends [(infer F)?, ...infer Rest extends readonly any[]]
|
|
68
|
+
? [PrimitiveMain<F>?, ...PrimitiveTuple<Rest>]
|
|
69
|
+
: [];
|
|
70
|
+
|
|
71
|
+
type ValueOf<Instance> = IsValueOf<Instance, Boolean> extends true
|
|
72
|
+
? boolean
|
|
73
|
+
: IsValueOf<Instance, Number> extends true
|
|
74
|
+
? number
|
|
75
|
+
: IsValueOf<Instance, String> extends true
|
|
76
|
+
? string
|
|
77
|
+
: Instance;
|
|
78
|
+
|
|
79
|
+
type NativeClass =
|
|
80
|
+
| Set<any>
|
|
81
|
+
| Map<any, any>
|
|
82
|
+
| WeakSet<any>
|
|
83
|
+
| WeakMap<any, any>
|
|
84
|
+
| Uint8Array
|
|
85
|
+
| Uint8ClampedArray
|
|
86
|
+
| Uint16Array
|
|
87
|
+
| Uint32Array
|
|
88
|
+
| BigUint64Array
|
|
89
|
+
| Int8Array
|
|
90
|
+
| Int16Array
|
|
91
|
+
| Int32Array
|
|
92
|
+
| BigInt64Array
|
|
93
|
+
| Float32Array
|
|
94
|
+
| Float64Array
|
|
95
|
+
| ArrayBuffer
|
|
96
|
+
| SharedArrayBuffer
|
|
97
|
+
| DataView;
|
|
98
|
+
|
|
99
|
+
type IsTuple<T extends readonly any[] | { length: number }> = [T] extends [
|
|
100
|
+
never,
|
|
101
|
+
]
|
|
102
|
+
? false
|
|
103
|
+
: T extends readonly any[]
|
|
104
|
+
? number extends T["length"]
|
|
105
|
+
? false
|
|
106
|
+
: true
|
|
107
|
+
: false;
|
|
108
|
+
|
|
109
|
+
type IsValueOf<Instance, Object extends IValueOf<any>> = Instance extends Object
|
|
110
|
+
? Object extends IValueOf<infer Primitive>
|
|
111
|
+
? Instance extends Primitive
|
|
112
|
+
? false
|
|
113
|
+
: true // not Primitive, but Object
|
|
114
|
+
: false // cannot be
|
|
115
|
+
: false;
|
|
116
|
+
|
|
117
|
+
interface IValueOf<T> {
|
|
118
|
+
valueOf(): T;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
interface IJsonable<T> {
|
|
122
|
+
toJSON(): T;
|
|
123
|
+
}
|
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { Metadata } from "../metadata/Metadata";
|
|
4
|
-
import { explore_metadata } from "./internal/metadata/explore_metadata";
|
|
5
|
-
|
|
6
|
-
import { MetadataCollection } from "./MetadataCollection";
|
|
7
|
-
|
|
8
|
-
export namespace MetadataFactory {
|
|
9
|
-
export interface IOptions {
|
|
10
|
-
resolve: boolean;
|
|
11
|
-
constant: boolean;
|
|
12
|
-
validate?: (meta: Metadata) => void;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const analyze =
|
|
16
|
-
(checker: ts.TypeChecker) =>
|
|
17
|
-
(options: IOptions) =>
|
|
18
|
-
(collection: MetadataCollection) =>
|
|
19
|
-
(type: ts.Type | null): Metadata => {
|
|
20
|
-
// CONSTRUCT SCHEMA WITH OBJECTS
|
|
21
|
-
const metadata: Metadata = explore_metadata(checker)(options)(
|
|
22
|
-
collection,
|
|
23
|
-
)(type, false);
|
|
24
|
-
|
|
25
|
-
// FIND RECURSIVE OBJECTS
|
|
26
|
-
for (const object of collection.objects())
|
|
27
|
-
object.recursive = object.properties.some((prop) =>
|
|
28
|
-
isRecursive(object.name)(prop.value),
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
// RETURNS
|
|
32
|
-
return metadata;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const isRecursive =
|
|
36
|
-
(name: string) =>
|
|
37
|
-
(meta: Metadata): boolean => {
|
|
38
|
-
const similar = (str: string) =>
|
|
39
|
-
name === str ||
|
|
40
|
-
name.indexOf(`<${str},`) !== -1 ||
|
|
41
|
-
name.indexOf(`, ${str}>`) !== -1 ||
|
|
42
|
-
name.indexOf(`, ${str},`) !== -1;
|
|
43
|
-
return (
|
|
44
|
-
meta.objects.some((obj) => similar(obj.name)) ||
|
|
45
|
-
meta.arrays.some((arr) => isRecursive(name)(arr)) ||
|
|
46
|
-
meta.tuples.some((tuple) =>
|
|
47
|
-
tuple.some((m) => isRecursive(name)(m.rest ?? m)),
|
|
48
|
-
) ||
|
|
49
|
-
meta.maps.some((map) => isRecursive(name)(map.value))
|
|
50
|
-
);
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @deprecated Use `analyze` function instead
|
|
55
|
-
*/
|
|
56
|
-
export const generate = (
|
|
57
|
-
checker: ts.TypeChecker,
|
|
58
|
-
collection: MetadataCollection,
|
|
59
|
-
type: ts.Type,
|
|
60
|
-
options: IOptions,
|
|
61
|
-
) => analyze(checker)(options)(collection)(type);
|
|
62
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { Metadata } from "../metadata/Metadata";
|
|
4
|
+
import { explore_metadata } from "./internal/metadata/explore_metadata";
|
|
5
|
+
|
|
6
|
+
import { MetadataCollection } from "./MetadataCollection";
|
|
7
|
+
|
|
8
|
+
export namespace MetadataFactory {
|
|
9
|
+
export interface IOptions {
|
|
10
|
+
resolve: boolean;
|
|
11
|
+
constant: boolean;
|
|
12
|
+
validate?: (meta: Metadata) => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const analyze =
|
|
16
|
+
(checker: ts.TypeChecker) =>
|
|
17
|
+
(options: IOptions) =>
|
|
18
|
+
(collection: MetadataCollection) =>
|
|
19
|
+
(type: ts.Type | null): Metadata => {
|
|
20
|
+
// CONSTRUCT SCHEMA WITH OBJECTS
|
|
21
|
+
const metadata: Metadata = explore_metadata(checker)(options)(
|
|
22
|
+
collection,
|
|
23
|
+
)(type, false);
|
|
24
|
+
|
|
25
|
+
// FIND RECURSIVE OBJECTS
|
|
26
|
+
for (const object of collection.objects())
|
|
27
|
+
object.recursive = object.properties.some((prop) =>
|
|
28
|
+
isRecursive(object.name)(prop.value),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
// RETURNS
|
|
32
|
+
return metadata;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const isRecursive =
|
|
36
|
+
(name: string) =>
|
|
37
|
+
(meta: Metadata): boolean => {
|
|
38
|
+
const similar = (str: string) =>
|
|
39
|
+
name === str ||
|
|
40
|
+
name.indexOf(`<${str},`) !== -1 ||
|
|
41
|
+
name.indexOf(`, ${str}>`) !== -1 ||
|
|
42
|
+
name.indexOf(`, ${str},`) !== -1;
|
|
43
|
+
return (
|
|
44
|
+
meta.objects.some((obj) => similar(obj.name)) ||
|
|
45
|
+
meta.arrays.some((arr) => isRecursive(name)(arr)) ||
|
|
46
|
+
meta.tuples.some((tuple) =>
|
|
47
|
+
tuple.some((m) => isRecursive(name)(m.rest ?? m)),
|
|
48
|
+
) ||
|
|
49
|
+
meta.maps.some((map) => isRecursive(name)(map.value))
|
|
50
|
+
);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @deprecated Use `analyze` function instead
|
|
55
|
+
*/
|
|
56
|
+
export const generate = (
|
|
57
|
+
checker: ts.TypeChecker,
|
|
58
|
+
collection: MetadataCollection,
|
|
59
|
+
type: ts.Type,
|
|
60
|
+
options: IOptions,
|
|
61
|
+
) => analyze(checker)(options)(collection)(type);
|
|
62
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { Metadata } from "../metadata/Metadata";
|
|
4
|
+
|
|
5
|
+
import { IProtocolMessage } from "../messages/IProtocolMessage";
|
|
6
|
+
import { MetadataCollection } from "./MetadataCollection";
|
|
7
|
+
import { MetadataFactory } from "./MetadataFactory";
|
|
8
|
+
import { emplace_protocol_object } from "./internal/protocols/emplace_protocol_object";
|
|
9
|
+
import { iterate_protocol_main } from "./internal/protocols/iterate_protocol_main";
|
|
10
|
+
|
|
11
|
+
export namespace ProtocolFactory {
|
|
12
|
+
export const metadata =
|
|
13
|
+
(checker: ts.TypeChecker) =>
|
|
14
|
+
(collection: MetadataCollection) =>
|
|
15
|
+
(type: ts.Type) =>
|
|
16
|
+
MetadataFactory.generate(checker, collection, type, {
|
|
17
|
+
resolve: false,
|
|
18
|
+
constant: true,
|
|
19
|
+
validate: (meta) => {
|
|
20
|
+
if (meta.any) throw new Error(ErrorMessages.NO_ANY);
|
|
21
|
+
else if (meta.functional && meta.size() !== 1)
|
|
22
|
+
throw new Error(ErrorMessages.NO_FUNCTIONAL);
|
|
23
|
+
else if (meta.objects.find((o) => o.name === "__Main"))
|
|
24
|
+
throw new Error(ErrorMessages.NO_MAIN);
|
|
25
|
+
else if (meta.objects.find((o) => o.name === "__Timestamp"))
|
|
26
|
+
throw new Error(ErrorMessages.NO_TIMESTAMP);
|
|
27
|
+
else if (
|
|
28
|
+
meta.objects.some((o) =>
|
|
29
|
+
o.properties.some((p) => !is_atomic_key(p.key)),
|
|
30
|
+
) ||
|
|
31
|
+
meta.maps.some((m) => !is_atomic_key(m.key))
|
|
32
|
+
)
|
|
33
|
+
throw new Error(ErrorMessages.NOT_ALLOWED_KEY);
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
export const generate =
|
|
38
|
+
(collection: MetadataCollection) =>
|
|
39
|
+
(dict: Map<string, IProtocolMessage>) =>
|
|
40
|
+
(meta: Metadata) => {
|
|
41
|
+
// EMPLACE OBJECTS
|
|
42
|
+
for (const obj of collection.objects())
|
|
43
|
+
emplace_protocol_object(dict)(obj);
|
|
44
|
+
|
|
45
|
+
// WHEN NOT OBJECT, WRAP IT INTO A FAKE MAIN OBJECT
|
|
46
|
+
iterate_protocol_main(dict)(meta);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const is_atomic_key = (key: Metadata) => {
|
|
50
|
+
if (
|
|
51
|
+
key.required &&
|
|
52
|
+
key.nullable === false &&
|
|
53
|
+
key.functional === false &&
|
|
54
|
+
key.resolved === null &&
|
|
55
|
+
key.size() ===
|
|
56
|
+
key.atomics.length +
|
|
57
|
+
key.constants
|
|
58
|
+
.map((c) => c.values.length)
|
|
59
|
+
.reduce((a, b) => a + b, 0) +
|
|
60
|
+
key.templates.length
|
|
61
|
+
) {
|
|
62
|
+
const set: Set<string> = new Set();
|
|
63
|
+
for (const atomic of key.atomics) set.add(atomic);
|
|
64
|
+
for (const constant of key.constants) set.add(constant.type);
|
|
65
|
+
if (key.templates.length) set.add("string");
|
|
66
|
+
|
|
67
|
+
return set.size === 1;
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const enum ErrorMessages {
|
|
74
|
+
NO_ANY = `Error on typia.message(): any type is not supported in protocol buffer.`,
|
|
75
|
+
NO_FUNCTIONAL = `Error on typia.message(): functional type is not supported in protocol buffer.`,
|
|
76
|
+
NO_MAIN = `Error on typia.message(): reserved type "__Main" has been detected.`,
|
|
77
|
+
NO_TIMESTAMP = `Error on typia.message(): reserved type "__Timestamp" has been detected.`,
|
|
78
|
+
NOT_ALLOWED_KEY = `Error on typia.message(): only atomic key type is supported in protocol buffer.`,
|
|
79
|
+
}
|