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: disabled the default check for the upstream encoder #657

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

khi195
Copy link

@khi195 khi195 commented Aug 19, 2022

this is a PR for the following issue
#643

for the upstream encoder, this PR has the ability to disable the check for default enums, because for proto2 we may not have a default enum, so will have to disable the default behaviour.

@khi195 khi195 changed the title disabled the default check for the upstream encoder feat: disabled the default check for the upstream encoder Aug 19, 2022
@@ -1125,11 +1125,29 @@ function generateEncode(ctx: Context, fullName: string, messageDesc: DescriptorP
}
`);
} else if (isScalar(field) || isEnum(field)) {
chunks.push(code`
if (${notDefaultCheck(ctx, field, messageDesc.options, `message.${fieldName}`)}) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @khi195 ; thanks for the PR! Instead of adding more options, I think we should try and solve this by being smarter at handling "there is no enum with value === 0".

I.e. this line is calling notDefaultCheck, which inside of that method has some code that looks like:

      const zerothValue = enumProto.value.find((v) => v.number === 0) || enumProto.value[0];
      if (options.stringEnums) {
        const enumType = messageToTypeName(ctx, field.typeName);
        return code`${maybeNotUndefinedAnd} ${place} !== ${enumType}.${zerothValue.name}`;
      } else {
        return code`${maybeNotUndefinedAnd} ${place} !== ${zerothValue.number}`;
      }

Note the || enumProto.value[0]. That is the line that is tripping up your code, b/c it means ts-proto is treating the "the enum value 1" as the default, which it just shouldn't do.

Can you try removing that || enumProto.value[0] and instead doing something like:

      const zerothValue = enumProto.value.find((v) => v.number === 0);
      if (!zerothValue) {
        return undefined;
      } else if (options.stringEnums) {
        const enumType = messageToTypeName(ctx, field.typeName);
        return code`${maybeNotUndefinedAnd} ${place} !== ${enumType}.${zerothValue.name}`;
      } else {
        return code`${maybeNotUndefinedAnd} ${place} !== ${zerothValue.number}`;
      }

And then here in the generateEncode method, you could do:

const maybeDefaultCheck = notDefaultCheck(ctx, field, messageDesc.options, `message.${fieldName}`);

And if maybeDefaultCheck is undefined, then leave the if statement out, just like you're currently doing by looking at the new disableDefaultCheck options.

I'm pretty sure that should work.

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

Successfully merging this pull request may close these issues.

2 participants