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

numerical enum first value not being picked up #643

Open
khi195 opened this issue Aug 4, 2022 · 5 comments
Open

numerical enum first value not being picked up #643

khi195 opened this issue Aug 4, 2022 · 5 comments

Comments

@khi195
Copy link

khi195 commented Aug 4, 2022

hi, any enum which is equal to 1 is not being picked up. For example

export enum AccountType {
CURRENT = 1,
SAVING = 2,
JOINT = 3,
UNRECOGNIZED = -1,
}

in the above example CURRENT = 1 is not being picked up when we make the network call? What am i doing wrong, how can I remedy this.

@khi195
Copy link
Author

khi195 commented Aug 4, 2022

I think the problem could be related to this encoding function,
if (message.accountType !== 1) {
writer.uint32(72).int32(message.accountType);
}

this is not allowing enums which are equal to 1, which obv incorrect. in the above example, when CURRENT=1 this is not being picked up.

should this not be the following

if (message.accountType !== -1) {
writer.uint32(72).int32(message.accountType);
}

@stephenh
Copy link
Owner

stephenh commented Aug 6, 2022

Ah yeah, you are probably running into this:

https://github.com/stephenh/ts-proto/blob/main/src/types.ts#L254

You could probably make notDefaultCheck return Code | undefined, instead of Code, and in the "there is no v.number === 0" case, return undefined, and have that tell generateEncode line 1107 to not put in that if check.

If you'd like to submit a PR, that'd be great. Thanks!

@sefaphlvn
Copy link
Contributor

is there any update in here?

@sefaphlvn
Copy link
Contributor

in my case I dont want to this condition ${place} !== ${enumType}.${enumValue}

Also I dont have experience for this.

https://github.com/stephenh/ts-proto/blob/75ba9073744e0c086540a8e3ae1edad629bdf85a/src/types.ts#L272C7-L275C84

if (options.stringEnums) { const enumType = messageToTypeName(ctx, field.typeName); const enumValue = getEnumMemberName(ctx, enumProto, zerothValue); return code${maybeNotUndefinedAnd} ${place} !== ${enumType}.${enumValue};

@sefaphlvn
Copy link
Contributor

I did some hardcoding in type.ts file, but it's a workaround. I'm writing this for you to understand my needs.

Replace the following line:

case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_BOOL:
    return (0, ts_poet_1.code) `${place} === true`;

with:

case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_BOOL:
    return (0, ts_poet_1.code) `${place} !== undefined`;

Replace the following block:

if (options.stringEnums) {
    const enumType = messageToTypeName(ctx, field.typeName);
    const enumValue = (0, enums_1.getMemberName)(ctx, enumProto, zerothValue);
    return (0, ts_poet_1.code) `${maybeNotUndefinedAnd} ${place} !== ${enumType}.${enumValue}`;
}

with:

if (options.stringEnums) {
    // const enumType = messageToTypeName(ctx, field.typeName);
    // const enumValue = (0, enums_1.getMemberName)(ctx, enumProto, zerothValue);
    return (0, ts_poet_1.code) `${maybeNotUndefinedAnd.replace(" && ", "")}`;
}

Based on my previous examples, is it possible to write an option for the toJSON function? I don't want it to exclude any default data from the returned JSON object.

in the first example, if the type of the field is bool and its content is false, it doesn't appear in the JSON.

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

3 participants