typia 5.0.0-dev.20230820 → 5.0.0-dev.20230821

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 (221) hide show
  1. package/README.md +1 -1
  2. package/lib/factories/MetadataTagFactory.js +1 -2
  3. package/lib/factories/MetadataTagFactory.js.map +1 -1
  4. package/lib/programmers/RandomProgrammer.js +18 -9
  5. package/lib/programmers/RandomProgrammer.js.map +1 -1
  6. package/lib/programmers/helpers/RandomRanger.js +2 -2
  7. package/lib/programmers/helpers/RandomRanger.js.map +1 -1
  8. package/lib/programmers/internal/application_number.js +6 -1
  9. package/lib/programmers/internal/application_number.js.map +1 -1
  10. package/lib/programmers/internal/check_bigint.js +8 -11
  11. package/lib/programmers/internal/check_bigint.js.map +1 -1
  12. package/lib/programmers/internal/check_number.js +12 -2
  13. package/lib/programmers/internal/check_number.js.map +1 -1
  14. package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js +1 -1
  15. package/lib/programmers/protobuf/ProtobufEncodeProgrammer.js.map +1 -1
  16. package/lib/schemas/metadata/IMetadataTag.d.ts +1 -1
  17. package/package.json +1 -1
  18. package/src/factories/ExpressionFactory.ts +115 -115
  19. package/src/factories/MetadataCollection.ts +269 -269
  20. package/src/factories/MetadataFactory.ts +34 -34
  21. package/src/factories/MetadataTagFactory.ts +364 -365
  22. package/src/factories/ProtobufFactory.ts +251 -251
  23. package/src/factories/StatementFactory.ts +72 -72
  24. package/src/factories/internal/metadata/MetadataHelper.ts +12 -12
  25. package/src/factories/internal/metadata/emend_metadata_atomics.ts +33 -33
  26. package/src/factories/internal/metadata/emplace_metadata_alias.ts +40 -40
  27. package/src/factories/internal/metadata/emplace_metadata_array.ts +34 -34
  28. package/src/factories/internal/metadata/emplace_metadata_object.ts +136 -136
  29. package/src/factories/internal/metadata/emplace_metadata_tuple.ts +50 -50
  30. package/src/factories/internal/metadata/explore_metadata.ts +38 -38
  31. package/src/factories/internal/metadata/iterate_metadata.ts +81 -81
  32. package/src/factories/internal/metadata/iterate_metadata_alias.ts +30 -30
  33. package/src/factories/internal/metadata/iterate_metadata_array.ts +24 -24
  34. package/src/factories/internal/metadata/iterate_metadata_atomic.ts +59 -59
  35. package/src/factories/internal/metadata/iterate_metadata_coalesce.ts +33 -33
  36. package/src/factories/internal/metadata/iterate_metadata_collection.ts +133 -133
  37. package/src/factories/internal/metadata/iterate_metadata_constant.ts +58 -58
  38. package/src/factories/internal/metadata/iterate_metadata_intersection.ts +84 -84
  39. package/src/factories/internal/metadata/iterate_metadata_map.ts +41 -41
  40. package/src/factories/internal/metadata/iterate_metadata_native.ts +219 -219
  41. package/src/factories/internal/metadata/iterate_metadata_object.ts +43 -43
  42. package/src/factories/internal/metadata/iterate_metadata_resolve.ts +49 -49
  43. package/src/factories/internal/metadata/iterate_metadata_set.ts +33 -33
  44. package/src/factories/internal/metadata/iterate_metadata_sort.ts +69 -69
  45. package/src/factories/internal/metadata/iterate_metadata_tag.ts +31 -31
  46. package/src/factories/internal/metadata/iterate_metadata_template.ts +38 -38
  47. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +24 -24
  48. package/src/factories/internal/metadata/iterate_metadata_union.ts +24 -24
  49. package/src/functional/$ProtobufReader.ts +188 -188
  50. package/src/functional/$ProtobufSizer.ts +144 -144
  51. package/src/functional/$ProtobufWriter.ts +152 -152
  52. package/src/functional/$number.ts +13 -13
  53. package/src/functional/$strlen.ts +7 -7
  54. package/src/functional/$varint.ts +130 -130
  55. package/src/functional/$zigzag.ts +39 -39
  56. package/src/functional/IProtobufWriter.ts +18 -18
  57. package/src/functional/Namespace.ts +149 -149
  58. package/src/json.ts +648 -648
  59. package/src/misc.ts +651 -651
  60. package/src/module.ts +708 -708
  61. package/src/programmers/CheckerProgrammer.ts +1173 -1173
  62. package/src/programmers/FeatureProgrammer.ts +497 -497
  63. package/src/programmers/IsProgrammer.ts +239 -239
  64. package/src/programmers/IsPruneProgrammer.ts +73 -73
  65. package/src/programmers/RandomProgrammer.ts +578 -579
  66. package/src/programmers/helpers/AtomicPredicator.ts +31 -31
  67. package/src/programmers/helpers/CloneJoiner.ts +107 -107
  68. package/src/programmers/helpers/FunctionImporeter.ts +89 -89
  69. package/src/programmers/helpers/IExpressionEntry.ts +12 -12
  70. package/src/programmers/helpers/ProtobufUtil.ts +24 -24
  71. package/src/programmers/helpers/ProtobufWire.ts +34 -34
  72. package/src/programmers/helpers/PruneJoiner.ts +143 -143
  73. package/src/programmers/helpers/RandomJoiner.ts +173 -173
  74. package/src/programmers/helpers/RandomRanger.ts +208 -216
  75. package/src/programmers/helpers/StringifyPredicator.ts +13 -13
  76. package/src/programmers/helpers/UnionExplorer.ts +310 -310
  77. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  78. package/src/programmers/helpers/disable_function_importer_declare.ts +27 -27
  79. package/src/programmers/internal/application_alias.ts +66 -66
  80. package/src/programmers/internal/application_array.ts +30 -30
  81. package/src/programmers/internal/application_constant.ts +26 -26
  82. package/src/programmers/internal/application_default.ts +17 -17
  83. package/src/programmers/internal/application_default_string.ts +33 -33
  84. package/src/programmers/internal/application_native.ts +39 -39
  85. package/src/programmers/internal/application_number.ts +81 -77
  86. package/src/programmers/internal/application_object.ts +165 -165
  87. package/src/programmers/internal/application_resolved.ts +55 -55
  88. package/src/programmers/internal/application_schema.ts +157 -157
  89. package/src/programmers/internal/application_string.ts +44 -44
  90. package/src/programmers/internal/application_templates.ts +25 -25
  91. package/src/programmers/internal/application_tuple.ts +57 -57
  92. package/src/programmers/internal/check_array.ts +30 -30
  93. package/src/programmers/internal/check_array_length.ts +35 -35
  94. package/src/programmers/internal/check_bigint.ts +110 -92
  95. package/src/programmers/internal/check_custom.ts +31 -31
  96. package/src/programmers/internal/check_number.ts +258 -230
  97. package/src/programmers/internal/check_string.ts +25 -25
  98. package/src/programmers/internal/check_string_tags.ts +67 -67
  99. package/src/programmers/internal/check_template.ts +56 -56
  100. package/src/programmers/internal/check_union_array_like.ts +329 -329
  101. package/src/programmers/internal/decode_union_object.ts +84 -84
  102. package/src/programmers/internal/feature_object_entries.ts +63 -63
  103. package/src/programmers/internal/get_comment_tags.ts +23 -23
  104. package/src/programmers/internal/metadata_to_pattern.ts +34 -34
  105. package/src/programmers/internal/prune_object_properties.ts +60 -60
  106. package/src/programmers/internal/random_custom.ts +29 -29
  107. package/src/programmers/internal/stringify_regular_properties.ts +83 -83
  108. package/src/programmers/internal/template_to_pattern.ts +15 -15
  109. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +16 -16
  110. package/src/programmers/json/JsonApplicationProgrammer.ts +47 -47
  111. package/src/programmers/json/JsonAssertParseProgrammer.ts +66 -66
  112. package/src/programmers/json/JsonAssertStringifyProgrammer.ts +66 -66
  113. package/src/programmers/json/JsonIsParseProgrammer.ts +72 -72
  114. package/src/programmers/json/JsonIsStringifyProgrammer.ts +76 -76
  115. package/src/programmers/json/JsonStringifyProgrammer.ts +987 -987
  116. package/src/programmers/json/JsonValidateParseProgrammer.ts +70 -70
  117. package/src/programmers/json/JsonValidateStringifyProgrammer.ts +84 -84
  118. package/src/programmers/misc/MiscAssertCloneProgrammer.ts +71 -71
  119. package/src/programmers/misc/MiscAssertPruneProgrammer.ts +68 -68
  120. package/src/programmers/misc/MiscCloneProgrammer.ts +587 -587
  121. package/src/programmers/misc/MiscIsCloneProgrammer.ts +78 -78
  122. package/src/programmers/misc/MiscLiteralsProgrammer.ts +64 -64
  123. package/src/programmers/misc/MiscPruneProgrammer.ts +542 -542
  124. package/src/programmers/misc/MiscValidateCloneProgrammer.ts +85 -85
  125. package/src/programmers/misc/MiscValidatePruneProgrammer.ts +78 -78
  126. package/src/programmers/protobuf/ProtobufAssertDecodeProgrammer.ts +69 -69
  127. package/src/programmers/protobuf/ProtobufAssertEncodeProgrammer.ts +66 -66
  128. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +38 -38
  129. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +920 -921
  130. package/src/programmers/protobuf/ProtobufIsDecodeProgrammer.ts +75 -75
  131. package/src/programmers/protobuf/ProtobufIsEncodeProgrammer.ts +76 -76
  132. package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +178 -178
  133. package/src/programmers/protobuf/ProtobufValidateDecodeProgrammer.ts +83 -83
  134. package/src/programmers/protobuf/ProtobufValidateEncodeProgrammer.ts +86 -86
  135. package/src/protobuf.ts +239 -239
  136. package/src/schemas/json/IJsonApplication.ts +8 -8
  137. package/src/schemas/json/IJsonComponents.ts +33 -33
  138. package/src/schemas/json/IJsonSchema.ts +133 -133
  139. package/src/schemas/metadata/ICommentTag.ts +4 -4
  140. package/src/schemas/metadata/IJsDocTagInfo.ts +10 -10
  141. package/src/schemas/metadata/IMetadata.ts +28 -28
  142. package/src/schemas/metadata/IMetadataAlias.ts +14 -14
  143. package/src/schemas/metadata/IMetadataApplication.ts +7 -7
  144. package/src/schemas/metadata/IMetadataArray.ts +10 -10
  145. package/src/schemas/metadata/IMetadataCollection.ts +11 -11
  146. package/src/schemas/metadata/IMetadataConstant.ts +16 -16
  147. package/src/schemas/metadata/IMetadataDictionary.ts +14 -14
  148. package/src/schemas/metadata/IMetadataEntry.ts +6 -6
  149. package/src/schemas/metadata/IMetadataObject.ts +18 -18
  150. package/src/schemas/metadata/IMetadataProperty.ts +11 -11
  151. package/src/schemas/metadata/IMetadataResolved.ts +6 -6
  152. package/src/schemas/metadata/IMetadataTag.ts +112 -113
  153. package/src/schemas/metadata/IMetadataTuple.ts +10 -10
  154. package/src/schemas/metadata/Metadata.ts +637 -637
  155. package/src/schemas/metadata/MetadataAlias.ts +66 -66
  156. package/src/schemas/metadata/MetadataArray.ts +55 -55
  157. package/src/schemas/metadata/MetadataConstant.ts +3 -3
  158. package/src/schemas/metadata/MetadataObject.ts +139 -139
  159. package/src/schemas/metadata/MetadataProperty.ts +64 -64
  160. package/src/schemas/metadata/MetadataResolved.ts +51 -51
  161. package/src/schemas/metadata/MetadataTuple.ts +58 -58
  162. package/src/transformers/CallExpressionTransformer.ts +240 -240
  163. package/src/transformers/features/AssertTransformer.ts +11 -11
  164. package/src/transformers/features/CreateAssertTransformer.ts +13 -13
  165. package/src/transformers/features/CreateIsTransformer.ts +11 -11
  166. package/src/transformers/features/CreateRandomTransformer.ts +39 -39
  167. package/src/transformers/features/CreateValidateTransformer.ts +13 -13
  168. package/src/transformers/features/IsTransformer.ts +11 -11
  169. package/src/transformers/features/RandomTransformer.ts +42 -42
  170. package/src/transformers/features/ValidateTransformer.ts +11 -11
  171. package/src/transformers/features/json/JsonApplicationTransformer.ts +111 -111
  172. package/src/transformers/features/json/JsonAssertParseTransformer.ts +10 -10
  173. package/src/transformers/features/json/JsonAssertStringifyTransformer.ts +10 -10
  174. package/src/transformers/features/json/JsonCreateAssertParseTransformer.ts +10 -10
  175. package/src/transformers/features/json/JsonCreateAssertStringifyTransformer.ts +12 -12
  176. package/src/transformers/features/json/JsonCreateIsParseTransformer.ts +9 -9
  177. package/src/transformers/features/json/JsonCreateIsStringifyTransformer.ts +10 -10
  178. package/src/transformers/features/json/JsonCreateStringifyTransformer.ts +9 -9
  179. package/src/transformers/features/json/JsonCreateValidateParseTransformer.ts +10 -10
  180. package/src/transformers/features/json/JsonCreateValidateStringifyProgrammer.ts +12 -12
  181. package/src/transformers/features/json/JsonIsParseTransformer.ts +9 -9
  182. package/src/transformers/features/json/JsonIsStringifyTransformer.ts +10 -10
  183. package/src/transformers/features/json/JsonStringifyTransformer.ts +9 -9
  184. package/src/transformers/features/json/JsonValidateParseTransformer.ts +10 -10
  185. package/src/transformers/features/json/JsonValidateStringifyTransformer.ts +10 -10
  186. package/src/transformers/features/misc/MetadataTransformer.ts +53 -53
  187. package/src/transformers/features/misc/MiscAssertCloneTransformer.ts +10 -10
  188. package/src/transformers/features/misc/MiscAssertPruneTransformer.ts +10 -10
  189. package/src/transformers/features/misc/MiscCloneTransformer.ts +9 -9
  190. package/src/transformers/features/misc/MiscCreateAssertCloneTransformer.ts +10 -10
  191. package/src/transformers/features/misc/MiscCreateAssertPruneTransformer.ts +10 -10
  192. package/src/transformers/features/misc/MiscCreateCloneTransformer.ts +9 -9
  193. package/src/transformers/features/misc/MiscCreateIsCloneTransformer.ts +9 -9
  194. package/src/transformers/features/misc/MiscCreateIsPruneTransformer.ts +9 -9
  195. package/src/transformers/features/misc/MiscCreatePruneTransformer.ts +9 -9
  196. package/src/transformers/features/misc/MiscCreateValidateCloneTransformer.ts +10 -10
  197. package/src/transformers/features/misc/MiscCreateValidatePruneTransformer.ts +10 -10
  198. package/src/transformers/features/misc/MiscIsCloneTransformer.ts +9 -9
  199. package/src/transformers/features/misc/MiscIsPruneTransformer.ts +9 -9
  200. package/src/transformers/features/misc/MiscLiteralsTransformer.ts +28 -28
  201. package/src/transformers/features/misc/MiscPruneTransformer.ts +9 -9
  202. package/src/transformers/features/misc/MiscValidateCloneTransformer.ts +10 -10
  203. package/src/transformers/features/misc/MiscValidatePruneTransformer.ts +10 -10
  204. package/src/transformers/features/protobuf/CreateProtobufAssertDecodeTransformer.ts +12 -12
  205. package/src/transformers/features/protobuf/CreateProtobufAssertEncodeTransformer.ts +12 -12
  206. package/src/transformers/features/protobuf/CreateProtobufDecodeTransformer.ts +9 -9
  207. package/src/transformers/features/protobuf/CreateProtobufEncodeTransformer.ts +9 -9
  208. package/src/transformers/features/protobuf/CreateProtobufIsDecodeTransformer.ts +12 -12
  209. package/src/transformers/features/protobuf/CreateProtobufIsEncodeTransformer.ts +12 -12
  210. package/src/transformers/features/protobuf/CreateProtobufValidateDecodeTransformer.ts +12 -12
  211. package/src/transformers/features/protobuf/CreateProtobufValidateEncodeTransformer.ts +12 -12
  212. package/src/transformers/features/protobuf/ProtobufAssertDecodeTransformer.ts +10 -10
  213. package/src/transformers/features/protobuf/ProtobufAssertEncodeTransformer.ts +10 -10
  214. package/src/transformers/features/protobuf/ProtobufDecodeTransformer.ts +10 -10
  215. package/src/transformers/features/protobuf/ProtobufEncodeTransformer.ts +10 -10
  216. package/src/transformers/features/protobuf/ProtobufIsDecodeTransformer.ts +10 -10
  217. package/src/transformers/features/protobuf/ProtobufIsEncodeTransformer.ts +10 -10
  218. package/src/transformers/features/protobuf/ProtobufMessageTransformer.ts +31 -31
  219. package/src/transformers/features/protobuf/ProtobufValidateDecodeTransformer.ts +12 -12
  220. package/src/transformers/features/protobuf/ProtobufValidateEncodeTransformer.ts +12 -12
  221. package/src/utils/NameEncoder.ts +32 -32
@@ -1,921 +1,920 @@
1
- import ts from "typescript";
2
-
3
- import { ExpressionFactory } from "../../factories/ExpressionFactory";
4
- import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
- import { MetadataCollection } from "../../factories/MetadataCollection";
6
- import { ProtobufFactory } from "../../factories/ProtobufFactory";
7
- import { StatementFactory } from "../../factories/StatementFactory";
8
- import { TypeFactory } from "../../factories/TypeFactory";
9
-
10
- import { IJsDocTagInfo } from "../../schemas/metadata/IJsDocTagInfo";
11
- import { IMetadataTag } from "../../schemas/metadata/IMetadataTag";
12
- import { Metadata } from "../../schemas/metadata/Metadata";
13
- import { MetadataArray } from "../../schemas/metadata/MetadataArray";
14
- import { MetadataObject } from "../../schemas/metadata/MetadataObject";
15
- import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
16
- import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
17
-
18
- import { IProject } from "../../transformers/IProject";
19
-
20
- import { Atomic } from "../../typings/Atomic";
21
-
22
- import { FeatureProgrammer } from "../FeatureProgrammer";
23
- import { IsProgrammer } from "../IsProgrammer";
24
- import { FunctionImporter } from "../helpers/FunctionImporeter";
25
- import { ProtobufUtil } from "../helpers/ProtobufUtil";
26
- import { ProtobufWire } from "../helpers/ProtobufWire";
27
- import { UnionExplorer } from "../helpers/UnionExplorer";
28
- import { UnionPredicator } from "../helpers/UnionPredicator";
29
- import { decode_union_object } from "../internal/decode_union_object";
30
-
31
- export namespace ProtobufEncodeProgrammer {
32
- export const write =
33
- (project: IProject) =>
34
- (modulo: ts.LeftHandSideExpression) =>
35
- (type: ts.Type, name?: string): ts.ArrowFunction => {
36
- const importer = new FunctionImporter();
37
- const collection = new MetadataCollection();
38
- const meta: Metadata = ProtobufFactory.metadata(modulo.getText())(
39
- project.checker,
40
- )(collection)(type);
41
-
42
- const callEncoder =
43
- (writer: string) => (factory: ts.NewExpression) =>
44
- StatementFactory.constant(
45
- writer,
46
- ts.factory.createCallExpression(
47
- ts.factory.createIdentifier("encoder"),
48
- undefined,
49
- [factory],
50
- ),
51
- );
52
-
53
- const block: ts.Statement[] = [
54
- StatementFactory.constant(
55
- "encoder",
56
- write_encoder(project)(importer)(collection)(meta),
57
- ),
58
- callEncoder("sizer")(
59
- ts.factory.createNewExpression(
60
- importer.use("Sizer"),
61
- undefined,
62
- [],
63
- ),
64
- ),
65
- callEncoder("writer")(
66
- ts.factory.createNewExpression(
67
- importer.use("Writer"),
68
- undefined,
69
- [ts.factory.createIdentifier("sizer")],
70
- ),
71
- ),
72
- ts.factory.createReturnStatement(
73
- ts.factory.createCallExpression(
74
- IdentifierFactory.access(WRITER())("buffer"),
75
- undefined,
76
- undefined,
77
- ),
78
- ),
79
- ];
80
-
81
- // @todo
82
- return ts.factory.createArrowFunction(
83
- undefined,
84
- undefined,
85
- [
86
- IdentifierFactory.parameter(
87
- "input",
88
- ts.factory.createTypeReferenceNode(
89
- name ??
90
- TypeFactory.getFullName(project.checker)(type),
91
- ),
92
- ),
93
- ],
94
- ts.factory.createTypeReferenceNode("Uint8Array"),
95
- undefined,
96
- ts.factory.createBlock(
97
- [...importer.declare(modulo, false), ...block],
98
- true,
99
- ),
100
- );
101
- };
102
-
103
- const write_encoder =
104
- (project: IProject) =>
105
- (importer: FunctionImporter) =>
106
- (collection: MetadataCollection) =>
107
- (meta: Metadata): ts.ArrowFunction => {
108
- const functors = collection
109
- .objects()
110
- .filter((obj) => obj._Messagable())
111
- .map((obj) =>
112
- StatementFactory.constant(
113
- `${PREFIX}o${obj.index}`,
114
- write_object_function(project)(importer)(
115
- ts.factory.createIdentifier("input"),
116
- obj,
117
- {
118
- source: "function",
119
- from: "object",
120
- tracable: false,
121
- postfix: "",
122
- },
123
- ),
124
- ),
125
- );
126
- const main = decode(project)(importer)(null)(
127
- ts.factory.createIdentifier("input"),
128
- meta,
129
- {
130
- source: "top",
131
- from: "top",
132
- tracable: false,
133
- postfix: "",
134
- },
135
- [],
136
- );
137
- return ts.factory.createArrowFunction(
138
- undefined,
139
- undefined,
140
- [IdentifierFactory.parameter("writer")],
141
- TypeFactory.keyword("any"),
142
- undefined,
143
- ts.factory.createBlock(
144
- [
145
- ...importer.declareUnions(),
146
- ...functors,
147
- ...IsProgrammer.write_function_statements(project)(
148
- importer,
149
- )(collection),
150
- ...main.statements,
151
- ts.factory.createReturnStatement(
152
- ts.factory.createIdentifier("writer"),
153
- ),
154
- ],
155
- true,
156
- ),
157
- );
158
- };
159
-
160
- const write_object_function =
161
- (project: IProject) =>
162
- (importer: FunctionImporter) =>
163
- (
164
- input: ts.Expression,
165
- obj: MetadataObject,
166
- explore: FeatureProgrammer.IExplore,
167
- ): ts.ArrowFunction => {
168
- let index: number = 1;
169
- const body: ts.Statement[] = obj.properties
170
- .map((p) => {
171
- const block = decode(project)(importer)(index)(
172
- IdentifierFactory.access(input)(
173
- p.key.getSoleLiteral()!,
174
- ),
175
- p.value,
176
- explore,
177
- p.tags,
178
- );
179
- index += p.value.binarySize();
180
- return [
181
- ts.factory.createExpressionStatement(
182
- ts.factory.createIdentifier(
183
- `// property "${p.key.getSoleLiteral()!}"`,
184
- ),
185
- ),
186
- ...block.statements,
187
- ];
188
- })
189
- .flat();
190
-
191
- return ts.factory.createArrowFunction(
192
- undefined,
193
- undefined,
194
- [IdentifierFactory.parameter("input")],
195
- TypeFactory.keyword("any"),
196
- undefined,
197
- ts.factory.createBlock(body, true),
198
- );
199
- };
200
-
201
- /* -----------------------------------------------------------
202
- DECODERS
203
- ----------------------------------------------------------- */
204
- const decode =
205
- (project: IProject) =>
206
- (importer: FunctionImporter) =>
207
- (index: number | null) =>
208
- (
209
- input: ts.Expression,
210
- meta: Metadata,
211
- explore: FeatureProgrammer.IExplore,
212
- tags: IMetadataTag[],
213
- ): ts.Block => {
214
- const wrapper: (block: ts.Block) => ts.Block =
215
- meta.isRequired() && meta.nullable === false
216
- ? (block) => block
217
- : meta.isRequired() === false && meta.nullable === false
218
- ? (block) =>
219
- ts.factory.createBlock(
220
- [
221
- ts.factory.createIfStatement(
222
- ts.factory.createLogicalAnd(
223
- ts.factory.createInequality(
224
- ts.factory.createIdentifier(
225
- "undefined",
226
- ),
227
- input,
228
- ),
229
- ts.factory.createInequality(
230
- ts.factory.createNull(),
231
- input,
232
- ),
233
- ),
234
- block,
235
- ),
236
- ],
237
- true,
238
- )
239
- : meta.isRequired() === false
240
- ? (block) =>
241
- ts.factory.createBlock(
242
- [
243
- ts.factory.createIfStatement(
244
- ts.factory.createInequality(
245
- ts.factory.createIdentifier(
246
- "undefined",
247
- ),
248
- input,
249
- ),
250
- block,
251
- ),
252
- ],
253
- true,
254
- )
255
- : (block) =>
256
- ts.factory.createBlock(
257
- [
258
- ts.factory.createIfStatement(
259
- ts.factory.createInequality(
260
- ts.factory.createNull(),
261
- input,
262
- ),
263
- block,
264
- ),
265
- ],
266
- true,
267
- );
268
-
269
- // STARTS FROM ATOMIC TYPES
270
- const unions: IUnion[] = ProtobufUtil.atomics(meta).map((type) => ({
271
- type,
272
- wire: () => null!,
273
- is: () =>
274
- ts.factory.createStrictEquality(
275
- ts.factory.createStringLiteral(type),
276
- ts.factory.createTypeOfExpression(input),
277
- ),
278
- value: (index) => decode_atomic(index!)(input, type, tags),
279
- }));
280
-
281
- // CONSIDER BYTES
282
- if (meta.natives.length)
283
- unions.push({
284
- type: "bytes",
285
- is: () =>
286
- ExpressionFactory.isInstanceOf("Uint8Array")(input),
287
- value: (index) => decode_bytes("bytes")(index!)(input),
288
- });
289
-
290
- // CONSIDER MAPS
291
- if (meta.maps.length)
292
- unions.push({
293
- type: "map",
294
- is: () => ExpressionFactory.isInstanceOf("Map")(input),
295
- value: (index) =>
296
- decode_map(project)(importer)(index!)(
297
- input,
298
- meta.maps[0]!,
299
- {
300
- ...explore,
301
- from: "array",
302
- },
303
- tags,
304
- ),
305
- });
306
-
307
- // CONSIDER ARRAYS
308
- if (meta.arrays.length)
309
- unions.push({
310
- type: "array",
311
- is: () => ExpressionFactory.isArray(input),
312
- value: (index) =>
313
- explore_arrays(project)(importer)(index!)(
314
- input,
315
- meta.arrays,
316
- {
317
- ...explore,
318
- from: "array",
319
- },
320
- tags,
321
- ),
322
- });
323
-
324
- // CONSIDER OBJECTS
325
- if (meta.objects.length)
326
- unions.push({
327
- type: "object",
328
- is: () =>
329
- ExpressionFactory.isObject({
330
- checkNull: true,
331
- checkArray: false,
332
- })(input),
333
- value: (index) =>
334
- explore_objects(project)(importer)(0)(index)(
335
- input,
336
- meta.objects,
337
- {
338
- ...explore,
339
- from: "object",
340
- },
341
- tags,
342
- ),
343
- });
344
-
345
- // RETURNS
346
- if (unions.length === 1) return wrapper(unions[0]!.value(index));
347
- else
348
- return wrapper(
349
- iterate(importer)(index)(unions)(meta.getName())(input),
350
- );
351
- };
352
-
353
- const iterate =
354
- (importer: FunctionImporter) =>
355
- (index: number | null) =>
356
- (unions: IUnion[]) =>
357
- (expected: string) =>
358
- (input: ts.Expression) =>
359
- ts.factory.createBlock(
360
- [
361
- unions
362
- .map((u, i) =>
363
- ts.factory.createIfStatement(
364
- u.is(),
365
- u.value(index ? index + i : null),
366
- i === unions.length - 1
367
- ? create_throw_error(importer)(expected)(
368
- input,
369
- )
370
- : undefined,
371
- ),
372
- )
373
- .reverse()
374
- .reduce((a, b) =>
375
- ts.factory.createIfStatement(
376
- b.expression,
377
- b.thenStatement,
378
- a,
379
- ),
380
- ),
381
- ],
382
- true,
383
- );
384
-
385
- const decode_map =
386
- (project: IProject) =>
387
- (importer: FunctionImporter) =>
388
- (index: number) =>
389
- (
390
- input: ts.Expression,
391
- map: Metadata.Entry,
392
- explore: FeatureProgrammer.IExplore,
393
- tags: IMetadataTag[],
394
- ): ts.Block => {
395
- const each: ts.Statement[] = [
396
- ts.factory.createExpressionStatement(
397
- decode_tag(ProtobufWire.LEN)(index),
398
- ),
399
- ts.factory.createExpressionStatement(
400
- ts.factory.createCallExpression(
401
- IdentifierFactory.access(WRITER())("fork"),
402
- undefined,
403
- undefined,
404
- ),
405
- ),
406
- ...decode(project)(importer)(1)(
407
- ts.factory.createIdentifier("key"),
408
- map.key,
409
- explore,
410
- [],
411
- ).statements,
412
- ...decode(project)(importer)(2)(
413
- ts.factory.createIdentifier("value"),
414
- map.value,
415
- explore,
416
- tags,
417
- ).statements,
418
- ts.factory.createExpressionStatement(
419
- ts.factory.createCallExpression(
420
- IdentifierFactory.access(WRITER())("ldelim"),
421
- undefined,
422
- undefined,
423
- ),
424
- ),
425
- ];
426
- return ts.factory.createBlock(
427
- [
428
- ts.factory.createForOfStatement(
429
- undefined,
430
- StatementFactory.entry("key")("value"),
431
- input,
432
- ts.factory.createBlock(each),
433
- ),
434
- ],
435
- true,
436
- );
437
- };
438
-
439
- const decode_object =
440
- (project: IProject) =>
441
- (importer: FunctionImporter) =>
442
- (index: number | null) =>
443
- (
444
- input: ts.Expression,
445
- object: MetadataObject,
446
- explore: FeatureProgrammer.IExplore,
447
- tags: IMetadataTag[],
448
- ): ts.Block => {
449
- const top: MetadataProperty = object.properties[0]!;
450
- if (top.key.isSoleLiteral() === false)
451
- return decode_map(project)(importer)(index!)(
452
- ts.factory.createCallExpression(
453
- ts.factory.createIdentifier("Object.entries"),
454
- [],
455
- [input],
456
- ),
457
- top,
458
- explore,
459
- tags,
460
- );
461
- return ts.factory.createBlock(
462
- [
463
- ...(index !== null
464
- ? [
465
- decode_tag(ProtobufWire.LEN)(index),
466
- ts.factory.createCallExpression(
467
- IdentifierFactory.access(WRITER())("fork"),
468
- undefined,
469
- undefined,
470
- ),
471
- ]
472
- : []),
473
- ts.factory.createCallExpression(
474
- ts.factory.createIdentifier(
475
- `${PREFIX}o${object.index}`,
476
- ),
477
- [],
478
- [input],
479
- ),
480
- ...(index !== null
481
- ? [
482
- ts.factory.createCallExpression(
483
- IdentifierFactory.access(WRITER())("ldelim"),
484
- undefined,
485
- undefined,
486
- ),
487
- ]
488
- : []),
489
- ].map((expr) => ts.factory.createExpressionStatement(expr)),
490
- true,
491
- );
492
- };
493
-
494
- const decode_array =
495
- (project: IProject) =>
496
- (importer: FunctionImporter) =>
497
- (index: number) =>
498
- (
499
- input: ts.Expression,
500
- array: MetadataArray,
501
- explore: FeatureProgrammer.IExplore,
502
- tags: IMetadataTag[],
503
- ): ts.Block => {
504
- const wire = get_standalone_wire(array.value, tags);
505
- const forLoop = (index: number | null) =>
506
- ts.factory.createForOfStatement(
507
- undefined,
508
- ts.factory.createVariableDeclarationList(
509
- [ts.factory.createVariableDeclaration("elem")],
510
- ts.NodeFlags.Const,
511
- ),
512
- input,
513
- decode(project)(importer)(index)(
514
- ts.factory.createIdentifier("elem"),
515
- array.value,
516
- explore,
517
- tags,
518
- ),
519
- );
520
- const length = (block: ts.Block) =>
521
- ts.factory.createBlock(
522
- [
523
- ts.factory.createIfStatement(
524
- ts.factory.createStrictInequality(
525
- ts.factory.createNumericLiteral(0),
526
- IdentifierFactory.access(input)("length"),
527
- ),
528
- block,
529
- ),
530
- ],
531
- true,
532
- );
533
-
534
- if (wire === ProtobufWire.LEN)
535
- return length(ts.factory.createBlock([forLoop(index)], true));
536
- return length(
537
- ts.factory.createBlock(
538
- [
539
- ts.factory.createExpressionStatement(
540
- decode_tag(ProtobufWire.LEN)(index),
541
- ),
542
- ts.factory.createExpressionStatement(
543
- ts.factory.createCallExpression(
544
- IdentifierFactory.access(WRITER())("fork"),
545
- undefined,
546
- undefined,
547
- ),
548
- ),
549
- forLoop(null),
550
- ts.factory.createExpressionStatement(
551
- ts.factory.createCallExpression(
552
- IdentifierFactory.access(WRITER())("ldelim"),
553
- undefined,
554
- undefined,
555
- ),
556
- ),
557
- ],
558
- true,
559
- ),
560
- );
561
- };
562
-
563
- const decode_atomic =
564
- (index: number | null) =>
565
- (
566
- input: ts.Expression,
567
- atomic: Atomic.Literal,
568
- tags: IMetadataTag[],
569
- ): ts.Block => {
570
- if (atomic === "string")
571
- return decode_bytes("string")(index!)(input);
572
-
573
- const out =
574
- (wire: ProtobufWire) =>
575
- (method: string): ts.Block =>
576
- ts.factory.createBlock(
577
- [
578
- ...(index !== null
579
- ? [decode_tag(wire)(index)]
580
- : []),
581
- ts.factory.createCallExpression(
582
- IdentifierFactory.access(WRITER())(method),
583
- undefined,
584
- [input],
585
- ),
586
- ].map((exp) =>
587
- ts.factory.createExpressionStatement(exp),
588
- ),
589
- true,
590
- );
591
- if (atomic === "boolean") return out(ProtobufWire.VARIANT)("bool");
592
-
593
- const type: IMetadataTag.INumberType | undefined = tags.find(
594
- (tag) => tag.kind === "type",
595
- ) as IMetadataTag.INumberType | undefined;
596
- if (atomic === "bigint")
597
- return out(ProtobufWire.VARIANT)(
598
- type?.value === "uint64" ? "uint64" : "int64",
599
- );
600
- else if (type === undefined || type?.value === "double")
601
- return out(ProtobufWire.I64)("double");
602
- else if (type.value === "float")
603
- return out(ProtobufWire.I32)("float");
604
- return out(ProtobufWire.VARIANT)(
605
- type.value === "uint32" || type.value === "uint"
606
- ? "uint32"
607
- : type.value === "int32" || type.value === "int"
608
- ? "int32"
609
- : type.value === "int64"
610
- ? "int64"
611
- : "uint64",
612
- );
613
- };
614
-
615
- const decode_bytes =
616
- (method: "bytes" | "string") =>
617
- (index: number) =>
618
- (input: ts.Expression): ts.Block =>
619
- ts.factory.createBlock(
620
- [
621
- decode_tag(ProtobufWire.LEN)(index),
622
- ts.factory.createCallExpression(
623
- IdentifierFactory.access(WRITER())(method),
624
- undefined,
625
- [input],
626
- ),
627
- ].map((expr) => ts.factory.createExpressionStatement(expr)),
628
- true,
629
- );
630
-
631
- const decode_tag =
632
- (wire: ProtobufWire) =>
633
- (index: number): ts.CallExpression =>
634
- ts.factory.createCallExpression(
635
- IdentifierFactory.access(WRITER())("uint32"),
636
- undefined,
637
- [ts.factory.createNumericLiteral((index << 3) | wire)],
638
- );
639
-
640
- const get_standalone_wire = (
641
- meta: Metadata,
642
- tags: IMetadataTag[],
643
- ): ProtobufWire => {
644
- if (
645
- meta.arrays.length ||
646
- meta.objects.length ||
647
- meta.maps.length ||
648
- meta.natives.length
649
- )
650
- return ProtobufWire.LEN;
651
-
652
- const v = ProtobufUtil.atomics(meta)[0]!;
653
- if (v === "string") return ProtobufWire.LEN;
654
- else if (v === "boolean" || v === "bigint") return ProtobufWire.VARIANT;
655
-
656
- const type: IMetadataTag.INumberType | undefined = tags.find(
657
- (t) => t.kind === "type",
658
- ) as IMetadataTag.INumberType | undefined;
659
- if (type === undefined) return ProtobufWire.I64;
660
- else if (type.value === "float") return ProtobufWire.I32;
661
- return ProtobufWire.VARIANT;
662
- };
663
-
664
- /* -----------------------------------------------------------
665
- EXPLORERS
666
- ----------------------------------------------------------- */
667
- const explore_objects =
668
- (project: IProject) =>
669
- (importer: FunctionImporter) =>
670
- (level: number) =>
671
- (index: number | null) =>
672
- (
673
- input: ts.Expression,
674
- targets: MetadataObject[],
675
- explore: FeatureProgrammer.IExplore,
676
- tags: IMetadataTag[],
677
- ): ts.Block => {
678
- if (targets.length === 1)
679
- return decode_object(project)(importer)(index)(
680
- input,
681
- targets[0]!,
682
- explore,
683
- tags,
684
- );
685
-
686
- const expected: string = `(${targets
687
- .map((t) => t.name)
688
- .join(" | ")})`;
689
-
690
- // POSSIBLE TO SPECIALIZE?
691
- const specList = UnionPredicator.object(targets);
692
- if (specList.length === 0) {
693
- const condition: ts.Expression = decode_union_object(
694
- IsProgrammer.decode_object(importer),
695
- )((i, o, e) =>
696
- ExpressionFactory.selfCall(
697
- decode_object(project)(importer)(index)(i, o, e, tags),
698
- ),
699
- )((expr) => expr)((value, expected) =>
700
- create_throw_error(importer)(expected)(value),
701
- )(input, targets, explore);
702
- return StatementFactory.block(condition);
703
- }
704
- const remained: MetadataObject[] = targets.filter(
705
- (t) => specList.find((s) => s.object === t) === undefined,
706
- );
707
-
708
- // DO SPECIALIZE
709
- const condition: ts.IfStatement = specList
710
- .filter((spec) => spec.property.key.getSoleLiteral() !== null)
711
- .map((spec, i, array) => {
712
- const key: string = spec.property.key.getSoleLiteral()!;
713
- const accessor: ts.Expression =
714
- IdentifierFactory.access(input)(key);
715
- const pred: ts.Expression = spec.neighbour
716
- ? IsProgrammer.decode(project)(importer)(
717
- accessor,
718
- spec.property.value,
719
- {
720
- ...explore,
721
- tracable: false,
722
- postfix: IdentifierFactory.postfix(key),
723
- },
724
- tags,
725
- [],
726
- )
727
- : ExpressionFactory.isRequired(accessor);
728
- return ts.factory.createIfStatement(
729
- pred,
730
- ts.factory.createReturnStatement(
731
- ExpressionFactory.selfCall(
732
- decode_object(project)(importer)(index)(
733
- input,
734
- spec.object,
735
- explore,
736
- tags,
737
- ),
738
- ),
739
- ),
740
- i === array.length - 1
741
- ? remained.length
742
- ? ts.factory.createReturnStatement(
743
- ExpressionFactory.selfCall(
744
- explore_objects(project)(importer)(
745
- level + 1,
746
- )(index)(
747
- input,
748
- remained,
749
- explore,
750
- tags,
751
- ),
752
- ),
753
- )
754
- : create_throw_error(importer)(expected)(input)
755
- : undefined,
756
- );
757
- })
758
- .reverse()
759
- .reduce((a, b) =>
760
- ts.factory.createIfStatement(
761
- b.expression,
762
- b.thenStatement,
763
- a,
764
- ),
765
- );
766
-
767
- // RETURNS WITH CONDITIONS
768
- return ts.factory.createBlock([condition], true);
769
- };
770
-
771
- const explore_arrays =
772
- (project: IProject) =>
773
- (importer: FunctionImporter) =>
774
- (index: number) =>
775
- (
776
- input: ts.Expression,
777
- elements: MetadataArray[],
778
- explore: FeatureProgrammer.IExplore,
779
- tags: IMetadataTag[],
780
- ): ts.Block =>
781
- elements.length === 1
782
- ? decode_array(project)(importer)(index)(
783
- input,
784
- elements[0]!,
785
- explore,
786
- tags,
787
- )
788
- : explore_array_like_union_types(importer)(
789
- UnionExplorer.array({
790
- checker: IsProgrammer.decode(project)(importer),
791
- decoder: (input, target, explore, tags) =>
792
- ts.factory.createCallExpression(
793
- ts.factory.createArrowFunction(
794
- undefined,
795
- undefined,
796
- [],
797
- undefined,
798
- undefined,
799
- decode_array(project)(importer)(index)(
800
- input,
801
- target,
802
- explore,
803
- tags,
804
- ),
805
- ),
806
- undefined,
807
- undefined,
808
- ),
809
- empty: ts.factory.createStringLiteral("[]"),
810
- success: ts.factory.createTrue(),
811
- failure: (input, expected) =>
812
- create_throw_error(importer)(expected)(input),
813
- }),
814
- )(input, elements, explore, tags);
815
-
816
- const explore_array_like_union_types =
817
- (importer: FunctionImporter) =>
818
- <T extends MetadataArray | MetadataTuple>(
819
- factory: (
820
- parameters: ts.ParameterDeclaration[],
821
- ) => (
822
- input: ts.Expression,
823
- elements: T[],
824
- explore: FeatureProgrammer.IExplore,
825
- tags: IMetadataTag[],
826
- jsDocTags: IJsDocTagInfo[],
827
- ) => ts.ArrowFunction,
828
- ) =>
829
- (
830
- input: ts.Expression,
831
- elements: T[],
832
- explore: FeatureProgrammer.IExplore,
833
- tags: IMetadataTag[],
834
- ): ts.Block => {
835
- const arrow =
836
- (parameters: ts.ParameterDeclaration[]) =>
837
- (explore: FeatureProgrammer.IExplore) =>
838
- (input: ts.Expression): ts.ArrowFunction =>
839
- factory(parameters)(input, elements, explore, tags, []);
840
- if (elements.every((e) => e.recursive === false))
841
- return StatementFactory.block(
842
- ts.factory.createCallExpression(
843
- arrow([])(explore)(input),
844
- undefined,
845
- [],
846
- ),
847
- );
848
-
849
- explore = {
850
- ...explore,
851
- source: "function",
852
- from: "array",
853
- };
854
- return StatementFactory.block(
855
- ts.factory.createCallExpression(
856
- ts.factory.createIdentifier(
857
- importer.emplaceUnion(
858
- PREFIX,
859
- elements.map((e) => e.name).join(" | "),
860
- () =>
861
- arrow(
862
- FeatureProgrammer.parameterDeclarations(
863
- CONFIG,
864
- )(TypeFactory.keyword("any"))(
865
- ts.factory.createIdentifier("input"),
866
- ),
867
- )({
868
- ...explore,
869
- postfix: "",
870
- })(ts.factory.createIdentifier("input")),
871
- ),
872
- ),
873
- undefined,
874
- FeatureProgrammer.argumentsArray(CONFIG)(explore)(input),
875
- ),
876
- );
877
- };
878
-
879
- /* -----------------------------------------------------------
880
- CONFIGURATIONS
881
- ----------------------------------------------------------- */
882
- const PREFIX = "$pe";
883
- const CONFIG = {
884
- path: false,
885
- trace: false,
886
- };
887
-
888
- const create_throw_error =
889
- (importer: FunctionImporter) =>
890
- (expected: string) =>
891
- (value: ts.Expression) =>
892
- ts.factory.createExpressionStatement(
893
- ts.factory.createCallExpression(
894
- importer.use("throws"),
895
- [],
896
- [
897
- ts.factory.createObjectLiteralExpression(
898
- [
899
- ts.factory.createPropertyAssignment(
900
- "expected",
901
- ts.factory.createStringLiteral(expected),
902
- ),
903
- ts.factory.createPropertyAssignment(
904
- "value",
905
- value,
906
- ),
907
- ],
908
- true,
909
- ),
910
- ],
911
- ),
912
- );
913
- }
914
-
915
- const WRITER = () => ts.factory.createIdentifier("writer");
916
-
917
- interface IUnion {
918
- type: string;
919
- is: () => ts.Expression;
920
- value: (index: number | null) => ts.Block;
921
- }
1
+ import ts from "typescript";
2
+
3
+ import { ExpressionFactory } from "../../factories/ExpressionFactory";
4
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
+ import { MetadataCollection } from "../../factories/MetadataCollection";
6
+ import { ProtobufFactory } from "../../factories/ProtobufFactory";
7
+ import { StatementFactory } from "../../factories/StatementFactory";
8
+ import { TypeFactory } from "../../factories/TypeFactory";
9
+
10
+ import { IJsDocTagInfo } from "../../schemas/metadata/IJsDocTagInfo";
11
+ import { IMetadataTag } from "../../schemas/metadata/IMetadataTag";
12
+ import { Metadata } from "../../schemas/metadata/Metadata";
13
+ import { MetadataArray } from "../../schemas/metadata/MetadataArray";
14
+ import { MetadataObject } from "../../schemas/metadata/MetadataObject";
15
+ import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
16
+ import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
17
+
18
+ import { IProject } from "../../transformers/IProject";
19
+
20
+ import { Atomic } from "../../typings/Atomic";
21
+
22
+ import { FeatureProgrammer } from "../FeatureProgrammer";
23
+ import { IsProgrammer } from "../IsProgrammer";
24
+ import { FunctionImporter } from "../helpers/FunctionImporeter";
25
+ import { ProtobufUtil } from "../helpers/ProtobufUtil";
26
+ import { ProtobufWire } from "../helpers/ProtobufWire";
27
+ import { UnionExplorer } from "../helpers/UnionExplorer";
28
+ import { UnionPredicator } from "../helpers/UnionPredicator";
29
+ import { decode_union_object } from "../internal/decode_union_object";
30
+
31
+ export namespace ProtobufEncodeProgrammer {
32
+ export const write =
33
+ (project: IProject) =>
34
+ (modulo: ts.LeftHandSideExpression) =>
35
+ (type: ts.Type, name?: string): ts.ArrowFunction => {
36
+ const importer = new FunctionImporter();
37
+ const collection = new MetadataCollection();
38
+ const meta: Metadata = ProtobufFactory.metadata(modulo.getText())(
39
+ project.checker,
40
+ )(collection)(type);
41
+
42
+ const callEncoder =
43
+ (writer: string) => (factory: ts.NewExpression) =>
44
+ StatementFactory.constant(
45
+ writer,
46
+ ts.factory.createCallExpression(
47
+ ts.factory.createIdentifier("encoder"),
48
+ undefined,
49
+ [factory],
50
+ ),
51
+ );
52
+
53
+ const block: ts.Statement[] = [
54
+ StatementFactory.constant(
55
+ "encoder",
56
+ write_encoder(project)(importer)(collection)(meta),
57
+ ),
58
+ callEncoder("sizer")(
59
+ ts.factory.createNewExpression(
60
+ importer.use("Sizer"),
61
+ undefined,
62
+ [],
63
+ ),
64
+ ),
65
+ callEncoder("writer")(
66
+ ts.factory.createNewExpression(
67
+ importer.use("Writer"),
68
+ undefined,
69
+ [ts.factory.createIdentifier("sizer")],
70
+ ),
71
+ ),
72
+ ts.factory.createReturnStatement(
73
+ ts.factory.createCallExpression(
74
+ IdentifierFactory.access(WRITER())("buffer"),
75
+ undefined,
76
+ undefined,
77
+ ),
78
+ ),
79
+ ];
80
+
81
+ // @todo
82
+ return ts.factory.createArrowFunction(
83
+ undefined,
84
+ undefined,
85
+ [
86
+ IdentifierFactory.parameter(
87
+ "input",
88
+ ts.factory.createTypeReferenceNode(
89
+ name ??
90
+ TypeFactory.getFullName(project.checker)(type),
91
+ ),
92
+ ),
93
+ ],
94
+ ts.factory.createTypeReferenceNode("Uint8Array"),
95
+ undefined,
96
+ ts.factory.createBlock(
97
+ [...importer.declare(modulo, false), ...block],
98
+ true,
99
+ ),
100
+ );
101
+ };
102
+
103
+ const write_encoder =
104
+ (project: IProject) =>
105
+ (importer: FunctionImporter) =>
106
+ (collection: MetadataCollection) =>
107
+ (meta: Metadata): ts.ArrowFunction => {
108
+ const functors = collection
109
+ .objects()
110
+ .filter((obj) => obj._Messagable())
111
+ .map((obj) =>
112
+ StatementFactory.constant(
113
+ `${PREFIX}o${obj.index}`,
114
+ write_object_function(project)(importer)(
115
+ ts.factory.createIdentifier("input"),
116
+ obj,
117
+ {
118
+ source: "function",
119
+ from: "object",
120
+ tracable: false,
121
+ postfix: "",
122
+ },
123
+ ),
124
+ ),
125
+ );
126
+ const main = decode(project)(importer)(null)(
127
+ ts.factory.createIdentifier("input"),
128
+ meta,
129
+ {
130
+ source: "top",
131
+ from: "top",
132
+ tracable: false,
133
+ postfix: "",
134
+ },
135
+ [],
136
+ );
137
+ return ts.factory.createArrowFunction(
138
+ undefined,
139
+ undefined,
140
+ [IdentifierFactory.parameter("writer")],
141
+ TypeFactory.keyword("any"),
142
+ undefined,
143
+ ts.factory.createBlock(
144
+ [
145
+ ...importer.declareUnions(),
146
+ ...functors,
147
+ ...IsProgrammer.write_function_statements(project)(
148
+ importer,
149
+ )(collection),
150
+ ...main.statements,
151
+ ts.factory.createReturnStatement(
152
+ ts.factory.createIdentifier("writer"),
153
+ ),
154
+ ],
155
+ true,
156
+ ),
157
+ );
158
+ };
159
+
160
+ const write_object_function =
161
+ (project: IProject) =>
162
+ (importer: FunctionImporter) =>
163
+ (
164
+ input: ts.Expression,
165
+ obj: MetadataObject,
166
+ explore: FeatureProgrammer.IExplore,
167
+ ): ts.ArrowFunction => {
168
+ let index: number = 1;
169
+ const body: ts.Statement[] = obj.properties
170
+ .map((p) => {
171
+ const block = decode(project)(importer)(index)(
172
+ IdentifierFactory.access(input)(
173
+ p.key.getSoleLiteral()!,
174
+ ),
175
+ p.value,
176
+ explore,
177
+ p.tags,
178
+ );
179
+ index += p.value.binarySize();
180
+ return [
181
+ ts.factory.createExpressionStatement(
182
+ ts.factory.createIdentifier(
183
+ `// property "${p.key.getSoleLiteral()!}"`,
184
+ ),
185
+ ),
186
+ ...block.statements,
187
+ ];
188
+ })
189
+ .flat();
190
+
191
+ return ts.factory.createArrowFunction(
192
+ undefined,
193
+ undefined,
194
+ [IdentifierFactory.parameter("input")],
195
+ TypeFactory.keyword("any"),
196
+ undefined,
197
+ ts.factory.createBlock(body, true),
198
+ );
199
+ };
200
+
201
+ /* -----------------------------------------------------------
202
+ DECODERS
203
+ ----------------------------------------------------------- */
204
+ const decode =
205
+ (project: IProject) =>
206
+ (importer: FunctionImporter) =>
207
+ (index: number | null) =>
208
+ (
209
+ input: ts.Expression,
210
+ meta: Metadata,
211
+ explore: FeatureProgrammer.IExplore,
212
+ tags: IMetadataTag[],
213
+ ): ts.Block => {
214
+ const wrapper: (block: ts.Block) => ts.Block =
215
+ meta.isRequired() && meta.nullable === false
216
+ ? (block) => block
217
+ : meta.isRequired() === false && meta.nullable === false
218
+ ? (block) =>
219
+ ts.factory.createBlock(
220
+ [
221
+ ts.factory.createIfStatement(
222
+ ts.factory.createLogicalAnd(
223
+ ts.factory.createInequality(
224
+ ts.factory.createIdentifier(
225
+ "undefined",
226
+ ),
227
+ input,
228
+ ),
229
+ ts.factory.createInequality(
230
+ ts.factory.createNull(),
231
+ input,
232
+ ),
233
+ ),
234
+ block,
235
+ ),
236
+ ],
237
+ true,
238
+ )
239
+ : meta.isRequired() === false
240
+ ? (block) =>
241
+ ts.factory.createBlock(
242
+ [
243
+ ts.factory.createIfStatement(
244
+ ts.factory.createInequality(
245
+ ts.factory.createIdentifier(
246
+ "undefined",
247
+ ),
248
+ input,
249
+ ),
250
+ block,
251
+ ),
252
+ ],
253
+ true,
254
+ )
255
+ : (block) =>
256
+ ts.factory.createBlock(
257
+ [
258
+ ts.factory.createIfStatement(
259
+ ts.factory.createInequality(
260
+ ts.factory.createNull(),
261
+ input,
262
+ ),
263
+ block,
264
+ ),
265
+ ],
266
+ true,
267
+ );
268
+
269
+ // STARTS FROM ATOMIC TYPES
270
+ const unions: IUnion[] = ProtobufUtil.atomics(meta).map((type) => ({
271
+ type,
272
+ wire: () => null!,
273
+ is: () =>
274
+ ts.factory.createStrictEquality(
275
+ ts.factory.createStringLiteral(type),
276
+ ts.factory.createTypeOfExpression(input),
277
+ ),
278
+ value: (index) => decode_atomic(index!)(input, type, tags),
279
+ }));
280
+
281
+ // CONSIDER BYTES
282
+ if (meta.natives.length)
283
+ unions.push({
284
+ type: "bytes",
285
+ is: () =>
286
+ ExpressionFactory.isInstanceOf("Uint8Array")(input),
287
+ value: (index) => decode_bytes("bytes")(index!)(input),
288
+ });
289
+
290
+ // CONSIDER MAPS
291
+ if (meta.maps.length)
292
+ unions.push({
293
+ type: "map",
294
+ is: () => ExpressionFactory.isInstanceOf("Map")(input),
295
+ value: (index) =>
296
+ decode_map(project)(importer)(index!)(
297
+ input,
298
+ meta.maps[0]!,
299
+ {
300
+ ...explore,
301
+ from: "array",
302
+ },
303
+ tags,
304
+ ),
305
+ });
306
+
307
+ // CONSIDER ARRAYS
308
+ if (meta.arrays.length)
309
+ unions.push({
310
+ type: "array",
311
+ is: () => ExpressionFactory.isArray(input),
312
+ value: (index) =>
313
+ explore_arrays(project)(importer)(index!)(
314
+ input,
315
+ meta.arrays,
316
+ {
317
+ ...explore,
318
+ from: "array",
319
+ },
320
+ tags,
321
+ ),
322
+ });
323
+
324
+ // CONSIDER OBJECTS
325
+ if (meta.objects.length)
326
+ unions.push({
327
+ type: "object",
328
+ is: () =>
329
+ ExpressionFactory.isObject({
330
+ checkNull: true,
331
+ checkArray: false,
332
+ })(input),
333
+ value: (index) =>
334
+ explore_objects(project)(importer)(0)(index)(
335
+ input,
336
+ meta.objects,
337
+ {
338
+ ...explore,
339
+ from: "object",
340
+ },
341
+ tags,
342
+ ),
343
+ });
344
+
345
+ // RETURNS
346
+ if (unions.length === 1) return wrapper(unions[0]!.value(index));
347
+ else
348
+ return wrapper(
349
+ iterate(importer)(index)(unions)(meta.getName())(input),
350
+ );
351
+ };
352
+
353
+ const iterate =
354
+ (importer: FunctionImporter) =>
355
+ (index: number | null) =>
356
+ (unions: IUnion[]) =>
357
+ (expected: string) =>
358
+ (input: ts.Expression) =>
359
+ ts.factory.createBlock(
360
+ [
361
+ unions
362
+ .map((u, i) =>
363
+ ts.factory.createIfStatement(
364
+ u.is(),
365
+ u.value(index ? index + i : null),
366
+ i === unions.length - 1
367
+ ? create_throw_error(importer)(expected)(
368
+ input,
369
+ )
370
+ : undefined,
371
+ ),
372
+ )
373
+ .reverse()
374
+ .reduce((a, b) =>
375
+ ts.factory.createIfStatement(
376
+ b.expression,
377
+ b.thenStatement,
378
+ a,
379
+ ),
380
+ ),
381
+ ],
382
+ true,
383
+ );
384
+
385
+ const decode_map =
386
+ (project: IProject) =>
387
+ (importer: FunctionImporter) =>
388
+ (index: number) =>
389
+ (
390
+ input: ts.Expression,
391
+ map: Metadata.Entry,
392
+ explore: FeatureProgrammer.IExplore,
393
+ tags: IMetadataTag[],
394
+ ): ts.Block => {
395
+ const each: ts.Statement[] = [
396
+ ts.factory.createExpressionStatement(
397
+ decode_tag(ProtobufWire.LEN)(index),
398
+ ),
399
+ ts.factory.createExpressionStatement(
400
+ ts.factory.createCallExpression(
401
+ IdentifierFactory.access(WRITER())("fork"),
402
+ undefined,
403
+ undefined,
404
+ ),
405
+ ),
406
+ ...decode(project)(importer)(1)(
407
+ ts.factory.createIdentifier("key"),
408
+ map.key,
409
+ explore,
410
+ [],
411
+ ).statements,
412
+ ...decode(project)(importer)(2)(
413
+ ts.factory.createIdentifier("value"),
414
+ map.value,
415
+ explore,
416
+ tags,
417
+ ).statements,
418
+ ts.factory.createExpressionStatement(
419
+ ts.factory.createCallExpression(
420
+ IdentifierFactory.access(WRITER())("ldelim"),
421
+ undefined,
422
+ undefined,
423
+ ),
424
+ ),
425
+ ];
426
+ return ts.factory.createBlock(
427
+ [
428
+ ts.factory.createForOfStatement(
429
+ undefined,
430
+ StatementFactory.entry("key")("value"),
431
+ input,
432
+ ts.factory.createBlock(each),
433
+ ),
434
+ ],
435
+ true,
436
+ );
437
+ };
438
+
439
+ const decode_object =
440
+ (project: IProject) =>
441
+ (importer: FunctionImporter) =>
442
+ (index: number | null) =>
443
+ (
444
+ input: ts.Expression,
445
+ object: MetadataObject,
446
+ explore: FeatureProgrammer.IExplore,
447
+ tags: IMetadataTag[],
448
+ ): ts.Block => {
449
+ const top: MetadataProperty = object.properties[0]!;
450
+ if (top.key.isSoleLiteral() === false)
451
+ return decode_map(project)(importer)(index!)(
452
+ ts.factory.createCallExpression(
453
+ ts.factory.createIdentifier("Object.entries"),
454
+ [],
455
+ [input],
456
+ ),
457
+ top,
458
+ explore,
459
+ tags,
460
+ );
461
+ return ts.factory.createBlock(
462
+ [
463
+ ...(index !== null
464
+ ? [
465
+ decode_tag(ProtobufWire.LEN)(index),
466
+ ts.factory.createCallExpression(
467
+ IdentifierFactory.access(WRITER())("fork"),
468
+ undefined,
469
+ undefined,
470
+ ),
471
+ ]
472
+ : []),
473
+ ts.factory.createCallExpression(
474
+ ts.factory.createIdentifier(
475
+ `${PREFIX}o${object.index}`,
476
+ ),
477
+ [],
478
+ [input],
479
+ ),
480
+ ...(index !== null
481
+ ? [
482
+ ts.factory.createCallExpression(
483
+ IdentifierFactory.access(WRITER())("ldelim"),
484
+ undefined,
485
+ undefined,
486
+ ),
487
+ ]
488
+ : []),
489
+ ].map((expr) => ts.factory.createExpressionStatement(expr)),
490
+ true,
491
+ );
492
+ };
493
+
494
+ const decode_array =
495
+ (project: IProject) =>
496
+ (importer: FunctionImporter) =>
497
+ (index: number) =>
498
+ (
499
+ input: ts.Expression,
500
+ array: MetadataArray,
501
+ explore: FeatureProgrammer.IExplore,
502
+ tags: IMetadataTag[],
503
+ ): ts.Block => {
504
+ const wire = get_standalone_wire(array.value, tags);
505
+ const forLoop = (index: number | null) =>
506
+ ts.factory.createForOfStatement(
507
+ undefined,
508
+ ts.factory.createVariableDeclarationList(
509
+ [ts.factory.createVariableDeclaration("elem")],
510
+ ts.NodeFlags.Const,
511
+ ),
512
+ input,
513
+ decode(project)(importer)(index)(
514
+ ts.factory.createIdentifier("elem"),
515
+ array.value,
516
+ explore,
517
+ tags,
518
+ ),
519
+ );
520
+ const length = (block: ts.Block) =>
521
+ ts.factory.createBlock(
522
+ [
523
+ ts.factory.createIfStatement(
524
+ ts.factory.createStrictInequality(
525
+ ts.factory.createNumericLiteral(0),
526
+ IdentifierFactory.access(input)("length"),
527
+ ),
528
+ block,
529
+ ),
530
+ ],
531
+ true,
532
+ );
533
+
534
+ if (wire === ProtobufWire.LEN)
535
+ return length(ts.factory.createBlock([forLoop(index)], true));
536
+ return length(
537
+ ts.factory.createBlock(
538
+ [
539
+ ts.factory.createExpressionStatement(
540
+ decode_tag(ProtobufWire.LEN)(index),
541
+ ),
542
+ ts.factory.createExpressionStatement(
543
+ ts.factory.createCallExpression(
544
+ IdentifierFactory.access(WRITER())("fork"),
545
+ undefined,
546
+ undefined,
547
+ ),
548
+ ),
549
+ forLoop(null),
550
+ ts.factory.createExpressionStatement(
551
+ ts.factory.createCallExpression(
552
+ IdentifierFactory.access(WRITER())("ldelim"),
553
+ undefined,
554
+ undefined,
555
+ ),
556
+ ),
557
+ ],
558
+ true,
559
+ ),
560
+ );
561
+ };
562
+
563
+ const decode_atomic =
564
+ (index: number | null) =>
565
+ (
566
+ input: ts.Expression,
567
+ atomic: Atomic.Literal,
568
+ tags: IMetadataTag[],
569
+ ): ts.Block => {
570
+ if (atomic === "string")
571
+ return decode_bytes("string")(index!)(input);
572
+
573
+ const out =
574
+ (wire: ProtobufWire) =>
575
+ (method: string): ts.Block =>
576
+ ts.factory.createBlock(
577
+ [
578
+ ...(index !== null
579
+ ? [decode_tag(wire)(index)]
580
+ : []),
581
+ ts.factory.createCallExpression(
582
+ IdentifierFactory.access(WRITER())(method),
583
+ undefined,
584
+ [input],
585
+ ),
586
+ ].map((exp) =>
587
+ ts.factory.createExpressionStatement(exp),
588
+ ),
589
+ true,
590
+ );
591
+ if (atomic === "boolean") return out(ProtobufWire.VARIANT)("bool");
592
+
593
+ const type: IMetadataTag.INumberType | undefined = tags.find(
594
+ (tag) => tag.kind === "type",
595
+ ) as IMetadataTag.INumberType | undefined;
596
+ if (atomic === "bigint")
597
+ return out(ProtobufWire.VARIANT)(
598
+ type?.value === "uint64" ? "uint64" : "int64",
599
+ );
600
+ else if (type === undefined) return out(ProtobufWire.I64)("double");
601
+ else if (type.value === "float")
602
+ return out(ProtobufWire.I32)("float");
603
+ return out(ProtobufWire.VARIANT)(
604
+ type.value === "uint32" || type.value === "uint"
605
+ ? "uint32"
606
+ : type.value === "int32" || type.value === "int"
607
+ ? "int32"
608
+ : type.value === "int64"
609
+ ? "int64"
610
+ : "uint64",
611
+ );
612
+ };
613
+
614
+ const decode_bytes =
615
+ (method: "bytes" | "string") =>
616
+ (index: number) =>
617
+ (input: ts.Expression): ts.Block =>
618
+ ts.factory.createBlock(
619
+ [
620
+ decode_tag(ProtobufWire.LEN)(index),
621
+ ts.factory.createCallExpression(
622
+ IdentifierFactory.access(WRITER())(method),
623
+ undefined,
624
+ [input],
625
+ ),
626
+ ].map((expr) => ts.factory.createExpressionStatement(expr)),
627
+ true,
628
+ );
629
+
630
+ const decode_tag =
631
+ (wire: ProtobufWire) =>
632
+ (index: number): ts.CallExpression =>
633
+ ts.factory.createCallExpression(
634
+ IdentifierFactory.access(WRITER())("uint32"),
635
+ undefined,
636
+ [ts.factory.createNumericLiteral((index << 3) | wire)],
637
+ );
638
+
639
+ const get_standalone_wire = (
640
+ meta: Metadata,
641
+ tags: IMetadataTag[],
642
+ ): ProtobufWire => {
643
+ if (
644
+ meta.arrays.length ||
645
+ meta.objects.length ||
646
+ meta.maps.length ||
647
+ meta.natives.length
648
+ )
649
+ return ProtobufWire.LEN;
650
+
651
+ const v = ProtobufUtil.atomics(meta)[0]!;
652
+ if (v === "string") return ProtobufWire.LEN;
653
+ else if (v === "boolean" || v === "bigint") return ProtobufWire.VARIANT;
654
+
655
+ const type: IMetadataTag.INumberType | undefined = tags.find(
656
+ (t) => t.kind === "type",
657
+ ) as IMetadataTag.INumberType | undefined;
658
+ if (type === undefined) return ProtobufWire.I64;
659
+ else if (type.value === "float") return ProtobufWire.I32;
660
+ return ProtobufWire.VARIANT;
661
+ };
662
+
663
+ /* -----------------------------------------------------------
664
+ EXPLORERS
665
+ ----------------------------------------------------------- */
666
+ const explore_objects =
667
+ (project: IProject) =>
668
+ (importer: FunctionImporter) =>
669
+ (level: number) =>
670
+ (index: number | null) =>
671
+ (
672
+ input: ts.Expression,
673
+ targets: MetadataObject[],
674
+ explore: FeatureProgrammer.IExplore,
675
+ tags: IMetadataTag[],
676
+ ): ts.Block => {
677
+ if (targets.length === 1)
678
+ return decode_object(project)(importer)(index)(
679
+ input,
680
+ targets[0]!,
681
+ explore,
682
+ tags,
683
+ );
684
+
685
+ const expected: string = `(${targets
686
+ .map((t) => t.name)
687
+ .join(" | ")})`;
688
+
689
+ // POSSIBLE TO SPECIALIZE?
690
+ const specList = UnionPredicator.object(targets);
691
+ if (specList.length === 0) {
692
+ const condition: ts.Expression = decode_union_object(
693
+ IsProgrammer.decode_object(importer),
694
+ )((i, o, e) =>
695
+ ExpressionFactory.selfCall(
696
+ decode_object(project)(importer)(index)(i, o, e, tags),
697
+ ),
698
+ )((expr) => expr)((value, expected) =>
699
+ create_throw_error(importer)(expected)(value),
700
+ )(input, targets, explore);
701
+ return StatementFactory.block(condition);
702
+ }
703
+ const remained: MetadataObject[] = targets.filter(
704
+ (t) => specList.find((s) => s.object === t) === undefined,
705
+ );
706
+
707
+ // DO SPECIALIZE
708
+ const condition: ts.IfStatement = specList
709
+ .filter((spec) => spec.property.key.getSoleLiteral() !== null)
710
+ .map((spec, i, array) => {
711
+ const key: string = spec.property.key.getSoleLiteral()!;
712
+ const accessor: ts.Expression =
713
+ IdentifierFactory.access(input)(key);
714
+ const pred: ts.Expression = spec.neighbour
715
+ ? IsProgrammer.decode(project)(importer)(
716
+ accessor,
717
+ spec.property.value,
718
+ {
719
+ ...explore,
720
+ tracable: false,
721
+ postfix: IdentifierFactory.postfix(key),
722
+ },
723
+ tags,
724
+ [],
725
+ )
726
+ : ExpressionFactory.isRequired(accessor);
727
+ return ts.factory.createIfStatement(
728
+ pred,
729
+ ts.factory.createReturnStatement(
730
+ ExpressionFactory.selfCall(
731
+ decode_object(project)(importer)(index)(
732
+ input,
733
+ spec.object,
734
+ explore,
735
+ tags,
736
+ ),
737
+ ),
738
+ ),
739
+ i === array.length - 1
740
+ ? remained.length
741
+ ? ts.factory.createReturnStatement(
742
+ ExpressionFactory.selfCall(
743
+ explore_objects(project)(importer)(
744
+ level + 1,
745
+ )(index)(
746
+ input,
747
+ remained,
748
+ explore,
749
+ tags,
750
+ ),
751
+ ),
752
+ )
753
+ : create_throw_error(importer)(expected)(input)
754
+ : undefined,
755
+ );
756
+ })
757
+ .reverse()
758
+ .reduce((a, b) =>
759
+ ts.factory.createIfStatement(
760
+ b.expression,
761
+ b.thenStatement,
762
+ a,
763
+ ),
764
+ );
765
+
766
+ // RETURNS WITH CONDITIONS
767
+ return ts.factory.createBlock([condition], true);
768
+ };
769
+
770
+ const explore_arrays =
771
+ (project: IProject) =>
772
+ (importer: FunctionImporter) =>
773
+ (index: number) =>
774
+ (
775
+ input: ts.Expression,
776
+ elements: MetadataArray[],
777
+ explore: FeatureProgrammer.IExplore,
778
+ tags: IMetadataTag[],
779
+ ): ts.Block =>
780
+ elements.length === 1
781
+ ? decode_array(project)(importer)(index)(
782
+ input,
783
+ elements[0]!,
784
+ explore,
785
+ tags,
786
+ )
787
+ : explore_array_like_union_types(importer)(
788
+ UnionExplorer.array({
789
+ checker: IsProgrammer.decode(project)(importer),
790
+ decoder: (input, target, explore, tags) =>
791
+ ts.factory.createCallExpression(
792
+ ts.factory.createArrowFunction(
793
+ undefined,
794
+ undefined,
795
+ [],
796
+ undefined,
797
+ undefined,
798
+ decode_array(project)(importer)(index)(
799
+ input,
800
+ target,
801
+ explore,
802
+ tags,
803
+ ),
804
+ ),
805
+ undefined,
806
+ undefined,
807
+ ),
808
+ empty: ts.factory.createStringLiteral("[]"),
809
+ success: ts.factory.createTrue(),
810
+ failure: (input, expected) =>
811
+ create_throw_error(importer)(expected)(input),
812
+ }),
813
+ )(input, elements, explore, tags);
814
+
815
+ const explore_array_like_union_types =
816
+ (importer: FunctionImporter) =>
817
+ <T extends MetadataArray | MetadataTuple>(
818
+ factory: (
819
+ parameters: ts.ParameterDeclaration[],
820
+ ) => (
821
+ input: ts.Expression,
822
+ elements: T[],
823
+ explore: FeatureProgrammer.IExplore,
824
+ tags: IMetadataTag[],
825
+ jsDocTags: IJsDocTagInfo[],
826
+ ) => ts.ArrowFunction,
827
+ ) =>
828
+ (
829
+ input: ts.Expression,
830
+ elements: T[],
831
+ explore: FeatureProgrammer.IExplore,
832
+ tags: IMetadataTag[],
833
+ ): ts.Block => {
834
+ const arrow =
835
+ (parameters: ts.ParameterDeclaration[]) =>
836
+ (explore: FeatureProgrammer.IExplore) =>
837
+ (input: ts.Expression): ts.ArrowFunction =>
838
+ factory(parameters)(input, elements, explore, tags, []);
839
+ if (elements.every((e) => e.recursive === false))
840
+ return StatementFactory.block(
841
+ ts.factory.createCallExpression(
842
+ arrow([])(explore)(input),
843
+ undefined,
844
+ [],
845
+ ),
846
+ );
847
+
848
+ explore = {
849
+ ...explore,
850
+ source: "function",
851
+ from: "array",
852
+ };
853
+ return StatementFactory.block(
854
+ ts.factory.createCallExpression(
855
+ ts.factory.createIdentifier(
856
+ importer.emplaceUnion(
857
+ PREFIX,
858
+ elements.map((e) => e.name).join(" | "),
859
+ () =>
860
+ arrow(
861
+ FeatureProgrammer.parameterDeclarations(
862
+ CONFIG,
863
+ )(TypeFactory.keyword("any"))(
864
+ ts.factory.createIdentifier("input"),
865
+ ),
866
+ )({
867
+ ...explore,
868
+ postfix: "",
869
+ })(ts.factory.createIdentifier("input")),
870
+ ),
871
+ ),
872
+ undefined,
873
+ FeatureProgrammer.argumentsArray(CONFIG)(explore)(input),
874
+ ),
875
+ );
876
+ };
877
+
878
+ /* -----------------------------------------------------------
879
+ CONFIGURATIONS
880
+ ----------------------------------------------------------- */
881
+ const PREFIX = "$pe";
882
+ const CONFIG = {
883
+ path: false,
884
+ trace: false,
885
+ };
886
+
887
+ const create_throw_error =
888
+ (importer: FunctionImporter) =>
889
+ (expected: string) =>
890
+ (value: ts.Expression) =>
891
+ ts.factory.createExpressionStatement(
892
+ ts.factory.createCallExpression(
893
+ importer.use("throws"),
894
+ [],
895
+ [
896
+ ts.factory.createObjectLiteralExpression(
897
+ [
898
+ ts.factory.createPropertyAssignment(
899
+ "expected",
900
+ ts.factory.createStringLiteral(expected),
901
+ ),
902
+ ts.factory.createPropertyAssignment(
903
+ "value",
904
+ value,
905
+ ),
906
+ ],
907
+ true,
908
+ ),
909
+ ],
910
+ ),
911
+ );
912
+ }
913
+
914
+ const WRITER = () => ts.factory.createIdentifier("writer");
915
+
916
+ interface IUnion {
917
+ type: string;
918
+ is: () => ts.Expression;
919
+ value: (index: number | null) => ts.Block;
920
+ }