@sdkgen/fsharp-generator 0.0.0-dev.20231002144112 → 0.0.0-dev.20231002191238

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/helpers.ts DELETED
@@ -1,578 +0,0 @@
1
- import type { Type } from "@sdkgen/parser";
2
- import {
3
- ArrayType,
4
- Base64PrimitiveType,
5
- BigIntPrimitiveType,
6
- BoolPrimitiveType,
7
- BytesPrimitiveType,
8
- CnpjPrimitiveType,
9
- CpfPrimitiveType,
10
- DatePrimitiveType,
11
- DateTimePrimitiveType,
12
- EmailPrimitiveType,
13
- EnumType,
14
- FloatPrimitiveType,
15
- HexPrimitiveType,
16
- HtmlPrimitiveType,
17
- IntPrimitiveType,
18
- JsonPrimitiveType,
19
- MoneyPrimitiveType,
20
- OptionalType,
21
- StringPrimitiveType,
22
- StructType,
23
- TypeReference,
24
- UIntPrimitiveType,
25
- UrlPrimitiveType,
26
- UuidPrimitiveType,
27
- VoidPrimitiveType,
28
- XmlPrimitiveType,
29
- } from "@sdkgen/parser";
30
-
31
- const reservedWords = [
32
- "abstract",
33
- "as",
34
- "base",
35
- "bool",
36
- "break",
37
- "byte",
38
- "case",
39
- "catch",
40
- "char",
41
- "checked",
42
- "class",
43
- "const",
44
- "continue",
45
- "decimal",
46
- "default",
47
- "delegate",
48
- "do",
49
- "double",
50
- "else",
51
- "enum",
52
- "event",
53
- "explicit",
54
- "extern",
55
- "false",
56
- "finally",
57
- "fixed",
58
- "float",
59
- "for",
60
- "foreach",
61
- "goto",
62
- "if",
63
- "implicit",
64
- "in",
65
- "int",
66
- "interface",
67
- "internal",
68
- "is",
69
- "lock",
70
- "long",
71
- "namespace",
72
- "new",
73
- "null",
74
- "object",
75
- "operator",
76
- "out",
77
- "override",
78
- "params",
79
- "private",
80
- "protected",
81
- "public",
82
- "readonly",
83
- "ref",
84
- "return",
85
- "sbyte",
86
- "sealed",
87
- "short",
88
- "sizeof",
89
- "stackalloc",
90
- "static",
91
- "string",
92
- "struct",
93
- "switch",
94
- "this",
95
- "throw",
96
- "true",
97
- "try",
98
- "typeof",
99
- "uint",
100
- "ulong",
101
- "unchecked",
102
- "unsafe",
103
- "ushort",
104
- "using",
105
- "using",
106
- "static",
107
- "virtual",
108
- "void",
109
- "volatile",
110
- "while",
111
- "type",
112
- "end",
113
- "id",
114
- ];
115
-
116
- const typesWithNativeNullable: Function[] = [
117
- StringPrimitiveType,
118
- HtmlPrimitiveType,
119
- CpfPrimitiveType,
120
- CnpjPrimitiveType,
121
- BytesPrimitiveType,
122
- EmailPrimitiveType,
123
- UrlPrimitiveType,
124
- UuidPrimitiveType,
125
- HexPrimitiveType,
126
- Base64PrimitiveType,
127
- XmlPrimitiveType,
128
- StructType,
129
- ArrayType,
130
- ];
131
-
132
- const needsTempVarForNullable: Function[] = [
133
- BigIntPrimitiveType,
134
- DatePrimitiveType,
135
- DateTimePrimitiveType,
136
- FloatPrimitiveType,
137
- IntPrimitiveType,
138
- MoneyPrimitiveType,
139
- UIntPrimitiveType,
140
- ArrayType,
141
- ];
142
-
143
- export function ident(name: string): string {
144
- return reservedWords.includes(name) ? `${name}'` : name;
145
- }
146
-
147
- export function capitalize(name: string): string {
148
- return name[0].toUpperCase() + name.slice(1);
149
- }
150
-
151
- export function generateTypeName(type: Type): string {
152
- switch (type.constructor) {
153
- case StringPrimitiveType:
154
- return "string";
155
- case IntPrimitiveType:
156
- return "int";
157
- case UIntPrimitiveType:
158
- return "uint";
159
- case FloatPrimitiveType:
160
- return "double";
161
- case BigIntPrimitiveType:
162
- return "bigint";
163
- case DatePrimitiveType:
164
- case DateTimePrimitiveType:
165
- return "DateTime";
166
- case BoolPrimitiveType:
167
- return "bool";
168
- case BytesPrimitiveType:
169
- return "byte[]";
170
- case MoneyPrimitiveType:
171
- return "decimal";
172
- case UuidPrimitiveType:
173
- return "Guid";
174
- case CpfPrimitiveType:
175
- case CnpjPrimitiveType:
176
- case EmailPrimitiveType:
177
- case HtmlPrimitiveType:
178
- case UrlPrimitiveType:
179
- case HexPrimitiveType:
180
- case Base64PrimitiveType:
181
- case XmlPrimitiveType:
182
- return "string";
183
- case VoidPrimitiveType:
184
- return "void";
185
- case JsonPrimitiveType:
186
- return "JsonElement";
187
- case OptionalType:
188
- return `${generateTypeName((type as OptionalType).base)} option`;
189
- case ArrayType:
190
- return `${generateTypeName((type as ArrayType).base)} list`;
191
- case StructType:
192
- return type.name;
193
- case EnumType:
194
- return type.name;
195
- case TypeReference:
196
- return generateTypeName((type as TypeReference).type);
197
- default:
198
- throw new Error(`BUG: generateTypeName with ${type.constructor.name}`);
199
- }
200
- }
201
-
202
- export function decodeType(type: Type, jsonElementVar: string, path: string, targetVar: string, suffix = 1, maybeNull = true): string {
203
- switch (type.constructor) {
204
- case IntPrimitiveType: {
205
- return `
206
- decodeInt32 ${jsonElementVar} ${path}
207
- `
208
- .replace(/\n {16}/gu, "\n")
209
- .trim();
210
- }
211
-
212
- case UIntPrimitiveType: {
213
- return `
214
- decodeUInt32 ${jsonElementVar} ${path}
215
- `
216
- .replace(/\n {16}/gu, "\n")
217
- .trim();
218
- }
219
-
220
- case MoneyPrimitiveType: {
221
- return `
222
- (decodeMoney ${jsonElementVar} ${path}) / 100m
223
- `
224
- .replace(/\n {16}/gu, "\n")
225
- .trim();
226
- }
227
-
228
- case FloatPrimitiveType: {
229
- return `
230
- decodeFloat ${jsonElementVar} ${path}
231
- `
232
- .replace(/\n {16}/gu, "\n")
233
- .trim();
234
- }
235
-
236
- case BigIntPrimitiveType: {
237
- return `
238
- decodeBigInt ${jsonElementVar} ${path}
239
- `
240
- .replace(/\n {16}/gu, "\n")
241
- .trim();
242
- }
243
-
244
- case StringPrimitiveType: {
245
- return `
246
- decodeString ${jsonElementVar} ${path}
247
- `
248
- .replace(/\n {16}/gu, "\n")
249
- .trim();
250
- }
251
-
252
- case HtmlPrimitiveType: {
253
- // TODO: validate HTML
254
- return `
255
- decodeHtml ${jsonElementVar} ${path}
256
- `
257
- .replace(/\n {16}/gu, "\n")
258
- .trim();
259
- }
260
-
261
- case CpfPrimitiveType: {
262
- // TODO: validate CPF
263
- return `
264
- decodeCpf ${jsonElementVar} ${path}
265
- `
266
- .replace(/\n {16}/gu, "\n")
267
- .trim();
268
- }
269
-
270
- case CnpjPrimitiveType: {
271
- // TODO: validate CNPJ
272
- return `
273
- decodeCnpj ${jsonElementVar} ${path}
274
- `
275
- .replace(/\n {16}/gu, "\n")
276
- .trim();
277
- }
278
-
279
- case EmailPrimitiveType: {
280
- // TODO: validate Email
281
- return `
282
- decodeEmail ${jsonElementVar} ${path}
283
- `
284
- .replace(/\n {16}/gu, "\n")
285
- .trim();
286
- }
287
-
288
- case UrlPrimitiveType: {
289
- // TODO: validate URL
290
- return `
291
- decodeUrl ${jsonElementVar} ${path}
292
- `
293
- .replace(/\n {16}/gu, "\n")
294
- .trim();
295
- }
296
-
297
- case UuidPrimitiveType: {
298
- // TODO: validate UUID
299
- return `
300
- decodeUuid ${jsonElementVar} ${path}
301
- `
302
- .replace(/\n {16}/gu, "\n")
303
- .trim();
304
- }
305
-
306
- case HexPrimitiveType: {
307
- // TODO: validate Hex
308
- return `
309
- decodeHex ${jsonElementVar} ${path}
310
- `
311
- .replace(/\n {16}/gu, "\n")
312
- .trim();
313
- }
314
-
315
- case Base64PrimitiveType: {
316
- // TODO: validate Base64
317
- return `
318
- decodeBase64 ${jsonElementVar} ${path}
319
- `
320
- .replace(/\n {16}/gu, "\n")
321
- .trim();
322
- }
323
-
324
- case XmlPrimitiveType: {
325
- // TODO: validate XML
326
- return `
327
- decodeXml ${jsonElementVar} ${path}
328
- `
329
- .replace(/\n {16}/gu, "\n")
330
- .trim();
331
- }
332
-
333
- case BoolPrimitiveType: {
334
- return `
335
- decodeBool ${jsonElementVar} ${path}
336
- `
337
- .replace(/\n {16}/gu, "\n")
338
- .trim();
339
- }
340
-
341
- case BytesPrimitiveType: {
342
- return `
343
- decodeBytes ${jsonElementVar} ${path}
344
- `
345
- .replace(/\n {16}/gu, "\n")
346
- .trim();
347
- }
348
-
349
- case TypeReference:
350
- return decodeType((type as TypeReference).type, jsonElementVar, path, targetVar, suffix);
351
- case OptionalType:
352
- if (needsTempVarForNullable.includes((type as OptionalType).base.constructor)) {
353
- return `match ${jsonElementVar}.ValueKind with
354
- | JsonValueKind.Null | JsonValueKind.Undefined -> None
355
- | _ ->
356
- (${decodeType((type as OptionalType).base, jsonElementVar, path, targetVar, suffix, false).replace(
357
- /\n/gu,
358
- "\n ",
359
- )}) |> Some
360
- `
361
- .replace(/\n {16}/gu, "\n")
362
- .trim();
363
- }
364
-
365
- if ((type as ArrayType).base instanceof OptionalType || (type as ArrayType).base instanceof ArrayType) {
366
- return `
367
- (>.>) decodeOptional ${decodeType((type as OptionalType).base, jsonElementVar, path, targetVar, suffix, false)}
368
- `
369
- .replace(/\n {16}/gu, "\n")
370
- .trim();
371
- }
372
-
373
- return `
374
- decodeOptional ${decodeType((type as OptionalType).base, jsonElementVar, path, targetVar, suffix, false)}
375
- `
376
- .replace(/\n {16}/gu, "\n")
377
- .trim();
378
- case EnumType:
379
- case StructType:
380
- return `Decode${type.name} ${jsonElementVar} ${path}`;
381
- case JsonPrimitiveType:
382
- if (maybeNull) {
383
- return `
384
- ${jsonElementVar}
385
- `
386
- .replace(/\n {16}/gu, "\n")
387
- .trim();
388
- }
389
-
390
- return `${targetVar} <- ${jsonElementVar}`;
391
-
392
- case DateTimePrimitiveType: {
393
- return `
394
- decodeDateTime ${jsonElementVar} ${path}
395
- `
396
- .replace(/\n {16}/gu, "\n")
397
- .trim();
398
- }
399
-
400
- case DatePrimitiveType: {
401
- return `
402
- decodeDate ${jsonElementVar} ${path}
403
- `
404
- .replace(/\n {16}/gu, "\n")
405
- .trim();
406
- }
407
-
408
- case ArrayType: {
409
- return `
410
- match ${jsonElementVar}.ValueKind with
411
- | JsonValueKind.Array ->
412
- let mutable list_ = List.empty
413
- for i1 in 0 .. (${jsonElementVar}.GetArrayLength() - 1) do
414
- let item = ${jsonElementVar}.[i1]
415
- let partialResult =
416
- ${decodeType(
417
- (type as ArrayType).base,
418
- `${jsonElementVar}.[i1]`,
419
- `${path.slice(0, -1)}.{i1}"`,
420
- targetVar,
421
- suffix,
422
- false,
423
- ).replace(/\n/gu, "\n ")}
424
- list_ <- list_ |> List.append [ partialResult ]
425
- list_
426
- | _ -> raise (FatalException(${path.slice(0, -1)} must be an array."))
427
- `
428
- .replace(/\n {16}/gu, "\n")
429
- .trim();
430
- }
431
-
432
- default:
433
- throw new Error(`BUG: decodeType with ${type.constructor.name}`);
434
- }
435
- }
436
-
437
- export function encodeType(type: Type, valueVar: string, path: string, suffix = 1, isRef = true): string {
438
- switch (type.constructor) {
439
- case StringPrimitiveType: {
440
- return `resultWriter_.WriteStringValue(${valueVar})`;
441
- }
442
-
443
- case FloatPrimitiveType:
444
- case UIntPrimitiveType:
445
- case IntPrimitiveType: {
446
- return `resultWriter_.WriteNumberValue(${valueVar})`;
447
- }
448
-
449
- case MoneyPrimitiveType: {
450
- return `resultWriter_.WriteNumberValue(int (Math.Round(${valueVar} * 100m)))`;
451
- }
452
-
453
- case BigIntPrimitiveType: {
454
- return `resultWriter_.WriteStringValue(${valueVar}.ToString())`;
455
- }
456
-
457
- case BoolPrimitiveType: {
458
- return `resultWriter_.WriteBooleanValue(${valueVar})`;
459
- }
460
-
461
- case BytesPrimitiveType: {
462
- return `resultWriter_.WriteStringValue(Convert.ToBase64String(${valueVar}))`;
463
- }
464
-
465
- case DateTimePrimitiveType: {
466
- return `resultWriter_.WriteStringValue(${valueVar}.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFF'Z'"))`;
467
- }
468
-
469
- case DatePrimitiveType: {
470
- return `resultWriter_.WriteStringValue(${valueVar}.ToString("yyyy-MM-dd"))`;
471
- }
472
-
473
- // TODO: format those
474
- case CpfPrimitiveType:
475
- case CnpjPrimitiveType:
476
- case EmailPrimitiveType:
477
- case HtmlPrimitiveType:
478
- case UrlPrimitiveType:
479
- case UuidPrimitiveType:
480
- case Base64PrimitiveType:
481
- case HexPrimitiveType:
482
- case XmlPrimitiveType: {
483
- return `resultWriter_.WriteStringValue(${valueVar})`;
484
- }
485
-
486
- case OptionalType: {
487
- let realBaseType = (type as OptionalType).base;
488
-
489
- while (realBaseType instanceof TypeReference) {
490
- realBaseType = realBaseType.type;
491
- }
492
-
493
- return `
494
- if (${valueVar}.IsNone) then
495
- resultWriter_.WriteNullValue()
496
- else
497
- ${encodeType(realBaseType, `${valueVar}.Value`, path, suffix, isRef)}
498
- `
499
- .replace(/\n {14}/gu, "\n")
500
- .trim();
501
- }
502
-
503
- case TypeReference:
504
- return encodeType((type as TypeReference).type, valueVar, path, suffix, isRef);
505
- case EnumType:
506
- return `Encode${type.name} ${valueVar} resultWriter_ ${path}`;
507
- case StructType:
508
- return `Encode${type.name} ${valueVar} resultWriter_ ${path}`;
509
- case JsonPrimitiveType:
510
- return `${valueVar}.WriteTo(resultWriter_)`;
511
- case ArrayType: {
512
- return `
513
- resultWriter_.WriteStartArray()
514
- for i${suffix} in 0..${valueVar}.Length - 1 do
515
- ${encodeType((type as ArrayType).base, `${valueVar}.[i${suffix}]`, `${path}`, suffix + 1).replace(/\n/gu, "\n ")}
516
- resultWriter_.WriteEndArray()
517
- `
518
- .replace(/\n {14}/gu, "\n")
519
- .trim();
520
- }
521
-
522
- default:
523
- throw new Error(`BUG: encodeType with ${type.constructor.name}`);
524
- }
525
- }
526
-
527
- export function generateStruct(struct: StructType): string {
528
- return `
529
- type ${struct.name} = {
530
- ${struct.fields.map(field => `${capitalize(field.name)}: ${generateTypeName(field.type)}`).join(";\n ")}
531
- } with
532
- static member Create (${struct.fields.map(field => `${ident(field.name)}: ${generateTypeName(field.type)}`).join(", ")}): ${struct.name} =
533
- { ${struct.fields.map(field => `${capitalize(field.name)} = ${ident(field.name)}`).join("; ")} }
534
-
535
- let Decode${struct.name} (json_: JsonElement) (path_: string): ${struct.name} =
536
- if (json_.ValueKind <> JsonValueKind.Object) then raise (FatalException($"'{path_}' must be an object."))
537
- ${struct.fields
538
- .map(
539
- field =>
540
- `
541
- let ${field.name}Json_ = decodeJsonElementStrict ${JSON.stringify(field.name)} json_ $"{path_}.${field.name}"
542
- let ${ident(field.name)} =
543
- ${decodeType(field.type, `${field.name}Json_`, `$"{path_}.${field.name}"`, ident(field.name)).replace(/\n/gu, "\n ")}
544
- `,
545
- )
546
- .join("")}
547
- { ${struct.fields.map(field => `${capitalize(field.name)} = ${ident(field.name)}`).join("; ")} }
548
-
549
- let Encode${struct.name} (obj_: ${struct.name}) (resultWriter_: Utf8JsonWriter) (path_: string) =
550
- resultWriter_.WriteStartObject()
551
- ${struct.fields
552
- .map(
553
- field =>
554
- `
555
- resultWriter_.WritePropertyName(${JSON.stringify(field.name)})
556
- ${encodeType(field.type, `obj_.${capitalize(field.name)}`, `$"{path_}.${field.name}"`).replace(/\n/gu, "\n ")}`,
557
- )
558
- .join("\n")}
559
- resultWriter_.WriteEndObject()`;
560
- }
561
-
562
- export function generateEnum(type: EnumType): string {
563
- return `
564
- type ${type.name} =
565
- ${type.values.map(({ value }) => `| ${capitalize(value)}`).join("\n ")}
566
-
567
- let Decode${type.name} (json_: JsonElement) (path_: string): ${type.name} =
568
- if (json_.ValueKind <> JsonValueKind.String) then raise (FatalException($"'{path_}' must be a string."))
569
- match json_.GetString() with
570
- ${type.values.map(({ value }) => `| "${value}" -> ${type.name}.${capitalize(value)}`).join("\n ")}
571
- | _ -> raise (FatalException($"'{path_}' must be one of: (${type.values.map(({ value }) => `'${value}'`).join(", ")})."))
572
-
573
- let Encode${type.name} (obj_: ${type.name}) (resultWriter_: Utf8JsonWriter) (path_: string) =
574
- match obj_ with
575
- ${type.values.map(({ value }) => `| ${type.name}.${capitalize(value)} -> resultWriter_.WriteStringValue("${value}")`).join("\n ")}
576
-
577
- `;
578
- }
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./fsharp-server";
File without changes
File without changes