-
Notifications
You must be signed in to change notification settings - Fork 349
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: support proto2 optional and default value fields #1007
Conversation
@stephenh Would like to get your thoughts on the Questions / Considerations section above. |
Hi @lukealvoeiro ; apologies for taking awhile to get back to you. I knew this PR would take more-than-a-few-minutes to give intelligent feedback on :-), so has been hard to find the time for it. Everything in your plan sounds good to me! In terms of passing around If you can continue on this path, I think it all sounds great and I'll merge everything when you're ready! Thank you! |
Just sanity checking my understanding here:
You're saying that like, on If I'm following, that makes sense.
Right, cool!
Ah okay, so you're thinking this would basically be a "force proto3 mode" type flag, where Fwiw I could kind of go back/forth on this one; part of me thinks that, well, if you're using proto2, this is just how proto2 is supposed to work. And also proto3 ended up walking back their "fields are always set" and re-introduced optional fields. But I don't really feel strongly about it either. Like if this is just about making ts-proto's own use of But if you've personally got a lot of proto2 files that you'd prefer their optional fields to be "more like proto3", then that sounds good to me. |
Thanks for the feedback @stephenh! This PR should be ready to merge, unless you think I should modify parts of it. I updated the description above to better match the recent commits. Replying to some of your questions:
Yup, thats exactly it. I tried updating the ts-proto-descriptors like I mentioned but ran into more difficulties than anticipated. As a result, I've left the ts-proto-descriptors as is by generating them using the "force proto3 mode" described below.
I don't think I have strong feelings either way, just didn't want to introduce breaking changes to folks who are used to proto2 files behaving like proto3 ones. For the time being, since we need to force proto3 for the ts-proto-descriptors, I've kept the flag. |
@@ -382,7 +382,7 @@ describe("unknown-fields", () => { | |||
}, | |||
], | |||
}, | |||
"ccEnableArenas": false, | |||
"ccEnableArenas": true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changed because the file imports at proto2 message, hence the correct default value is being picked up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great @lukealvoeiro !
One really minor question about the readme updates, but otherwise looks great to merge, thank you!
This again looks great @lukealvoeiro ! Thank you! |
🎉 This PR is included in version 1.169.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Description
This PR aims to implement the features necessary to enable proto2 optional fields and support proto2 default values, requested by #973
The following changes are present in this PR:
main.ts
andtypes.ts
that implement optional fields, as well as default values.disableProto2Optionals
anddisableProto2DefaultValues
. By default these are set to false.proto3
andproto2
syntax types, intended to catch any regressionsencode
function of a messageforceLong=true
option, always check via a.equals()
instead of.isZero()
. This could be reverted, however, I find it to be cleaner; regardless of whether there is a default value provided or not this check will always work.Outstanding work
In a subsequent future PRs, we need to handle the following:
ts-proto-descriptors
to correctly render fields as optional. This involves running changing the parameters forprotos/build.sh
to using the build code fromts-protos
. I tried doing this as part of this PR, but the updated proto descriptors did not work well with ts-proto. Going to re-attempt this at a later date.todo(proto2)
label around the places that need updating.Past Discussion
Please ignore everything below, as it is out of date. I am keeping it within the PR description so below comments make sense to future readers.
Broken TypeScript in
src
As I mentioned above, I tried do this but didn't get very far down this path. I think in general it makes sense to update
ts-proto-descriptors
to use the optional fields (and default values), however, it was harder than expected to get the updated descriptors working withts-proto
File-specific context
I implemented this change, folks can continue adding to the file context whenever appropriate.
Testing performed