ts-proto icon indicating copy to clipboard operation
ts-proto copied to clipboard

numerical enum first value not being picked up

Open khi195 opened this issue 3 years ago • 6 comments

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 avatar Aug 04 '22 17:08 khi195

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); }

khi195 avatar Aug 04 '22 21:08 khi195

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!

stephenh avatar Aug 06 '22 15:08 stephenh

is there any update in here?

sefaphlvn avatar Sep 08 '23 21:09 sefaphlvn

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 avatar Sep 08 '23 21:09 sefaphlvn

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.

sefaphlvn avatar Sep 09 '23 10:09 sefaphlvn