@arrirpc/codegen-kotlin 0.72.0 → 0.74.0
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/dist/index.cjs +97 -72
- package/dist/index.d.cts +1 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +97 -72
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
@@ -65,7 +65,7 @@ function getClassName(schema, context) {
|
|
65
65
|
normalize: true
|
66
66
|
})
|
67
67
|
);
|
68
|
-
return
|
68
|
+
return className2;
|
69
69
|
}
|
70
70
|
const depth = instanceDepth(context);
|
71
71
|
if (depth === 1 && !context.discriminatorKey) {
|
@@ -74,7 +74,7 @@ function getClassName(schema, context) {
|
|
74
74
|
normalize: true
|
75
75
|
})
|
76
76
|
);
|
77
|
-
return
|
77
|
+
return className2;
|
78
78
|
}
|
79
79
|
if (context.discriminatorParentId && context.discriminatorKey && context.discriminatorValue) {
|
80
80
|
const className2 = kotlinClassName(
|
@@ -83,7 +83,7 @@ function getClassName(schema, context) {
|
|
83
83
|
{ normalize: true }
|
84
84
|
)
|
85
85
|
);
|
86
|
-
return
|
86
|
+
return className2;
|
87
87
|
}
|
88
88
|
const className = kotlinClassName(
|
89
89
|
codegenUtils.pascalCase(
|
@@ -93,7 +93,7 @@ function getClassName(schema, context) {
|
|
93
93
|
}
|
94
94
|
)
|
95
95
|
);
|
96
|
-
return
|
96
|
+
return className;
|
97
97
|
}
|
98
98
|
function instanceDepth(context) {
|
99
99
|
const parts = context.instancePath.split("/");
|
@@ -130,6 +130,7 @@ function kotlinAnyFromSchema(schema, context) {
|
|
130
130
|
const defaultValue = nullable ? "null" : "JsonNull";
|
131
131
|
return {
|
132
132
|
typeName: "JsonElement",
|
133
|
+
prefixedTypeName: "JsonElement",
|
133
134
|
isNullable: nullable,
|
134
135
|
defaultValue,
|
135
136
|
fromJson(input) {
|
@@ -178,9 +179,10 @@ function kotlinArrayFromSchema(schema, context) {
|
|
178
179
|
schemaPath: `${context.schemaPath}/elements`,
|
179
180
|
existingTypeIds: context.existingTypeIds
|
180
181
|
});
|
181
|
-
const typeName = `MutableList<${subType.
|
182
|
+
const typeName = `MutableList<${subType.prefixedTypeName}${subType.isNullable ? "?" : ""}>`;
|
182
183
|
return {
|
183
184
|
typeName,
|
185
|
+
prefixedTypeName: typeName,
|
184
186
|
isNullable: nullable,
|
185
187
|
defaultValue,
|
186
188
|
fromJson(input) {
|
@@ -246,16 +248,18 @@ function kotlinArrayFromSchema(schema, context) {
|
|
246
248
|
|
247
249
|
function kotlinObjectFromSchema(schema, context) {
|
248
250
|
const className = getClassName(schema, context);
|
251
|
+
const prefixedClassName = `${context.typePrefix}${className}`;
|
249
252
|
const nullable = isNullable(schema, context);
|
250
|
-
const defaultValue = nullable ? "null" : `${
|
253
|
+
const defaultValue = nullable ? "null" : `${prefixedClassName}.new()`;
|
251
254
|
const result = {
|
252
255
|
typeName: className,
|
256
|
+
prefixedTypeName: prefixedClassName,
|
253
257
|
isNullable: nullable,
|
254
258
|
defaultValue,
|
255
259
|
fromJson(input, key) {
|
256
260
|
if (nullable) {
|
257
261
|
return `when (${input}) {
|
258
|
-
is JsonObject -> ${
|
262
|
+
is JsonObject -> ${prefixedClassName}.fromJsonElement(
|
259
263
|
${input}!!,
|
260
264
|
"$instancePath/${key}",
|
261
265
|
)
|
@@ -263,12 +267,12 @@ function kotlinObjectFromSchema(schema, context) {
|
|
263
267
|
}`;
|
264
268
|
}
|
265
269
|
return `when (${input}) {
|
266
|
-
is JsonObject -> ${
|
270
|
+
is JsonObject -> ${prefixedClassName}.fromJsonElement(
|
267
271
|
${input}!!,
|
268
272
|
"$instancePath/${key}",
|
269
273
|
)
|
270
274
|
|
271
|
-
else -> ${
|
275
|
+
else -> ${prefixedClassName}.new()
|
272
276
|
}`;
|
273
277
|
},
|
274
278
|
toJson(input, target) {
|
@@ -323,7 +327,7 @@ function kotlinObjectFromSchema(schema, context) {
|
|
323
327
|
subContent.push(type.content);
|
324
328
|
}
|
325
329
|
fieldParts.push(
|
326
|
-
`${getCodeComment(prop.metadata, " ", "field")} val ${kotlinKey}: ${type.
|
330
|
+
`${getCodeComment(prop.metadata, " ", "field")} val ${kotlinKey}: ${type.prefixedTypeName}${type.isNullable ? "?" : ""},`
|
327
331
|
);
|
328
332
|
if (i === 0 && !context.discriminatorKey) {
|
329
333
|
toJsonParts.push(`output += "\\"${key}\\":"`);
|
@@ -333,7 +337,7 @@ function kotlinObjectFromSchema(schema, context) {
|
|
333
337
|
toJsonParts.push(type.toJson(kotlinKey, "output"));
|
334
338
|
toQueryParts.push(type.toQueryString(kotlinKey, "queryParts", key));
|
335
339
|
fromJsonParts.push(
|
336
|
-
`val ${kotlinKey}: ${type.
|
340
|
+
`val ${kotlinKey}: ${type.prefixedTypeName}${type.isNullable ? "?" : ""} = ${type.fromJson(`__input.jsonObject["${key}"]`, key)}`
|
337
341
|
);
|
338
342
|
defaultParts.push(
|
339
343
|
` ${kotlinKey} = ${type.defaultValue},`
|
@@ -361,7 +365,7 @@ function kotlinObjectFromSchema(schema, context) {
|
|
361
365
|
if (hasProperties) output += ","
|
362
366
|
`;
|
363
367
|
fieldParts.push(
|
364
|
-
`${getCodeComment(schema.optionalProperties[key].metadata, " ", "field")} val ${kotlinKey}: ${type.
|
368
|
+
`${getCodeComment(schema.optionalProperties[key].metadata, " ", "field")} val ${kotlinKey}: ${type.prefixedTypeName}? = null,`
|
365
369
|
);
|
366
370
|
if (hasKnownKeys) {
|
367
371
|
toJsonParts.push(`if (${kotlinKey} != null) {
|
@@ -377,20 +381,20 @@ ${isLast ? "" : " hasProperties = true"}
|
|
377
381
|
}
|
378
382
|
toQueryParts.push(type.toQueryString(kotlinKey, "queryParts", key));
|
379
383
|
fromJsonParts.push(
|
380
|
-
`val ${kotlinKey}: ${type.
|
384
|
+
`val ${kotlinKey}: ${type.prefixedTypeName}? = ${type.fromJson(`__input.jsonObject["${key}"]`, key)}`
|
381
385
|
);
|
382
386
|
}
|
383
387
|
toJsonParts.push('output += "}"');
|
384
388
|
toJsonParts.push("return output");
|
385
389
|
toQueryParts.push('return queryParts.joinToString("&")');
|
386
|
-
const implementedClass = context.discriminatorParentId
|
390
|
+
const implementedClass = context.discriminatorParentId ? `${context.typePrefix}${context.discriminatorParentId}` : `${context.clientName}Model`;
|
387
391
|
let discriminatorField = "";
|
388
392
|
if (context.discriminatorKey && context.discriminatorValue) {
|
389
393
|
discriminatorField = `
|
390
394
|
override val ${kotlinIdentifier(context.discriminatorKey)} get() = "${context.discriminatorValue}"
|
391
395
|
`;
|
392
396
|
}
|
393
|
-
const content = `${getCodeComment(schema.metadata, "", "class")}data class ${
|
397
|
+
const content = `${getCodeComment(schema.metadata, "", "class")}data class ${prefixedClassName}(
|
394
398
|
${fieldParts.join("\n")}
|
395
399
|
) : ${implementedClass} {${discriminatorField}
|
396
400
|
override fun toJson(): String {
|
@@ -401,27 +405,27 @@ ${toJsonParts.join("\n")}
|
|
401
405
|
${toQueryParts.join("\n")}
|
402
406
|
}
|
403
407
|
|
404
|
-
companion object Factory : ${context.clientName}ModelFactory<${
|
408
|
+
companion object Factory : ${context.clientName}ModelFactory<${prefixedClassName}> {
|
405
409
|
@JvmStatic
|
406
|
-
override fun new(): ${
|
407
|
-
return ${
|
410
|
+
override fun new(): ${prefixedClassName} {
|
411
|
+
return ${prefixedClassName}(
|
408
412
|
${defaultParts.join("\n")}
|
409
413
|
)
|
410
414
|
}
|
411
415
|
|
412
416
|
@JvmStatic
|
413
|
-
override fun fromJson(input: String): ${
|
417
|
+
override fun fromJson(input: String): ${prefixedClassName} {
|
414
418
|
return fromJsonElement(JsonInstance.parseToJsonElement(input))
|
415
419
|
}
|
416
420
|
|
417
421
|
@JvmStatic
|
418
|
-
override fun fromJsonElement(__input: JsonElement, instancePath: String): ${
|
422
|
+
override fun fromJsonElement(__input: JsonElement, instancePath: String): ${prefixedClassName} {
|
419
423
|
if (__input !is JsonObject) {
|
420
|
-
__logError("[WARNING] ${
|
424
|
+
__logError("[WARNING] ${prefixedClassName}.fromJsonElement() expected kotlinx.serialization.json.JsonObject at $instancePath. Got \${__input.javaClass}. Initializing empty ${prefixedClassName}.")
|
421
425
|
return new()
|
422
426
|
}
|
423
427
|
${fromJsonParts.join("\n")}
|
424
|
-
return ${
|
428
|
+
return ${prefixedClassName}(
|
425
429
|
${kotlinKeys.join(",\n ")},
|
426
430
|
)
|
427
431
|
}
|
@@ -439,6 +443,7 @@ ${subContent.join("\n\n")}`;
|
|
439
443
|
function kotlinDiscriminatorFromSchema(schema, context) {
|
440
444
|
const kotlinDiscriminatorKey = kotlinIdentifier(schema.discriminator);
|
441
445
|
const className = getClassName(schema, context);
|
446
|
+
const prefixedClassName = `${context.typePrefix}${className}`;
|
442
447
|
const nullable = isNullable(schema, context);
|
443
448
|
const subTypes = [];
|
444
449
|
const subContent = [];
|
@@ -457,6 +462,7 @@ function kotlinDiscriminatorFromSchema(schema, context) {
|
|
457
462
|
});
|
458
463
|
subTypes.push({
|
459
464
|
typeName: subType.typeName,
|
465
|
+
prefixedTypeName: subType.prefixedTypeName,
|
460
466
|
discriminatorValue: key
|
461
467
|
});
|
462
468
|
if (subType.content) {
|
@@ -466,14 +472,15 @@ function kotlinDiscriminatorFromSchema(schema, context) {
|
|
466
472
|
if (subTypes.length === 0) {
|
467
473
|
throw new Error("Discriminator schemas must have at least one mapping");
|
468
474
|
}
|
469
|
-
const defaultValue = nullable ? "null" : `${
|
475
|
+
const defaultValue = nullable ? "null" : `${prefixedClassName}.new()`;
|
470
476
|
const result = {
|
471
477
|
typeName: className,
|
478
|
+
prefixedTypeName: prefixedClassName,
|
472
479
|
isNullable: nullable,
|
473
480
|
defaultValue,
|
474
481
|
fromJson(input, key) {
|
475
482
|
return `when (${input}) {
|
476
|
-
is JsonObject -> ${
|
483
|
+
is JsonObject -> ${prefixedClassName}.fromJsonElement(
|
477
484
|
${input}!!,
|
478
485
|
"$instancePath/${key}",
|
479
486
|
)
|
@@ -495,29 +502,29 @@ function kotlinDiscriminatorFromSchema(schema, context) {
|
|
495
502
|
return result;
|
496
503
|
}
|
497
504
|
const codeComment = getCodeComment(schema.metadata, "", "class");
|
498
|
-
const content = `${codeComment}sealed interface ${
|
505
|
+
const content = `${codeComment}sealed interface ${prefixedClassName} : ${context.clientName}Model {
|
499
506
|
val ${kotlinDiscriminatorKey}: String
|
500
507
|
|
501
|
-
companion object Factory : ${context.clientName}ModelFactory<${
|
508
|
+
companion object Factory : ${context.clientName}ModelFactory<${prefixedClassName}> {
|
502
509
|
@JvmStatic
|
503
|
-
override fun new(): ${
|
504
|
-
return ${subTypes[0].
|
510
|
+
override fun new(): ${prefixedClassName} {
|
511
|
+
return ${subTypes[0].prefixedTypeName}.new()
|
505
512
|
}
|
506
513
|
|
507
514
|
@JvmStatic
|
508
|
-
override fun fromJson(input: String): ${
|
515
|
+
override fun fromJson(input: String): ${prefixedClassName} {
|
509
516
|
return fromJsonElement(JsonInstance.parseToJsonElement(input))
|
510
517
|
}
|
511
518
|
|
512
519
|
@JvmStatic
|
513
|
-
override fun fromJsonElement(__input: JsonElement, instancePath: String): ${
|
520
|
+
override fun fromJsonElement(__input: JsonElement, instancePath: String): ${prefixedClassName} {
|
514
521
|
if (__input !is JsonObject) {
|
515
|
-
__logError("[WARNING] Discriminator.fromJsonElement() expected kotlinx.serialization.json.JsonObject at $instancePath. Got \${__input.javaClass}. Initializing empty ${
|
522
|
+
__logError("[WARNING] Discriminator.fromJsonElement() expected kotlinx.serialization.json.JsonObject at $instancePath. Got \${__input.javaClass}. Initializing empty ${prefixedClassName}.")
|
516
523
|
return new()
|
517
524
|
}
|
518
525
|
return when (__input.jsonObject["${schema.discriminator}"]) {
|
519
526
|
is JsonPrimitive -> when (__input.jsonObject["${schema.discriminator}"]!!.jsonPrimitive.contentOrNull) {
|
520
|
-
${subTypes.map((type) => `"${type.discriminatorValue}" -> ${type.
|
527
|
+
${subTypes.map((type) => `"${type.discriminatorValue}" -> ${type.prefixedTypeName}.fromJsonElement(__input, instancePath)`).join("\n")}
|
521
528
|
else -> new()
|
522
529
|
}
|
523
530
|
|
@@ -538,6 +545,7 @@ ${subContent.join("\n\n")}`;
|
|
538
545
|
function kotlinEnumFromSchema(schema, context) {
|
539
546
|
const nullable = isNullable(schema, context);
|
540
547
|
const className = getClassName(schema, context);
|
548
|
+
const prefixedClassName = `${context.typePrefix}${className}`;
|
541
549
|
const enumItems = schema.enum.map((val) => {
|
542
550
|
return {
|
543
551
|
name: codegenUtils.pascalCase(val, { normalize: true }),
|
@@ -549,24 +557,24 @@ function kotlinEnumFromSchema(schema, context) {
|
|
549
557
|
`Enum schemas must have at least one enum value. At ${context.schemaPath}.`
|
550
558
|
);
|
551
559
|
}
|
552
|
-
const defaultValue = nullable ? "null" : `${
|
560
|
+
const defaultValue = nullable ? "null" : `${prefixedClassName}.new()`;
|
553
561
|
let content = "";
|
554
562
|
if (!context.existingTypeIds.includes(className)) {
|
555
|
-
content = `enum class ${
|
563
|
+
content = `enum class ${prefixedClassName} {
|
556
564
|
${enumItems.map((item) => item.name).join(",\n ")};
|
557
565
|
val serialValue: String
|
558
566
|
get() = when (this) {
|
559
567
|
${enumItems.map((item) => `${item.name} -> "${item.value}"`).join("\n ")}
|
560
568
|
}
|
561
569
|
|
562
|
-
companion object Factory : ${context.clientName}ModelFactory<${
|
570
|
+
companion object Factory : ${context.clientName}ModelFactory<${prefixedClassName}> {
|
563
571
|
@JvmStatic
|
564
|
-
override fun new(): ${
|
572
|
+
override fun new(): ${prefixedClassName} {
|
565
573
|
return ${enumItems[0].name}
|
566
574
|
}
|
567
575
|
|
568
576
|
@JvmStatic
|
569
|
-
override fun fromJson(input: String): ${
|
577
|
+
override fun fromJson(input: String): ${prefixedClassName} {
|
570
578
|
return when (input) {
|
571
579
|
${enumItems.map((item) => `${item.name}.serialValue -> ${item.name}`).join("\n ")}
|
572
580
|
else -> ${enumItems[0].name}
|
@@ -574,9 +582,9 @@ function kotlinEnumFromSchema(schema, context) {
|
|
574
582
|
}
|
575
583
|
|
576
584
|
@JvmStatic
|
577
|
-
override fun fromJsonElement(__input: JsonElement, instancePath: String): ${
|
585
|
+
override fun fromJsonElement(__input: JsonElement, instancePath: String): ${prefixedClassName} {
|
578
586
|
if (__input !is JsonPrimitive) {
|
579
|
-
__logError("[WARNING] ${
|
587
|
+
__logError("[WARNING] ${prefixedClassName}.fromJsonElement() expected kotlinx.serialization.json.JsonPrimitive at $instancePath. Got \${__input.javaClass}. Initializing empty ${prefixedClassName}.")
|
580
588
|
return new()
|
581
589
|
}
|
582
590
|
return when (__input.jsonPrimitive.contentOrNull) {
|
@@ -590,26 +598,27 @@ function kotlinEnumFromSchema(schema, context) {
|
|
590
598
|
}
|
591
599
|
return {
|
592
600
|
typeName: className,
|
601
|
+
prefixedTypeName: prefixedClassName,
|
593
602
|
isNullable: nullable,
|
594
603
|
defaultValue,
|
595
604
|
fromJson(input, key) {
|
596
605
|
if (nullable) {
|
597
606
|
return `when (${input}) {
|
598
607
|
is JsonNull -> null
|
599
|
-
is JsonPrimitive -> ${
|
608
|
+
is JsonPrimitive -> ${prefixedClassName}.fromJsonElement(${input}!!, "$instancePath/${key ?? ""}")
|
600
609
|
else -> null
|
601
610
|
}`;
|
602
611
|
}
|
603
612
|
return `when (${input}) {
|
604
613
|
is JsonNull -> ${defaultValue}
|
605
|
-
is JsonPrimitive -> ${
|
614
|
+
is JsonPrimitive -> ${prefixedClassName}.fromJsonElement(${input}!!, "$instancePath/${key}")
|
606
615
|
else -> ${defaultValue}
|
607
616
|
}`;
|
608
617
|
},
|
609
618
|
toJson(input, target) {
|
610
619
|
if (schema.nullable) {
|
611
620
|
return `${target} += when (${input}) {
|
612
|
-
is ${
|
621
|
+
is ${prefixedClassName} -> "\\"\${${input}.serialValue}\\""
|
613
622
|
else -> "null"
|
614
623
|
}`;
|
615
624
|
}
|
@@ -640,10 +649,11 @@ function kotlinMapFromSchema(schema, context) {
|
|
640
649
|
schemaPath: `${context.schemaPath}/values`,
|
641
650
|
existingTypeIds: context.existingTypeIds
|
642
651
|
});
|
643
|
-
const typeName = `MutableMap<String, ${subType.
|
652
|
+
const typeName = `MutableMap<String, ${subType.prefixedTypeName}${subType.isNullable ? "?" : ""}>`;
|
644
653
|
const defaultValue = nullable ? "null" : "mutableMapOf()";
|
645
654
|
return {
|
646
655
|
typeName,
|
656
|
+
prefixedTypeName: typeName,
|
647
657
|
isNullable: nullable,
|
648
658
|
defaultValue,
|
649
659
|
fromJson(input, key) {
|
@@ -708,6 +718,7 @@ function kotlinStringFromSchema(schema, context) {
|
|
708
718
|
const defaultValue = nullable ? "null" : '""';
|
709
719
|
return {
|
710
720
|
typeName: "String",
|
721
|
+
prefixedTypeName: "String",
|
711
722
|
isNullable: nullable,
|
712
723
|
defaultValue,
|
713
724
|
fromJson(input) {
|
@@ -742,6 +753,7 @@ function kotlinBooleanFromSchema(schema, context) {
|
|
742
753
|
const defaultValue = nullable ? "null" : "false";
|
743
754
|
return {
|
744
755
|
typeName: "Boolean",
|
756
|
+
prefixedTypeName: "Boolean",
|
745
757
|
isNullable: nullable,
|
746
758
|
defaultValue,
|
747
759
|
fromJson(input) {
|
@@ -770,6 +782,7 @@ function kotlinTimestampFromSchema(schema, context) {
|
|
770
782
|
const defaultValue = nullable ? "null" : "Instant.now()";
|
771
783
|
return {
|
772
784
|
typeName: "Instant",
|
785
|
+
prefixedTypeName: "Instant",
|
773
786
|
isNullable: nullable,
|
774
787
|
defaultValue,
|
775
788
|
fromJson(input) {
|
@@ -825,6 +838,7 @@ function kotlinFloat32FromSchema(schema, context) {
|
|
825
838
|
const defaultValue = nullable ? "null" : "0.0F";
|
826
839
|
return {
|
827
840
|
typeName: "Float",
|
841
|
+
prefixedTypeName: "Float",
|
828
842
|
isNullable: nullable,
|
829
843
|
defaultValue,
|
830
844
|
fromJson(input) {
|
@@ -853,6 +867,7 @@ function kotlinFloat64FromSchema(schema, context) {
|
|
853
867
|
const defaultValue = nullable ? "null" : "0.0";
|
854
868
|
return {
|
855
869
|
typeName: "Double",
|
870
|
+
prefixedTypeName: "Double",
|
856
871
|
isNullable: nullable,
|
857
872
|
defaultValue,
|
858
873
|
fromJson(input) {
|
@@ -881,6 +896,7 @@ function kotlinInt8FromSchema(schema, context) {
|
|
881
896
|
const defaultValue = nullable ? "null" : "0";
|
882
897
|
return {
|
883
898
|
typeName: "Byte",
|
899
|
+
prefixedTypeName: "Byte",
|
884
900
|
isNullable: nullable,
|
885
901
|
defaultValue,
|
886
902
|
fromJson(input) {
|
@@ -909,6 +925,7 @@ function kotlinInt16FromSchema(schema, context) {
|
|
909
925
|
const defaultValue = nullable ? "null" : "0";
|
910
926
|
return {
|
911
927
|
typeName: "Short",
|
928
|
+
prefixedTypeName: "Short",
|
912
929
|
isNullable: nullable,
|
913
930
|
defaultValue,
|
914
931
|
fromJson(input) {
|
@@ -937,6 +954,7 @@ function kotlinInt32FromSchema(schema, context) {
|
|
937
954
|
const defaultValue = nullable ? "null" : "0";
|
938
955
|
return {
|
939
956
|
typeName: "Int",
|
957
|
+
prefixedTypeName: "Int",
|
940
958
|
isNullable: nullable,
|
941
959
|
defaultValue,
|
942
960
|
fromJson(input) {
|
@@ -965,6 +983,7 @@ function kotlinInt64FromSchema(schema, context) {
|
|
965
983
|
const defaultValue = nullable ? "null" : "0L";
|
966
984
|
return {
|
967
985
|
typeName: "Long",
|
986
|
+
prefixedTypeName: "Long",
|
968
987
|
isNullable: nullable,
|
969
988
|
defaultValue,
|
970
989
|
fromJson(input) {
|
@@ -999,6 +1018,7 @@ function kotlinUint8FromSchema(schema, context) {
|
|
999
1018
|
const defaultValue = nullable ? "null" : "0u";
|
1000
1019
|
return {
|
1001
1020
|
typeName: "UByte",
|
1021
|
+
prefixedTypeName: "UByte",
|
1002
1022
|
isNullable: nullable,
|
1003
1023
|
defaultValue,
|
1004
1024
|
fromJson(input) {
|
@@ -1027,6 +1047,7 @@ function kotlinUint16FromSchema(schema, context) {
|
|
1027
1047
|
const defaultValue = nullable ? "null" : "0u";
|
1028
1048
|
return {
|
1029
1049
|
typeName: "UShort",
|
1050
|
+
prefixedTypeName: "UShort",
|
1030
1051
|
isNullable: nullable,
|
1031
1052
|
defaultValue,
|
1032
1053
|
fromJson(input) {
|
@@ -1055,6 +1076,7 @@ function kotlinUint32FromSchema(schema, context) {
|
|
1055
1076
|
const defaultValue = nullable ? "null" : "0u";
|
1056
1077
|
return {
|
1057
1078
|
typeName: "UInt",
|
1079
|
+
prefixedTypeName: "UInt",
|
1058
1080
|
isNullable: nullable,
|
1059
1081
|
defaultValue,
|
1060
1082
|
fromJson(input) {
|
@@ -1083,6 +1105,7 @@ function kotlinUint64FromSchema(schema, context) {
|
|
1083
1105
|
const defaultValue = nullable ? "null" : "0UL";
|
1084
1106
|
return {
|
1085
1107
|
typeName: "ULong",
|
1108
|
+
prefixedTypeName: "ULong",
|
1086
1109
|
isNullable: nullable,
|
1087
1110
|
defaultValue,
|
1088
1111
|
fromJson(input) {
|
@@ -1242,7 +1265,7 @@ function kotlinServiceFromSchema(schema, context) {
|
|
1242
1265
|
content: `class ${name}(
|
1243
1266
|
private val httpClient: HttpClient,
|
1244
1267
|
private val baseUrl: String,
|
1245
|
-
private val headers:
|
1268
|
+
private val headers: __${context.clientName}HeadersFn,
|
1246
1269
|
private val onError: ((err: Exception) -> Unit) = {},
|
1247
1270
|
) {
|
1248
1271
|
${procedureParts.join("\n\n ")}
|
@@ -1262,15 +1285,17 @@ function getServiceName(context) {
|
|
1262
1285
|
|
1263
1286
|
function kotlinRefFromSchema(schema, context) {
|
1264
1287
|
const typeName = kotlinClassName(schema.ref);
|
1288
|
+
const prefixedTypeName = `${context.typePrefix}${typeName}`;
|
1265
1289
|
const nullable = isNullable(schema, context);
|
1266
|
-
const defaultValue = nullable ? "null" : `${
|
1290
|
+
const defaultValue = nullable ? "null" : `${prefixedTypeName}.new()`;
|
1267
1291
|
return {
|
1268
1292
|
typeName,
|
1293
|
+
prefixedTypeName,
|
1269
1294
|
isNullable: nullable,
|
1270
1295
|
defaultValue,
|
1271
1296
|
fromJson(input, key) {
|
1272
1297
|
return `when (${input}) {
|
1273
|
-
is JsonObject -> ${
|
1298
|
+
is JsonObject -> ${prefixedTypeName}.fromJsonElement(
|
1274
1299
|
${input}!!,
|
1275
1300
|
"$instancePath/${key}",
|
1276
1301
|
)
|
@@ -1372,7 +1397,7 @@ ${getUtilityFunctions(clientName)}`;
|
|
1372
1397
|
class ${clientName}(
|
1373
1398
|
private val httpClient: HttpClient,
|
1374
1399
|
private val baseUrl: String,
|
1375
|
-
private val headers:
|
1400
|
+
private val headers: __${clientName}HeadersFn,
|
1376
1401
|
private val onError: ((err: Exception) -> Unit) = {},
|
1377
1402
|
) {
|
1378
1403
|
${procedureParts.join("\n\n ")}
|
@@ -1554,7 +1579,7 @@ data class ${clientName}Error(
|
|
1554
1579
|
}
|
1555
1580
|
function getUtilityFunctions(clientName) {
|
1556
1581
|
return `// Implementation copied from https://github.com/Kotlin/kotlinx.serialization/blob/d0ae697b9394103879e6c7f836d0f7cf128f4b1e/formats/json/commonMain/src/kotlinx/serialization/json/internal/StringOps.kt#L45
|
1557
|
-
|
1582
|
+
private const val STRING = '"'
|
1558
1583
|
|
1559
1584
|
private fun toHexChar(i: Int): Char {
|
1560
1585
|
val d = i and 0xf
|
@@ -1562,7 +1587,7 @@ private fun toHexChar(i: Int): Char {
|
|
1562
1587
|
else (d - 10 + 'a'.code).toChar()
|
1563
1588
|
}
|
1564
1589
|
|
1565
|
-
|
1590
|
+
private val ESCAPE_STRINGS: Array<String?> = arrayOfNulls<String>(93).apply {
|
1566
1591
|
for (c in 0..0x1f) {
|
1567
1592
|
val c1 = toHexChar(c shr 12)
|
1568
1593
|
val c2 = toHexChar(c shr 8)
|
@@ -1579,7 +1604,7 @@ internal val ESCAPE_STRINGS: Array<String?> = arrayOfNulls<String>(93).apply {
|
|
1579
1604
|
this[0x0c] = "\\\\f"
|
1580
1605
|
}
|
1581
1606
|
|
1582
|
-
|
1607
|
+
private val ESCAPE_MARKERS: ByteArray = ByteArray(93).apply {
|
1583
1608
|
for (c in 0..0x1f) {
|
1584
1609
|
this[c] = 1.toByte()
|
1585
1610
|
}
|
@@ -1592,7 +1617,7 @@ internal val ESCAPE_MARKERS: ByteArray = ByteArray(93).apply {
|
|
1592
1617
|
this[0x0c] = 'f'.code.toByte()
|
1593
1618
|
}
|
1594
1619
|
|
1595
|
-
|
1620
|
+
private fun StringBuilder.printQuoted(value: String) {
|
1596
1621
|
append(STRING)
|
1597
1622
|
var lastPos = 0
|
1598
1623
|
for (i in value.indices) {
|
@@ -1650,7 +1675,7 @@ private suspend fun __prepareRequest(
|
|
1650
1675
|
}
|
1651
1676
|
|
1652
1677
|
// SSE_FN_START
|
1653
|
-
private enum class SseEventLineType {
|
1678
|
+
private enum class __${clientName}SseEventLineType {
|
1654
1679
|
Id,
|
1655
1680
|
Event,
|
1656
1681
|
Data,
|
@@ -1658,36 +1683,36 @@ private enum class SseEventLineType {
|
|
1658
1683
|
None,
|
1659
1684
|
}
|
1660
1685
|
|
1661
|
-
private fun __parseSseEventLine(line: String): Pair<SseEventLineType, String> {
|
1686
|
+
private fun __parseSseEventLine(line: String): Pair<__${clientName}SseEventLineType, String> {
|
1662
1687
|
if (line.startsWith("id:")) {
|
1663
|
-
return Pair(SseEventLineType.Id, line.substring(3).trim())
|
1688
|
+
return Pair(__${clientName}SseEventLineType.Id, line.substring(3).trim())
|
1664
1689
|
}
|
1665
1690
|
if (line.startsWith("event:")) {
|
1666
|
-
return Pair(SseEventLineType.Event, line.substring(6).trim())
|
1691
|
+
return Pair(__${clientName}SseEventLineType.Event, line.substring(6).trim())
|
1667
1692
|
}
|
1668
1693
|
if (line.startsWith("data:")) {
|
1669
|
-
return Pair(SseEventLineType.Data, line.substring(5).trim())
|
1694
|
+
return Pair(__${clientName}SseEventLineType.Data, line.substring(5).trim())
|
1670
1695
|
}
|
1671
1696
|
if (line.startsWith("retry:")) {
|
1672
|
-
return Pair(SseEventLineType.Retry, line.substring(6).trim())
|
1697
|
+
return Pair(__${clientName}SseEventLineType.Retry, line.substring(6).trim())
|
1673
1698
|
}
|
1674
|
-
return Pair(SseEventLineType.None, "")
|
1699
|
+
return Pair(__${clientName}SseEventLineType.None, "")
|
1675
1700
|
}
|
1676
1701
|
|
1677
|
-
private data class
|
1702
|
+
private data class __${clientName}SseEvent(
|
1678
1703
|
val id: String? = null,
|
1679
1704
|
val event: String,
|
1680
1705
|
val data: String,
|
1681
1706
|
val retry: Int? = null
|
1682
1707
|
)
|
1683
1708
|
|
1684
|
-
private class
|
1709
|
+
private class __${clientName}SseEventParsingResult(val events: List<__${clientName}SseEvent>, val leftover: String)
|
1685
1710
|
|
1686
|
-
private fun __parseSseEvents(input: String):
|
1687
|
-
val events = mutableListOf<
|
1711
|
+
private fun __parseSseEvents(input: String): __${clientName}SseEventParsingResult {
|
1712
|
+
val events = mutableListOf<__${clientName}SseEvent>()
|
1688
1713
|
val lines = input.lines()
|
1689
1714
|
if (lines.isEmpty()) {
|
1690
|
-
return
|
1715
|
+
return __${clientName}SseEventParsingResult(events = listOf(), leftover = "")
|
1691
1716
|
}
|
1692
1717
|
var id: String? = null
|
1693
1718
|
var event: String? = null
|
@@ -1698,18 +1723,18 @@ private fun __parseSseEvents(input: String): __SseEventParsingResult {
|
|
1698
1723
|
if (line.isNotEmpty()) {
|
1699
1724
|
val (type, value) = __parseSseEventLine(line)
|
1700
1725
|
when (type) {
|
1701
|
-
SseEventLineType.Id -> id = value
|
1702
|
-
SseEventLineType.Event -> event = value
|
1703
|
-
SseEventLineType.Data -> data = value
|
1704
|
-
SseEventLineType.Retry -> retry = value.toInt()
|
1705
|
-
SseEventLineType.None -> {}
|
1726
|
+
__${clientName}SseEventLineType.Id -> id = value
|
1727
|
+
__${clientName}SseEventLineType.Event -> event = value
|
1728
|
+
__${clientName}SseEventLineType.Data -> data = value
|
1729
|
+
__${clientName}SseEventLineType.Retry -> retry = value.toInt()
|
1730
|
+
__${clientName}SseEventLineType.None -> {}
|
1706
1731
|
}
|
1707
1732
|
}
|
1708
1733
|
val isEnd = line == ""
|
1709
1734
|
if (isEnd) {
|
1710
1735
|
if (data != null) {
|
1711
1736
|
events.add(
|
1712
|
-
|
1737
|
+
__${clientName}SseEvent(
|
1713
1738
|
id = id,
|
1714
1739
|
event = event ?: "message",
|
1715
1740
|
data = data!!,
|
@@ -1724,7 +1749,7 @@ private fun __parseSseEvents(input: String): __SseEventParsingResult {
|
|
1724
1749
|
lastIndex = if (index + 1 < lines.size) index + 1 else null
|
1725
1750
|
}
|
1726
1751
|
}
|
1727
|
-
return
|
1752
|
+
return __${clientName}SseEventParsingResult(
|
1728
1753
|
events = events,
|
1729
1754
|
leftover = if (lastIndex != null) lines.subList(lastIndex!!, lines.size).joinToString(separator = "\\n") else ""
|
1730
1755
|
)
|
@@ -1736,7 +1761,7 @@ private suspend fun __handleSseRequest(
|
|
1736
1761
|
url: String,
|
1737
1762
|
method: HttpMethod,
|
1738
1763
|
params: ${clientName}Model?,
|
1739
|
-
headers:
|
1764
|
+
headers: __${clientName}HeadersFn,
|
1740
1765
|
backoffTime: Long,
|
1741
1766
|
maxBackoffTime: Long,
|
1742
1767
|
lastEventId: String?,
|
@@ -1977,7 +2002,7 @@ private val JsonInstance = Json {
|
|
1977
2002
|
encodeDefaults = true
|
1978
2003
|
ignoreUnknownKeys = true
|
1979
2004
|
}
|
1980
|
-
private typealias
|
2005
|
+
private typealias __${options.clientName}HeadersFn = (() -> MutableMap<String, String>?)?`;
|
1981
2006
|
}
|
1982
2007
|
|
1983
2008
|
exports.kotlinClientFromAppDefinition = kotlinClientFromAppDefinition;
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.mjs
CHANGED
@@ -59,7 +59,7 @@ function getClassName(schema, context) {
|
|
59
59
|
normalize: true
|
60
60
|
})
|
61
61
|
);
|
62
|
-
return
|
62
|
+
return className2;
|
63
63
|
}
|
64
64
|
const depth = instanceDepth(context);
|
65
65
|
if (depth === 1 && !context.discriminatorKey) {
|
@@ -68,7 +68,7 @@ function getClassName(schema, context) {
|
|
68
68
|
normalize: true
|
69
69
|
})
|
70
70
|
);
|
71
|
-
return
|
71
|
+
return className2;
|
72
72
|
}
|
73
73
|
if (context.discriminatorParentId && context.discriminatorKey && context.discriminatorValue) {
|
74
74
|
const className2 = kotlinClassName(
|
@@ -77,7 +77,7 @@ function getClassName(schema, context) {
|
|
77
77
|
{ normalize: true }
|
78
78
|
)
|
79
79
|
);
|
80
|
-
return
|
80
|
+
return className2;
|
81
81
|
}
|
82
82
|
const className = kotlinClassName(
|
83
83
|
pascalCase(
|
@@ -87,7 +87,7 @@ function getClassName(schema, context) {
|
|
87
87
|
}
|
88
88
|
)
|
89
89
|
);
|
90
|
-
return
|
90
|
+
return className;
|
91
91
|
}
|
92
92
|
function instanceDepth(context) {
|
93
93
|
const parts = context.instancePath.split("/");
|
@@ -124,6 +124,7 @@ function kotlinAnyFromSchema(schema, context) {
|
|
124
124
|
const defaultValue = nullable ? "null" : "JsonNull";
|
125
125
|
return {
|
126
126
|
typeName: "JsonElement",
|
127
|
+
prefixedTypeName: "JsonElement",
|
127
128
|
isNullable: nullable,
|
128
129
|
defaultValue,
|
129
130
|
fromJson(input) {
|
@@ -172,9 +173,10 @@ function kotlinArrayFromSchema(schema, context) {
|
|
172
173
|
schemaPath: `${context.schemaPath}/elements`,
|
173
174
|
existingTypeIds: context.existingTypeIds
|
174
175
|
});
|
175
|
-
const typeName = `MutableList<${subType.
|
176
|
+
const typeName = `MutableList<${subType.prefixedTypeName}${subType.isNullable ? "?" : ""}>`;
|
176
177
|
return {
|
177
178
|
typeName,
|
179
|
+
prefixedTypeName: typeName,
|
178
180
|
isNullable: nullable,
|
179
181
|
defaultValue,
|
180
182
|
fromJson(input) {
|
@@ -240,16 +242,18 @@ function kotlinArrayFromSchema(schema, context) {
|
|
240
242
|
|
241
243
|
function kotlinObjectFromSchema(schema, context) {
|
242
244
|
const className = getClassName(schema, context);
|
245
|
+
const prefixedClassName = `${context.typePrefix}${className}`;
|
243
246
|
const nullable = isNullable(schema, context);
|
244
|
-
const defaultValue = nullable ? "null" : `${
|
247
|
+
const defaultValue = nullable ? "null" : `${prefixedClassName}.new()`;
|
245
248
|
const result = {
|
246
249
|
typeName: className,
|
250
|
+
prefixedTypeName: prefixedClassName,
|
247
251
|
isNullable: nullable,
|
248
252
|
defaultValue,
|
249
253
|
fromJson(input, key) {
|
250
254
|
if (nullable) {
|
251
255
|
return `when (${input}) {
|
252
|
-
is JsonObject -> ${
|
256
|
+
is JsonObject -> ${prefixedClassName}.fromJsonElement(
|
253
257
|
${input}!!,
|
254
258
|
"$instancePath/${key}",
|
255
259
|
)
|
@@ -257,12 +261,12 @@ function kotlinObjectFromSchema(schema, context) {
|
|
257
261
|
}`;
|
258
262
|
}
|
259
263
|
return `when (${input}) {
|
260
|
-
is JsonObject -> ${
|
264
|
+
is JsonObject -> ${prefixedClassName}.fromJsonElement(
|
261
265
|
${input}!!,
|
262
266
|
"$instancePath/${key}",
|
263
267
|
)
|
264
268
|
|
265
|
-
else -> ${
|
269
|
+
else -> ${prefixedClassName}.new()
|
266
270
|
}`;
|
267
271
|
},
|
268
272
|
toJson(input, target) {
|
@@ -317,7 +321,7 @@ function kotlinObjectFromSchema(schema, context) {
|
|
317
321
|
subContent.push(type.content);
|
318
322
|
}
|
319
323
|
fieldParts.push(
|
320
|
-
`${getCodeComment(prop.metadata, " ", "field")} val ${kotlinKey}: ${type.
|
324
|
+
`${getCodeComment(prop.metadata, " ", "field")} val ${kotlinKey}: ${type.prefixedTypeName}${type.isNullable ? "?" : ""},`
|
321
325
|
);
|
322
326
|
if (i === 0 && !context.discriminatorKey) {
|
323
327
|
toJsonParts.push(`output += "\\"${key}\\":"`);
|
@@ -327,7 +331,7 @@ function kotlinObjectFromSchema(schema, context) {
|
|
327
331
|
toJsonParts.push(type.toJson(kotlinKey, "output"));
|
328
332
|
toQueryParts.push(type.toQueryString(kotlinKey, "queryParts", key));
|
329
333
|
fromJsonParts.push(
|
330
|
-
`val ${kotlinKey}: ${type.
|
334
|
+
`val ${kotlinKey}: ${type.prefixedTypeName}${type.isNullable ? "?" : ""} = ${type.fromJson(`__input.jsonObject["${key}"]`, key)}`
|
331
335
|
);
|
332
336
|
defaultParts.push(
|
333
337
|
` ${kotlinKey} = ${type.defaultValue},`
|
@@ -355,7 +359,7 @@ function kotlinObjectFromSchema(schema, context) {
|
|
355
359
|
if (hasProperties) output += ","
|
356
360
|
`;
|
357
361
|
fieldParts.push(
|
358
|
-
`${getCodeComment(schema.optionalProperties[key].metadata, " ", "field")} val ${kotlinKey}: ${type.
|
362
|
+
`${getCodeComment(schema.optionalProperties[key].metadata, " ", "field")} val ${kotlinKey}: ${type.prefixedTypeName}? = null,`
|
359
363
|
);
|
360
364
|
if (hasKnownKeys) {
|
361
365
|
toJsonParts.push(`if (${kotlinKey} != null) {
|
@@ -371,20 +375,20 @@ ${isLast ? "" : " hasProperties = true"}
|
|
371
375
|
}
|
372
376
|
toQueryParts.push(type.toQueryString(kotlinKey, "queryParts", key));
|
373
377
|
fromJsonParts.push(
|
374
|
-
`val ${kotlinKey}: ${type.
|
378
|
+
`val ${kotlinKey}: ${type.prefixedTypeName}? = ${type.fromJson(`__input.jsonObject["${key}"]`, key)}`
|
375
379
|
);
|
376
380
|
}
|
377
381
|
toJsonParts.push('output += "}"');
|
378
382
|
toJsonParts.push("return output");
|
379
383
|
toQueryParts.push('return queryParts.joinToString("&")');
|
380
|
-
const implementedClass = context.discriminatorParentId
|
384
|
+
const implementedClass = context.discriminatorParentId ? `${context.typePrefix}${context.discriminatorParentId}` : `${context.clientName}Model`;
|
381
385
|
let discriminatorField = "";
|
382
386
|
if (context.discriminatorKey && context.discriminatorValue) {
|
383
387
|
discriminatorField = `
|
384
388
|
override val ${kotlinIdentifier(context.discriminatorKey)} get() = "${context.discriminatorValue}"
|
385
389
|
`;
|
386
390
|
}
|
387
|
-
const content = `${getCodeComment(schema.metadata, "", "class")}data class ${
|
391
|
+
const content = `${getCodeComment(schema.metadata, "", "class")}data class ${prefixedClassName}(
|
388
392
|
${fieldParts.join("\n")}
|
389
393
|
) : ${implementedClass} {${discriminatorField}
|
390
394
|
override fun toJson(): String {
|
@@ -395,27 +399,27 @@ ${toJsonParts.join("\n")}
|
|
395
399
|
${toQueryParts.join("\n")}
|
396
400
|
}
|
397
401
|
|
398
|
-
companion object Factory : ${context.clientName}ModelFactory<${
|
402
|
+
companion object Factory : ${context.clientName}ModelFactory<${prefixedClassName}> {
|
399
403
|
@JvmStatic
|
400
|
-
override fun new(): ${
|
401
|
-
return ${
|
404
|
+
override fun new(): ${prefixedClassName} {
|
405
|
+
return ${prefixedClassName}(
|
402
406
|
${defaultParts.join("\n")}
|
403
407
|
)
|
404
408
|
}
|
405
409
|
|
406
410
|
@JvmStatic
|
407
|
-
override fun fromJson(input: String): ${
|
411
|
+
override fun fromJson(input: String): ${prefixedClassName} {
|
408
412
|
return fromJsonElement(JsonInstance.parseToJsonElement(input))
|
409
413
|
}
|
410
414
|
|
411
415
|
@JvmStatic
|
412
|
-
override fun fromJsonElement(__input: JsonElement, instancePath: String): ${
|
416
|
+
override fun fromJsonElement(__input: JsonElement, instancePath: String): ${prefixedClassName} {
|
413
417
|
if (__input !is JsonObject) {
|
414
|
-
__logError("[WARNING] ${
|
418
|
+
__logError("[WARNING] ${prefixedClassName}.fromJsonElement() expected kotlinx.serialization.json.JsonObject at $instancePath. Got \${__input.javaClass}. Initializing empty ${prefixedClassName}.")
|
415
419
|
return new()
|
416
420
|
}
|
417
421
|
${fromJsonParts.join("\n")}
|
418
|
-
return ${
|
422
|
+
return ${prefixedClassName}(
|
419
423
|
${kotlinKeys.join(",\n ")},
|
420
424
|
)
|
421
425
|
}
|
@@ -433,6 +437,7 @@ ${subContent.join("\n\n")}`;
|
|
433
437
|
function kotlinDiscriminatorFromSchema(schema, context) {
|
434
438
|
const kotlinDiscriminatorKey = kotlinIdentifier(schema.discriminator);
|
435
439
|
const className = getClassName(schema, context);
|
440
|
+
const prefixedClassName = `${context.typePrefix}${className}`;
|
436
441
|
const nullable = isNullable(schema, context);
|
437
442
|
const subTypes = [];
|
438
443
|
const subContent = [];
|
@@ -451,6 +456,7 @@ function kotlinDiscriminatorFromSchema(schema, context) {
|
|
451
456
|
});
|
452
457
|
subTypes.push({
|
453
458
|
typeName: subType.typeName,
|
459
|
+
prefixedTypeName: subType.prefixedTypeName,
|
454
460
|
discriminatorValue: key
|
455
461
|
});
|
456
462
|
if (subType.content) {
|
@@ -460,14 +466,15 @@ function kotlinDiscriminatorFromSchema(schema, context) {
|
|
460
466
|
if (subTypes.length === 0) {
|
461
467
|
throw new Error("Discriminator schemas must have at least one mapping");
|
462
468
|
}
|
463
|
-
const defaultValue = nullable ? "null" : `${
|
469
|
+
const defaultValue = nullable ? "null" : `${prefixedClassName}.new()`;
|
464
470
|
const result = {
|
465
471
|
typeName: className,
|
472
|
+
prefixedTypeName: prefixedClassName,
|
466
473
|
isNullable: nullable,
|
467
474
|
defaultValue,
|
468
475
|
fromJson(input, key) {
|
469
476
|
return `when (${input}) {
|
470
|
-
is JsonObject -> ${
|
477
|
+
is JsonObject -> ${prefixedClassName}.fromJsonElement(
|
471
478
|
${input}!!,
|
472
479
|
"$instancePath/${key}",
|
473
480
|
)
|
@@ -489,29 +496,29 @@ function kotlinDiscriminatorFromSchema(schema, context) {
|
|
489
496
|
return result;
|
490
497
|
}
|
491
498
|
const codeComment = getCodeComment(schema.metadata, "", "class");
|
492
|
-
const content = `${codeComment}sealed interface ${
|
499
|
+
const content = `${codeComment}sealed interface ${prefixedClassName} : ${context.clientName}Model {
|
493
500
|
val ${kotlinDiscriminatorKey}: String
|
494
501
|
|
495
|
-
companion object Factory : ${context.clientName}ModelFactory<${
|
502
|
+
companion object Factory : ${context.clientName}ModelFactory<${prefixedClassName}> {
|
496
503
|
@JvmStatic
|
497
|
-
override fun new(): ${
|
498
|
-
return ${subTypes[0].
|
504
|
+
override fun new(): ${prefixedClassName} {
|
505
|
+
return ${subTypes[0].prefixedTypeName}.new()
|
499
506
|
}
|
500
507
|
|
501
508
|
@JvmStatic
|
502
|
-
override fun fromJson(input: String): ${
|
509
|
+
override fun fromJson(input: String): ${prefixedClassName} {
|
503
510
|
return fromJsonElement(JsonInstance.parseToJsonElement(input))
|
504
511
|
}
|
505
512
|
|
506
513
|
@JvmStatic
|
507
|
-
override fun fromJsonElement(__input: JsonElement, instancePath: String): ${
|
514
|
+
override fun fromJsonElement(__input: JsonElement, instancePath: String): ${prefixedClassName} {
|
508
515
|
if (__input !is JsonObject) {
|
509
|
-
__logError("[WARNING] Discriminator.fromJsonElement() expected kotlinx.serialization.json.JsonObject at $instancePath. Got \${__input.javaClass}. Initializing empty ${
|
516
|
+
__logError("[WARNING] Discriminator.fromJsonElement() expected kotlinx.serialization.json.JsonObject at $instancePath. Got \${__input.javaClass}. Initializing empty ${prefixedClassName}.")
|
510
517
|
return new()
|
511
518
|
}
|
512
519
|
return when (__input.jsonObject["${schema.discriminator}"]) {
|
513
520
|
is JsonPrimitive -> when (__input.jsonObject["${schema.discriminator}"]!!.jsonPrimitive.contentOrNull) {
|
514
|
-
${subTypes.map((type) => `"${type.discriminatorValue}" -> ${type.
|
521
|
+
${subTypes.map((type) => `"${type.discriminatorValue}" -> ${type.prefixedTypeName}.fromJsonElement(__input, instancePath)`).join("\n")}
|
515
522
|
else -> new()
|
516
523
|
}
|
517
524
|
|
@@ -532,6 +539,7 @@ ${subContent.join("\n\n")}`;
|
|
532
539
|
function kotlinEnumFromSchema(schema, context) {
|
533
540
|
const nullable = isNullable(schema, context);
|
534
541
|
const className = getClassName(schema, context);
|
542
|
+
const prefixedClassName = `${context.typePrefix}${className}`;
|
535
543
|
const enumItems = schema.enum.map((val) => {
|
536
544
|
return {
|
537
545
|
name: pascalCase(val, { normalize: true }),
|
@@ -543,24 +551,24 @@ function kotlinEnumFromSchema(schema, context) {
|
|
543
551
|
`Enum schemas must have at least one enum value. At ${context.schemaPath}.`
|
544
552
|
);
|
545
553
|
}
|
546
|
-
const defaultValue = nullable ? "null" : `${
|
554
|
+
const defaultValue = nullable ? "null" : `${prefixedClassName}.new()`;
|
547
555
|
let content = "";
|
548
556
|
if (!context.existingTypeIds.includes(className)) {
|
549
|
-
content = `enum class ${
|
557
|
+
content = `enum class ${prefixedClassName} {
|
550
558
|
${enumItems.map((item) => item.name).join(",\n ")};
|
551
559
|
val serialValue: String
|
552
560
|
get() = when (this) {
|
553
561
|
${enumItems.map((item) => `${item.name} -> "${item.value}"`).join("\n ")}
|
554
562
|
}
|
555
563
|
|
556
|
-
companion object Factory : ${context.clientName}ModelFactory<${
|
564
|
+
companion object Factory : ${context.clientName}ModelFactory<${prefixedClassName}> {
|
557
565
|
@JvmStatic
|
558
|
-
override fun new(): ${
|
566
|
+
override fun new(): ${prefixedClassName} {
|
559
567
|
return ${enumItems[0].name}
|
560
568
|
}
|
561
569
|
|
562
570
|
@JvmStatic
|
563
|
-
override fun fromJson(input: String): ${
|
571
|
+
override fun fromJson(input: String): ${prefixedClassName} {
|
564
572
|
return when (input) {
|
565
573
|
${enumItems.map((item) => `${item.name}.serialValue -> ${item.name}`).join("\n ")}
|
566
574
|
else -> ${enumItems[0].name}
|
@@ -568,9 +576,9 @@ function kotlinEnumFromSchema(schema, context) {
|
|
568
576
|
}
|
569
577
|
|
570
578
|
@JvmStatic
|
571
|
-
override fun fromJsonElement(__input: JsonElement, instancePath: String): ${
|
579
|
+
override fun fromJsonElement(__input: JsonElement, instancePath: String): ${prefixedClassName} {
|
572
580
|
if (__input !is JsonPrimitive) {
|
573
|
-
__logError("[WARNING] ${
|
581
|
+
__logError("[WARNING] ${prefixedClassName}.fromJsonElement() expected kotlinx.serialization.json.JsonPrimitive at $instancePath. Got \${__input.javaClass}. Initializing empty ${prefixedClassName}.")
|
574
582
|
return new()
|
575
583
|
}
|
576
584
|
return when (__input.jsonPrimitive.contentOrNull) {
|
@@ -584,26 +592,27 @@ function kotlinEnumFromSchema(schema, context) {
|
|
584
592
|
}
|
585
593
|
return {
|
586
594
|
typeName: className,
|
595
|
+
prefixedTypeName: prefixedClassName,
|
587
596
|
isNullable: nullable,
|
588
597
|
defaultValue,
|
589
598
|
fromJson(input, key) {
|
590
599
|
if (nullable) {
|
591
600
|
return `when (${input}) {
|
592
601
|
is JsonNull -> null
|
593
|
-
is JsonPrimitive -> ${
|
602
|
+
is JsonPrimitive -> ${prefixedClassName}.fromJsonElement(${input}!!, "$instancePath/${key ?? ""}")
|
594
603
|
else -> null
|
595
604
|
}`;
|
596
605
|
}
|
597
606
|
return `when (${input}) {
|
598
607
|
is JsonNull -> ${defaultValue}
|
599
|
-
is JsonPrimitive -> ${
|
608
|
+
is JsonPrimitive -> ${prefixedClassName}.fromJsonElement(${input}!!, "$instancePath/${key}")
|
600
609
|
else -> ${defaultValue}
|
601
610
|
}`;
|
602
611
|
},
|
603
612
|
toJson(input, target) {
|
604
613
|
if (schema.nullable) {
|
605
614
|
return `${target} += when (${input}) {
|
606
|
-
is ${
|
615
|
+
is ${prefixedClassName} -> "\\"\${${input}.serialValue}\\""
|
607
616
|
else -> "null"
|
608
617
|
}`;
|
609
618
|
}
|
@@ -634,10 +643,11 @@ function kotlinMapFromSchema(schema, context) {
|
|
634
643
|
schemaPath: `${context.schemaPath}/values`,
|
635
644
|
existingTypeIds: context.existingTypeIds
|
636
645
|
});
|
637
|
-
const typeName = `MutableMap<String, ${subType.
|
646
|
+
const typeName = `MutableMap<String, ${subType.prefixedTypeName}${subType.isNullable ? "?" : ""}>`;
|
638
647
|
const defaultValue = nullable ? "null" : "mutableMapOf()";
|
639
648
|
return {
|
640
649
|
typeName,
|
650
|
+
prefixedTypeName: typeName,
|
641
651
|
isNullable: nullable,
|
642
652
|
defaultValue,
|
643
653
|
fromJson(input, key) {
|
@@ -702,6 +712,7 @@ function kotlinStringFromSchema(schema, context) {
|
|
702
712
|
const defaultValue = nullable ? "null" : '""';
|
703
713
|
return {
|
704
714
|
typeName: "String",
|
715
|
+
prefixedTypeName: "String",
|
705
716
|
isNullable: nullable,
|
706
717
|
defaultValue,
|
707
718
|
fromJson(input) {
|
@@ -736,6 +747,7 @@ function kotlinBooleanFromSchema(schema, context) {
|
|
736
747
|
const defaultValue = nullable ? "null" : "false";
|
737
748
|
return {
|
738
749
|
typeName: "Boolean",
|
750
|
+
prefixedTypeName: "Boolean",
|
739
751
|
isNullable: nullable,
|
740
752
|
defaultValue,
|
741
753
|
fromJson(input) {
|
@@ -764,6 +776,7 @@ function kotlinTimestampFromSchema(schema, context) {
|
|
764
776
|
const defaultValue = nullable ? "null" : "Instant.now()";
|
765
777
|
return {
|
766
778
|
typeName: "Instant",
|
779
|
+
prefixedTypeName: "Instant",
|
767
780
|
isNullable: nullable,
|
768
781
|
defaultValue,
|
769
782
|
fromJson(input) {
|
@@ -819,6 +832,7 @@ function kotlinFloat32FromSchema(schema, context) {
|
|
819
832
|
const defaultValue = nullable ? "null" : "0.0F";
|
820
833
|
return {
|
821
834
|
typeName: "Float",
|
835
|
+
prefixedTypeName: "Float",
|
822
836
|
isNullable: nullable,
|
823
837
|
defaultValue,
|
824
838
|
fromJson(input) {
|
@@ -847,6 +861,7 @@ function kotlinFloat64FromSchema(schema, context) {
|
|
847
861
|
const defaultValue = nullable ? "null" : "0.0";
|
848
862
|
return {
|
849
863
|
typeName: "Double",
|
864
|
+
prefixedTypeName: "Double",
|
850
865
|
isNullable: nullable,
|
851
866
|
defaultValue,
|
852
867
|
fromJson(input) {
|
@@ -875,6 +890,7 @@ function kotlinInt8FromSchema(schema, context) {
|
|
875
890
|
const defaultValue = nullable ? "null" : "0";
|
876
891
|
return {
|
877
892
|
typeName: "Byte",
|
893
|
+
prefixedTypeName: "Byte",
|
878
894
|
isNullable: nullable,
|
879
895
|
defaultValue,
|
880
896
|
fromJson(input) {
|
@@ -903,6 +919,7 @@ function kotlinInt16FromSchema(schema, context) {
|
|
903
919
|
const defaultValue = nullable ? "null" : "0";
|
904
920
|
return {
|
905
921
|
typeName: "Short",
|
922
|
+
prefixedTypeName: "Short",
|
906
923
|
isNullable: nullable,
|
907
924
|
defaultValue,
|
908
925
|
fromJson(input) {
|
@@ -931,6 +948,7 @@ function kotlinInt32FromSchema(schema, context) {
|
|
931
948
|
const defaultValue = nullable ? "null" : "0";
|
932
949
|
return {
|
933
950
|
typeName: "Int",
|
951
|
+
prefixedTypeName: "Int",
|
934
952
|
isNullable: nullable,
|
935
953
|
defaultValue,
|
936
954
|
fromJson(input) {
|
@@ -959,6 +977,7 @@ function kotlinInt64FromSchema(schema, context) {
|
|
959
977
|
const defaultValue = nullable ? "null" : "0L";
|
960
978
|
return {
|
961
979
|
typeName: "Long",
|
980
|
+
prefixedTypeName: "Long",
|
962
981
|
isNullable: nullable,
|
963
982
|
defaultValue,
|
964
983
|
fromJson(input) {
|
@@ -993,6 +1012,7 @@ function kotlinUint8FromSchema(schema, context) {
|
|
993
1012
|
const defaultValue = nullable ? "null" : "0u";
|
994
1013
|
return {
|
995
1014
|
typeName: "UByte",
|
1015
|
+
prefixedTypeName: "UByte",
|
996
1016
|
isNullable: nullable,
|
997
1017
|
defaultValue,
|
998
1018
|
fromJson(input) {
|
@@ -1021,6 +1041,7 @@ function kotlinUint16FromSchema(schema, context) {
|
|
1021
1041
|
const defaultValue = nullable ? "null" : "0u";
|
1022
1042
|
return {
|
1023
1043
|
typeName: "UShort",
|
1044
|
+
prefixedTypeName: "UShort",
|
1024
1045
|
isNullable: nullable,
|
1025
1046
|
defaultValue,
|
1026
1047
|
fromJson(input) {
|
@@ -1049,6 +1070,7 @@ function kotlinUint32FromSchema(schema, context) {
|
|
1049
1070
|
const defaultValue = nullable ? "null" : "0u";
|
1050
1071
|
return {
|
1051
1072
|
typeName: "UInt",
|
1073
|
+
prefixedTypeName: "UInt",
|
1052
1074
|
isNullable: nullable,
|
1053
1075
|
defaultValue,
|
1054
1076
|
fromJson(input) {
|
@@ -1077,6 +1099,7 @@ function kotlinUint64FromSchema(schema, context) {
|
|
1077
1099
|
const defaultValue = nullable ? "null" : "0UL";
|
1078
1100
|
return {
|
1079
1101
|
typeName: "ULong",
|
1102
|
+
prefixedTypeName: "ULong",
|
1080
1103
|
isNullable: nullable,
|
1081
1104
|
defaultValue,
|
1082
1105
|
fromJson(input) {
|
@@ -1236,7 +1259,7 @@ function kotlinServiceFromSchema(schema, context) {
|
|
1236
1259
|
content: `class ${name}(
|
1237
1260
|
private val httpClient: HttpClient,
|
1238
1261
|
private val baseUrl: String,
|
1239
|
-
private val headers:
|
1262
|
+
private val headers: __${context.clientName}HeadersFn,
|
1240
1263
|
private val onError: ((err: Exception) -> Unit) = {},
|
1241
1264
|
) {
|
1242
1265
|
${procedureParts.join("\n\n ")}
|
@@ -1256,15 +1279,17 @@ function getServiceName(context) {
|
|
1256
1279
|
|
1257
1280
|
function kotlinRefFromSchema(schema, context) {
|
1258
1281
|
const typeName = kotlinClassName(schema.ref);
|
1282
|
+
const prefixedTypeName = `${context.typePrefix}${typeName}`;
|
1259
1283
|
const nullable = isNullable(schema, context);
|
1260
|
-
const defaultValue = nullable ? "null" : `${
|
1284
|
+
const defaultValue = nullable ? "null" : `${prefixedTypeName}.new()`;
|
1261
1285
|
return {
|
1262
1286
|
typeName,
|
1287
|
+
prefixedTypeName,
|
1263
1288
|
isNullable: nullable,
|
1264
1289
|
defaultValue,
|
1265
1290
|
fromJson(input, key) {
|
1266
1291
|
return `when (${input}) {
|
1267
|
-
is JsonObject -> ${
|
1292
|
+
is JsonObject -> ${prefixedTypeName}.fromJsonElement(
|
1268
1293
|
${input}!!,
|
1269
1294
|
"$instancePath/${key}",
|
1270
1295
|
)
|
@@ -1366,7 +1391,7 @@ ${getUtilityFunctions(clientName)}`;
|
|
1366
1391
|
class ${clientName}(
|
1367
1392
|
private val httpClient: HttpClient,
|
1368
1393
|
private val baseUrl: String,
|
1369
|
-
private val headers:
|
1394
|
+
private val headers: __${clientName}HeadersFn,
|
1370
1395
|
private val onError: ((err: Exception) -> Unit) = {},
|
1371
1396
|
) {
|
1372
1397
|
${procedureParts.join("\n\n ")}
|
@@ -1548,7 +1573,7 @@ data class ${clientName}Error(
|
|
1548
1573
|
}
|
1549
1574
|
function getUtilityFunctions(clientName) {
|
1550
1575
|
return `// Implementation copied from https://github.com/Kotlin/kotlinx.serialization/blob/d0ae697b9394103879e6c7f836d0f7cf128f4b1e/formats/json/commonMain/src/kotlinx/serialization/json/internal/StringOps.kt#L45
|
1551
|
-
|
1576
|
+
private const val STRING = '"'
|
1552
1577
|
|
1553
1578
|
private fun toHexChar(i: Int): Char {
|
1554
1579
|
val d = i and 0xf
|
@@ -1556,7 +1581,7 @@ private fun toHexChar(i: Int): Char {
|
|
1556
1581
|
else (d - 10 + 'a'.code).toChar()
|
1557
1582
|
}
|
1558
1583
|
|
1559
|
-
|
1584
|
+
private val ESCAPE_STRINGS: Array<String?> = arrayOfNulls<String>(93).apply {
|
1560
1585
|
for (c in 0..0x1f) {
|
1561
1586
|
val c1 = toHexChar(c shr 12)
|
1562
1587
|
val c2 = toHexChar(c shr 8)
|
@@ -1573,7 +1598,7 @@ internal val ESCAPE_STRINGS: Array<String?> = arrayOfNulls<String>(93).apply {
|
|
1573
1598
|
this[0x0c] = "\\\\f"
|
1574
1599
|
}
|
1575
1600
|
|
1576
|
-
|
1601
|
+
private val ESCAPE_MARKERS: ByteArray = ByteArray(93).apply {
|
1577
1602
|
for (c in 0..0x1f) {
|
1578
1603
|
this[c] = 1.toByte()
|
1579
1604
|
}
|
@@ -1586,7 +1611,7 @@ internal val ESCAPE_MARKERS: ByteArray = ByteArray(93).apply {
|
|
1586
1611
|
this[0x0c] = 'f'.code.toByte()
|
1587
1612
|
}
|
1588
1613
|
|
1589
|
-
|
1614
|
+
private fun StringBuilder.printQuoted(value: String) {
|
1590
1615
|
append(STRING)
|
1591
1616
|
var lastPos = 0
|
1592
1617
|
for (i in value.indices) {
|
@@ -1644,7 +1669,7 @@ private suspend fun __prepareRequest(
|
|
1644
1669
|
}
|
1645
1670
|
|
1646
1671
|
// SSE_FN_START
|
1647
|
-
private enum class SseEventLineType {
|
1672
|
+
private enum class __${clientName}SseEventLineType {
|
1648
1673
|
Id,
|
1649
1674
|
Event,
|
1650
1675
|
Data,
|
@@ -1652,36 +1677,36 @@ private enum class SseEventLineType {
|
|
1652
1677
|
None,
|
1653
1678
|
}
|
1654
1679
|
|
1655
|
-
private fun __parseSseEventLine(line: String): Pair<SseEventLineType, String> {
|
1680
|
+
private fun __parseSseEventLine(line: String): Pair<__${clientName}SseEventLineType, String> {
|
1656
1681
|
if (line.startsWith("id:")) {
|
1657
|
-
return Pair(SseEventLineType.Id, line.substring(3).trim())
|
1682
|
+
return Pair(__${clientName}SseEventLineType.Id, line.substring(3).trim())
|
1658
1683
|
}
|
1659
1684
|
if (line.startsWith("event:")) {
|
1660
|
-
return Pair(SseEventLineType.Event, line.substring(6).trim())
|
1685
|
+
return Pair(__${clientName}SseEventLineType.Event, line.substring(6).trim())
|
1661
1686
|
}
|
1662
1687
|
if (line.startsWith("data:")) {
|
1663
|
-
return Pair(SseEventLineType.Data, line.substring(5).trim())
|
1688
|
+
return Pair(__${clientName}SseEventLineType.Data, line.substring(5).trim())
|
1664
1689
|
}
|
1665
1690
|
if (line.startsWith("retry:")) {
|
1666
|
-
return Pair(SseEventLineType.Retry, line.substring(6).trim())
|
1691
|
+
return Pair(__${clientName}SseEventLineType.Retry, line.substring(6).trim())
|
1667
1692
|
}
|
1668
|
-
return Pair(SseEventLineType.None, "")
|
1693
|
+
return Pair(__${clientName}SseEventLineType.None, "")
|
1669
1694
|
}
|
1670
1695
|
|
1671
|
-
private data class
|
1696
|
+
private data class __${clientName}SseEvent(
|
1672
1697
|
val id: String? = null,
|
1673
1698
|
val event: String,
|
1674
1699
|
val data: String,
|
1675
1700
|
val retry: Int? = null
|
1676
1701
|
)
|
1677
1702
|
|
1678
|
-
private class
|
1703
|
+
private class __${clientName}SseEventParsingResult(val events: List<__${clientName}SseEvent>, val leftover: String)
|
1679
1704
|
|
1680
|
-
private fun __parseSseEvents(input: String):
|
1681
|
-
val events = mutableListOf<
|
1705
|
+
private fun __parseSseEvents(input: String): __${clientName}SseEventParsingResult {
|
1706
|
+
val events = mutableListOf<__${clientName}SseEvent>()
|
1682
1707
|
val lines = input.lines()
|
1683
1708
|
if (lines.isEmpty()) {
|
1684
|
-
return
|
1709
|
+
return __${clientName}SseEventParsingResult(events = listOf(), leftover = "")
|
1685
1710
|
}
|
1686
1711
|
var id: String? = null
|
1687
1712
|
var event: String? = null
|
@@ -1692,18 +1717,18 @@ private fun __parseSseEvents(input: String): __SseEventParsingResult {
|
|
1692
1717
|
if (line.isNotEmpty()) {
|
1693
1718
|
val (type, value) = __parseSseEventLine(line)
|
1694
1719
|
when (type) {
|
1695
|
-
SseEventLineType.Id -> id = value
|
1696
|
-
SseEventLineType.Event -> event = value
|
1697
|
-
SseEventLineType.Data -> data = value
|
1698
|
-
SseEventLineType.Retry -> retry = value.toInt()
|
1699
|
-
SseEventLineType.None -> {}
|
1720
|
+
__${clientName}SseEventLineType.Id -> id = value
|
1721
|
+
__${clientName}SseEventLineType.Event -> event = value
|
1722
|
+
__${clientName}SseEventLineType.Data -> data = value
|
1723
|
+
__${clientName}SseEventLineType.Retry -> retry = value.toInt()
|
1724
|
+
__${clientName}SseEventLineType.None -> {}
|
1700
1725
|
}
|
1701
1726
|
}
|
1702
1727
|
val isEnd = line == ""
|
1703
1728
|
if (isEnd) {
|
1704
1729
|
if (data != null) {
|
1705
1730
|
events.add(
|
1706
|
-
|
1731
|
+
__${clientName}SseEvent(
|
1707
1732
|
id = id,
|
1708
1733
|
event = event ?: "message",
|
1709
1734
|
data = data!!,
|
@@ -1718,7 +1743,7 @@ private fun __parseSseEvents(input: String): __SseEventParsingResult {
|
|
1718
1743
|
lastIndex = if (index + 1 < lines.size) index + 1 else null
|
1719
1744
|
}
|
1720
1745
|
}
|
1721
|
-
return
|
1746
|
+
return __${clientName}SseEventParsingResult(
|
1722
1747
|
events = events,
|
1723
1748
|
leftover = if (lastIndex != null) lines.subList(lastIndex!!, lines.size).joinToString(separator = "\\n") else ""
|
1724
1749
|
)
|
@@ -1730,7 +1755,7 @@ private suspend fun __handleSseRequest(
|
|
1730
1755
|
url: String,
|
1731
1756
|
method: HttpMethod,
|
1732
1757
|
params: ${clientName}Model?,
|
1733
|
-
headers:
|
1758
|
+
headers: __${clientName}HeadersFn,
|
1734
1759
|
backoffTime: Long,
|
1735
1760
|
maxBackoffTime: Long,
|
1736
1761
|
lastEventId: String?,
|
@@ -1971,7 +1996,7 @@ private val JsonInstance = Json {
|
|
1971
1996
|
encodeDefaults = true
|
1972
1997
|
ignoreUnknownKeys = true
|
1973
1998
|
}
|
1974
|
-
private typealias
|
1999
|
+
private typealias __${options.clientName}HeadersFn = (() -> MutableMap<String, String>?)?`;
|
1975
2000
|
}
|
1976
2001
|
|
1977
2002
|
export { kotlinClientFromAppDefinition, kotlinClientGenerator, kotlinTypeFromSchema };
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@arrirpc/codegen-kotlin",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.74.0",
|
4
4
|
"type": "module",
|
5
5
|
"license": "MIT",
|
6
6
|
"author": {
|
@@ -22,6 +22,6 @@
|
|
22
22
|
"dist"
|
23
23
|
],
|
24
24
|
"dependencies": {
|
25
|
-
"@arrirpc/codegen-utils": "0.
|
25
|
+
"@arrirpc/codegen-utils": "0.74.0"
|
26
26
|
}
|
27
27
|
}
|