zod-to-x 1.4.7-dev.7 → 1.4.8-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.
package/README.md CHANGED
@@ -568,8 +568,9 @@ In case of use of Google protobuf to improve communication performance, you can
568
568
  - **packageName**: Name of the protobuf file package.
569
569
  - **header**: Text to add as a comment at the beginning of the output.
570
570
  - **indent**: Number of spaces to use for indentation in the generated code. Defaults to 4 if not specified.
571
- - **includeComments**: Determines whether to include comments in the transpiled code. Defaults to true.
571
+ - **includeComments**: Determines whether to include comments in the transpiled code. Defaults to `true`.
572
572
  - **keepKeys**: Specifies whether property names should follow the Google Protobuf naming convention (false) or remain as originally defined (true). The default is `false`.
573
+ - **encodeDoubleAsInt**: Double values will be represented as integers in the proto file. Defaults to `false`.
573
574
 
574
575
  - Limitations:
575
576
  - ZodTuple is supported only for items of the same type.
@@ -9,5 +9,11 @@ export interface IZod2ProtoV3Opt extends IZodToXOpt {
9
9
  * language's naming conventions. If set to true, the original property names are preserved.
10
10
  */
11
11
  keepKeys?: boolean;
12
+ /**
13
+ * If true, double values will be represented as integers in the generated protobuf file.
14
+ * This could be useful when integer representation could be done with int32 or smaller because
15
+ * output will be more compact. Default is false.
16
+ */
17
+ encodeDoubleAsInt?: boolean;
12
18
  }
13
19
  export declare const defaultOpts: IZod2ProtoV3Opt;
@@ -5,5 +5,6 @@ exports.defaultOpts = {
5
5
  includeComments: true,
6
6
  indent: 4,
7
7
  keepKeys: false,
8
+ encodeDoubleAsInt: false,
8
9
  useImports: false, // Not required for protobuf files
9
10
  };
@@ -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,4 @@ 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>, opt?: Pick<IZod2AstOpt, "strict"> & Pick<IZod2ProtoV3Opt, "packageName" | "keepKeys" | "header" | "indent" | "includeComments">): string;
20
+ export declare function zod2ProtoV3(schema: ZodObject<any> | ZodDiscriminatedUnion<string, ZodObject<any>[]> | ZodUnion<[ZodObject<any>, ...ZodObject<any>[]]>, opt?: Pick<IZod2AstOpt, "strict"> & Pick<IZod2ProtoV3Opt, "packageName" | "keepKeys" | "header" | "indent" | "includeComments" | "encodeDoubleAsInt">): string;
@@ -34,7 +34,7 @@ class Zod2ProtoV3 extends core_1.Zod2X {
34
34
  this.getBooleanType = () => "bool";
35
35
  this.getStringType = () => "string";
36
36
  this.getNumberType = (isInt, range) => {
37
- if (!isInt) {
37
+ if (!isInt && this.opt.encodeDoubleAsInt !== true) {
38
38
  return "double";
39
39
  }
40
40
  if ((range === null || range === void 0 ? void 0 : range.min) >= number_limits_1.UINT32_RANGES[0]) {
@@ -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, `set_opt<${i.typeName}>(j, "${i.origName}", x.${i.snakeName});`);
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, `set_opt<${i.typeName}>(j, "${i.origName}", x.get_${i.snakeName}());`);
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");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-to-x",
3
- "version": "1.4.7-dev.7",
3
+ "version": "1.4.8-dev.2",
4
4
  "description": "Multi language types generation from Zod schemas.",
5
5
  "main": "dist/index.js",
6
6
  "files": [