Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enum decoding #910

Merged
merged 9 commits into from
Sep 3, 2023
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
Loading