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

Add support for 'optional' in proto3 files #317

Merged
merged 4 commits into from
Feb 15, 2022
Merged

Conversation

matthew-healy
Copy link
Contributor

Since protoc 3.15, proto3 files have had support for the optional keyword, which can be used with scalar fields to achieve nullability.

Our underlying code generator already supported this, we just needed to return the feature in order to indicate support in our CodeGenApp.

@matthew-healy matthew-healy added the enhancement New feature or request label Feb 3, 2022
Comment on lines +13 to +14
iTakeAnOptionInt(req.maybeInt)
iTakeAnOptionString(req.maybeString)
Copy link
Contributor

Choose a reason for hiding this comment

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

this would also pass if maybeInt were of type Some[Int] or None
I think it might be better to assert the fields' types directly (without even needing a service I guess?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added same-type constraints to these methods, so they can only be called by a value of the exact type we expect.

Copy link
Member

@ccmtaylor ccmtaylor Feb 3, 2022

Choose a reason for hiding this comment

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

my FP-scala-foo is a bit rusty. What is the difference to writing private def iTakeAnOptionInt(_: Option[Int]): Unit = ()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's what I originally had, but as Jordan pointed out, if the generated code was of type Some[Int] or None[Int] it would still pass. Using the same-type constraint ensures that the function can really only be called with an expression that has the exact type Option[T].

I'm not sure how much we really need to be concerned about this though, so I'm happy to revert it if you 'd prefer.

Copy link
Member

@ccmtaylor ccmtaylor left a comment

Choose a reason for hiding this comment

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

nice, thank you!

@@ -43,7 +43,8 @@ object ServerClientCodeGenerator extends CodeGenApp {
for {
file <- request.filesToGenerate
serviceFile <- generateServiceFiles(file, implicits)
} yield serviceFile
} yield serviceFile,
supportedFeatures = Set(CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL)
Copy link
Member

Choose a reason for hiding this comment

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

is this empty by default? If not, maybe

Suggested change
supportedFeatures = Set(CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL)
supportedFeatures += CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's empty, so I think it's probably fine to leave as-is?

Comment on lines +13 to +14
iTakeAnOptionInt(req.maybeInt)
iTakeAnOptionString(req.maybeString)
Copy link
Member

@ccmtaylor ccmtaylor Feb 3, 2022

Choose a reason for hiding this comment

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

my FP-scala-foo is a bit rusty. What is the difference to writing private def iTakeAnOptionInt(_: Option[Int]): Unit = ()?

@ccmtaylor ccmtaylor merged commit 87c2991 into master Feb 15, 2022
@ccmtaylor ccmtaylor deleted the support-proto3-optional branch February 15, 2022 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

3 participants