Skip to content

Commit

Permalink
feat: enum decoding (#910)
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

* feat: removed toJson/fromJSON enums when not used

* feat(#909): ran format

---------

Co-authored-by: Francesco Battista <[email protected]>
  • Loading branch information
Frabat and FrabatUni authored Sep 3, 2023
1 parent 6c37f87 commit 9e0a0b5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
Binary file added integration/fromJson-enums/from-json.bin
Binary file not shown.
9 changes: 9 additions & 0 deletions integration/fromJson-enums/from-json.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
message Test {
enum TestType {
TEST_TYPE_UNSPECIFIED = 0;
TEST_TYPE_EVENT = 1;
TEST_TYPE_METRIC = 2;
TEST_TYPE_DERIVED = 3;
}

}
37 changes: 37 additions & 0 deletions integration/fromJson-enums/from-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable */

export const protobufPackage = "";

export interface Test {
}

export enum Test_TestType {
TEST_TYPE_UNSPECIFIED = 0,
TEST_TYPE_EVENT = 1,
TEST_TYPE_METRIC = 2,
TEST_TYPE_DERIVED = 3,
UNRECOGNIZED = -1,
}

export function test_TestTypeToJSON(object: Test_TestType): string {
switch (object) {
case Test_TestType.TEST_TYPE_UNSPECIFIED:
return "TEST_TYPE_UNSPECIFIED";
case Test_TestType.TEST_TYPE_EVENT:
return "TEST_TYPE_EVENT";
case Test_TestType.TEST_TYPE_METRIC:
return "TEST_TYPE_METRIC";
case Test_TestType.TEST_TYPE_DERIVED:
return "TEST_TYPE_DERIVED";
case Test_TestType.UNRECOGNIZED:
default:
return "UNRECOGNIZED";
}
}

export const Test = {
toJSON(_: Test): unknown {
const obj: any = {};
return obj;
},
};
1 change: 1 addition & 0 deletions integration/fromJson-enums/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outputEncodeMethods=false,outputPartialMethods=false,outputJsonMethods=to-only,nestJs=false
8 changes: 6 additions & 2 deletions src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ export function generateEnum(
chunks.push(code`}`);
}

if (options.outputJsonMethods || (options.stringEnums && options.outputEncodeMethods)) {
if (
options.outputJsonMethods === true ||
options.outputJsonMethods === "from-only" ||
(options.stringEnums && options.outputEncodeMethods)
) {
chunks.push(code`\n`);
chunks.push(generateEnumFromJson(ctx, fullName, enumDesc));
}
if (options.outputJsonMethods) {
if (options.outputJsonMethods === true || options.outputJsonMethods === "to-only") {
chunks.push(code`\n`);
chunks.push(generateEnumToJson(ctx, fullName, enumDesc));
}
Expand Down

0 comments on commit 9e0a0b5

Please sign in to comment.