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

proto gen: support optionals for proto syntax 3 #105

Merged
merged 2 commits into from
Mar 23, 2022

Conversation

aadedamola
Copy link
Contributor

Based on the issue raised, this PR adds support for optional fields in proto 3. It also contains a test case for an optional field.

@aadedamola aadedamola marked this pull request as ready for review March 17, 2022 18:28
@aadedamola
Copy link
Contributor Author

@utrack @bullgare can you please take a look?

Copy link
Contributor

@bullgare bullgare left a comment

Choose a reason for hiding this comment

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

Looks good to me, @aadedamola.
A couple of minor comments from me.
WDYT, @utrack ?

@@ -203,17 +203,30 @@ func addValueTyped(f *descriptor.Field) string {
if valueFormatter != "" {
return fmt.Sprintf(valueFormatter, getter)
}

if f.GetProto3Optional() {
Copy link
Contributor

Choose a reason for hiding this comment

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

is it possible to have both value formatter and optional for the same field?
I guess not, but could you please check it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can have both -- currently, the valueformatter is used for encoding bytes. Bytes evaluate to the byte[] scalar type on go which is nullable. So the current implementation works.

The question is do we want to create pointers for bytes[] on code generation instead? And if yes, why?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree. I don't think we need a pointer to a slice

return fmt.Sprintf(`fmt.Sprintf("%s", %s)`, valueVerb, getter)
}

if !isRepeated {
if isRepeated {
Copy link
Contributor

Choose a reason for hiding this comment

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

It is not possible to have both optional and repeated for the same field, right?
It does not make much sense to me, but is it possible from the protobuf perspective?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To the best of my knowledge, repeated fields are inherently optional. If we try to put both optional and repeated on the same field, the parser will raise an error (did a quick experiment btw)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, an answer on Stack Overflow -- https://stackoverflow.com/a/25637833

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for checking it

@utrack
Copy link
Owner

utrack commented Mar 23, 2022

LGTM, thanks! :)

@utrack utrack merged commit 6429581 into utrack:master Mar 23, 2022
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.

None yet

3 participants