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

Enum JSON decoding/encoding methods #909

Closed
Frabat opened this issue Aug 23, 2023 · 2 comments
Closed

Enum JSON decoding/encoding methods #909

Frabat opened this issue Aug 23, 2023 · 2 comments

Comments

@Frabat
Copy link
Contributor

Frabat commented Aug 23, 2023

Hi,
when using the toJSON/fromJSON only encoding feature, I've noticed that the ts-proto outputs the serializing methods for both of them regardless which one we're using.

I wanted to track this, even though I'm already taking care and provide the PR

@Frabat
Copy link
Contributor Author

Frabat commented Aug 23, 2023

Example:

PROTO FILE:
`message Test {
enum TestType {
TEST_TYPE_UNSPECIFIED = 0;
TEST_TYPE_EVENT = 1;
TEST_TYPE_METRIC = 2;
TEST_TYPE_DERIVED = 3;
}

}`

exported TS file:

`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_TestTypeFromJSON(object: any): Test_TestType {
switch (object) {
case 0:
case "TEST_TYPE_UNSPECIFIED":
return Test_TestType.TEST_TYPE_UNSPECIFIED;
case 1:
case "TEST_TYPE_EVENT":
return Test_TestType.TEST_TYPE_EVENT;
case 2:
case "TEST_TYPE_METRIC":
return Test_TestType.TEST_TYPE_METRIC;
case 3:
case "TEST_TYPE_DERIVED":
return Test_TestType.TEST_TYPE_DERIVED;
case -1:
case "UNRECOGNIZED":
default:
return Test_TestType.UNRECOGNIZED;
}
}

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;
},
};`

Frabat pushed a commit to Frabat/ts-proto that referenced this issue Aug 23, 2023
stephenh pushed a commit that referenced this issue Sep 3, 2023
* 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 <francesco.battista@nbcuni.com>
@stephenh
Copy link
Owner

stephenh commented Sep 3, 2023

Fixed in #910 , thanks @Frabat !

@stephenh stephenh closed this as completed Sep 3, 2023
stephenh pushed a commit that referenced this issue Sep 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants