typia 5.0.0-dev.20230821 → 5.0.0-dev.20230823

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