zod-to-x 2.0.1 → 2.0.2-dev.2
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.
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ZodDiscriminatedUnion, ZodUnion } from "zod";
|
|
1
2
|
import { IZod2AstOpt } from "../../core";
|
|
2
3
|
import { ZodObject } from "../../lib/zod_helpers";
|
|
3
4
|
import { IZod2ProtoV3Opt } from "./options";
|
|
@@ -16,4 +17,5 @@ import { IZod2ProtoV3Opt } from "./options";
|
|
|
16
17
|
* definition.
|
|
17
18
|
* @returns The Protocol Buffers v3 definition as a string.
|
|
18
19
|
*/
|
|
19
|
-
export declare function zod2ProtoV3(schema: ZodObject<any
|
|
20
|
+
export declare function zod2ProtoV3(schema: ZodObject<any> | ZodDiscriminatedUnion<string, any> | ZodUnion<any>, // TODO: fix any to force only ZodObjects
|
|
21
|
+
opt?: Pick<IZod2AstOpt, "strict"> & Pick<IZod2ProtoV3Opt, "packageName" | "keepKeys" | "header" | "indent" | "includeComments" | "encodeDoubleAsInt">): string;
|
|
@@ -243,7 +243,8 @@ class Zod2ProtoV3 extends core_1.Zod2X {
|
|
|
243
243
|
* definition.
|
|
244
244
|
* @returns The Protocol Buffers v3 definition as a string.
|
|
245
245
|
*/
|
|
246
|
-
function zod2ProtoV3(schema,
|
|
246
|
+
function zod2ProtoV3(schema, // TODO: fix any to force only ZodObjects
|
|
247
|
+
opt = {}) {
|
|
247
248
|
const astNode = new core_1.Zod2Ast({ strict: opt.strict }).build(schema);
|
|
248
249
|
return new Zod2ProtoV3(opt).transpile(astNode);
|
|
249
250
|
}
|
|
@@ -415,13 +415,14 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
415
415
|
* @param childs - Structure attributes data.
|
|
416
416
|
*/
|
|
417
417
|
_createStructSerializer(parent, childs) {
|
|
418
|
+
const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
|
|
418
419
|
this._push0(this.serializers, `inline void to_json(json& j, const ${parent}& x) {`);
|
|
419
420
|
childs.forEach((i) => {
|
|
420
421
|
if (i.required) {
|
|
421
422
|
this._push1(this.serializers, `j["${i.origName}"] = x.${i.snakeName};`);
|
|
422
423
|
}
|
|
423
424
|
else {
|
|
424
|
-
this._push1(this.serializers,
|
|
425
|
+
this._push1(this.serializers, `${prefix}set_opt<${i.typeName}>(j, "${i.origName}", x.${i.snakeName});`);
|
|
425
426
|
}
|
|
426
427
|
});
|
|
427
428
|
this._push0(this.serializers, "}\n");
|
|
@@ -438,13 +439,14 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
438
439
|
* @param childs - Structure attributes data.
|
|
439
440
|
*/
|
|
440
441
|
_createStructDeserializer(parent, childs) {
|
|
442
|
+
const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
|
|
441
443
|
this._push0(this.serializers, `inline void from_json(const json& j, ${parent}& x) {`);
|
|
442
444
|
childs.forEach((i) => {
|
|
443
445
|
if (i.required) {
|
|
444
446
|
this._push1(this.serializers, `x.${i.snakeName} = j.at("${i.origName}").get<${i.typeName}>();`);
|
|
445
447
|
}
|
|
446
448
|
else {
|
|
447
|
-
this._push1(this.serializers, `x.${i.snakeName} = get_opt<${i.typeName}>(j, "${i.origName}");`);
|
|
449
|
+
this._push1(this.serializers, `x.${i.snakeName} = ${prefix}get_opt<${i.typeName}>(j, "${i.origName}");`);
|
|
448
450
|
}
|
|
449
451
|
});
|
|
450
452
|
this._push0(this.serializers, "}\n");
|
|
@@ -464,13 +466,14 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
464
466
|
* @param childs - Structure attributes data.
|
|
465
467
|
*/
|
|
466
468
|
_createClassSerializer(parent, childs) {
|
|
469
|
+
const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
|
|
467
470
|
this._push0(this.serializers, `inline void to_json(json& j, const ${parent}& x) {`);
|
|
468
471
|
childs.forEach((i) => {
|
|
469
472
|
if (i.required) {
|
|
470
473
|
this._push1(this.serializers, `j["${i.origName}"] = x.get_${i.snakeName}();`);
|
|
471
474
|
}
|
|
472
475
|
else {
|
|
473
|
-
this._push1(this.serializers,
|
|
476
|
+
this._push1(this.serializers, `${prefix}set_opt<${i.typeName}>(j, "${i.origName}", x.get_${i.snakeName}());`);
|
|
474
477
|
}
|
|
475
478
|
});
|
|
476
479
|
this._push0(this.serializers, "}\n");
|
|
@@ -487,13 +490,14 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
487
490
|
* @param childs - Structure attributes data.
|
|
488
491
|
*/
|
|
489
492
|
_createClassDeserializer(parent, childs) {
|
|
493
|
+
const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
|
|
490
494
|
this._push0(this.serializers, `inline void from_json(const json& j, ${parent}& x) {`);
|
|
491
495
|
childs.forEach((i) => {
|
|
492
496
|
if (i.required) {
|
|
493
497
|
this._push1(this.serializers, `x.set_${i.snakeName}(j.at("${i.origName}").get<${i.typeName}>());`);
|
|
494
498
|
}
|
|
495
499
|
else {
|
|
496
|
-
this._push1(this.serializers, `x.set_${i.snakeName}(get_opt<${i.typeName}>(j, "${i.origName}"));`);
|
|
500
|
+
this._push1(this.serializers, `x.set_${i.snakeName}(${prefix}get_opt<${i.typeName}>(j, "${i.origName}"));`);
|
|
497
501
|
}
|
|
498
502
|
});
|
|
499
503
|
this._push0(this.serializers, "}\n");
|