Skip to content

Commit

Permalink
feat(ts-proto-#859): added encode-only options to toJSON methods (#886)
Browse files Browse the repository at this point in the history
* feat(ts-proto-#859): added encode-only options to toJSON methods

* feat(ts-proto-#859): fixed error in implementation

* feat(ts-proto#859): inverted fromJSON and toJSON methods to avoid changing all tests

* feat(ts-proto#859): ran yarn format

* feat(ts-proto-#859): updated docs and renamed methods to toOnly and fromOnly

---------

Co-authored-by: Francesco Battista <francesco.battista@nbcuni.com>
  • Loading branch information
Frabat and FrabatUni committed Jul 20, 2023
1 parent 44e8e8a commit d0cf57d
Show file tree
Hide file tree
Showing 13 changed files with 458 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ Generated code will be placed in the Gradle build directory.

This is also useful if you want "only types".

- With `--ts_proto_opt=outputJsonMethods=to-only` and `--ts_proto_opt=outputJsonMethods=from-only` you will be able to export only one between the `Message.toJSON` and `Message.fromJSON` methods.

This is useful if you're using ts-proto just to `encode` or `decode` and not for both.

- With `--ts_proto_opt=outputPartialMethods=false`, the `Message.fromPartial` and `Message.create` methods for accepting partially-formed objects/object literals will not be output.

- With `--ts_proto_opt=stringEnums=true`, the generated enum types will be string-based instead of int-based.
Expand Down
Binary file added integration/output-fromJSON-only/decode.bin
Binary file not shown.
7 changes: 7 additions & 0 deletions integration/output-fromJSON-only/decode.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

import "google/protobuf/wrappers.proto";

message Encode {
string encode = 1;
}
17 changes: 17 additions & 0 deletions integration/output-fromJSON-only/decode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable */

export const protobufPackage = "";

export interface Encode {
encode: string;
}

export const Encode = {
fromJSON(object: any): Encode {
return { encode: isSet(object.encode) ? String(object.encode) : "" };
},
};

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
183 changes: 183 additions & 0 deletions integration/output-fromJSON-only/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/* eslint-disable */

export const protobufPackage = "google.protobuf";

/**
* Wrapper message for `double`.
*
* The JSON representation for `DoubleValue` is JSON number.
*/
export interface DoubleValue {
/** The double value. */
value: number;
}

/**
* Wrapper message for `float`.
*
* The JSON representation for `FloatValue` is JSON number.
*/
export interface FloatValue {
/** The float value. */
value: number;
}

/**
* Wrapper message for `int64`.
*
* The JSON representation for `Int64Value` is JSON string.
*/
export interface Int64Value {
/** The int64 value. */
value: number;
}

/**
* Wrapper message for `uint64`.
*
* The JSON representation for `UInt64Value` is JSON string.
*/
export interface UInt64Value {
/** The uint64 value. */
value: number;
}

/**
* Wrapper message for `int32`.
*
* The JSON representation for `Int32Value` is JSON number.
*/
export interface Int32Value {
/** The int32 value. */
value: number;
}

/**
* Wrapper message for `uint32`.
*
* The JSON representation for `UInt32Value` is JSON number.
*/
export interface UInt32Value {
/** The uint32 value. */
value: number;
}

/**
* Wrapper message for `bool`.
*
* The JSON representation for `BoolValue` is JSON `true` and `false`.
*/
export interface BoolValue {
/** The bool value. */
value: boolean;
}

/**
* Wrapper message for `string`.
*
* The JSON representation for `StringValue` is JSON string.
*/
export interface StringValue {
/** The string value. */
value: string;
}

/**
* Wrapper message for `bytes`.
*
* The JSON representation for `BytesValue` is JSON string.
*/
export interface BytesValue {
/** The bytes value. */
value: Uint8Array;
}

export const DoubleValue = {
fromJSON(object: any): DoubleValue {
return { value: isSet(object.value) ? Number(object.value) : 0 };
},
};

export const FloatValue = {
fromJSON(object: any): FloatValue {
return { value: isSet(object.value) ? Number(object.value) : 0 };
},
};

export const Int64Value = {
fromJSON(object: any): Int64Value {
return { value: isSet(object.value) ? Number(object.value) : 0 };
},
};

export const UInt64Value = {
fromJSON(object: any): UInt64Value {
return { value: isSet(object.value) ? Number(object.value) : 0 };
},
};

export const Int32Value = {
fromJSON(object: any): Int32Value {
return { value: isSet(object.value) ? Number(object.value) : 0 };
},
};

export const UInt32Value = {
fromJSON(object: any): UInt32Value {
return { value: isSet(object.value) ? Number(object.value) : 0 };
},
};

export const BoolValue = {
fromJSON(object: any): BoolValue {
return { value: isSet(object.value) ? Boolean(object.value) : false };
},
};

export const StringValue = {
fromJSON(object: any): StringValue {
return { value: isSet(object.value) ? String(object.value) : "" };
},
};

export const BytesValue = {
fromJSON(object: any): BytesValue {
return { value: isSet(object.value) ? bytesFromBase64(object.value) : new Uint8Array(0) };
},
};

declare const self: any | undefined;
declare const window: any | undefined;
declare const global: any | undefined;
const tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
if (typeof self !== "undefined") {
return self;
}
if (typeof window !== "undefined") {
return window;
}
if (typeof global !== "undefined") {
return global;
}
throw "Unable to locate global object";
})();

function bytesFromBase64(b64: string): Uint8Array {
if (tsProtoGlobalThis.Buffer) {
return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64"));
} else {
const bin = tsProtoGlobalThis.atob(b64);
const arr = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; ++i) {
arr[i] = bin.charCodeAt(i);
}
return arr;
}
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
1 change: 1 addition & 0 deletions integration/output-fromJSON-only/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outputEncodeMethods=false,outputPartialMethods=false,outputJsonMethods=from-only,nestJs=false
Binary file added integration/output-toJSON-only/encode.bin
Binary file not shown.
7 changes: 7 additions & 0 deletions integration/output-toJSON-only/encode.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

import "google/protobuf/wrappers.proto";

message Encode {
string encode = 1;
}
17 changes: 17 additions & 0 deletions integration/output-toJSON-only/encode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable */

export const protobufPackage = "";

export interface Encode {
encode: string;
}

export const Encode = {
toJSON(message: Encode): unknown {
const obj: any = {};
if (message.encode !== "") {
obj.encode = message.encode;
}
return obj;
},
};
Loading

0 comments on commit d0cf57d

Please sign in to comment.