typia 5.0.0-dev.20230821 → 5.0.0-dev.20230822

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