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

Support optional map values in explicit presence #8419

Closed
powerman opened this issue Mar 22, 2021 · 1 comment
Closed

Support optional map values in explicit presence #8419

powerman opened this issue Mar 22, 2021 · 1 comment

Comments

@powerman
Copy link

What language does this apply to?
proto2 and proto3

Describe the problem you are trying to solve.
I need to batch update some configuration value for a list of items, this value may have values in range -10…10 (including zero) but also it may be disabled.

Describe the solution you'd like
map<string, optional int32> value = 1

Describe alternatives you've considered

  • map<string, MaybeInt32> value = 1 where MaybeInt32 is similar to google.protobuf.Int32Value (or contains extra bool set field for clarity) but a different message to work around protoc-gen-swagger: should well known types be nullable grpc-ecosystem/grpc-gateway#669 - it works, but it's ugly and less convenient to use than native "pointer to int32" type generated for optional in many languages
  • rewrite map as repeated message with key and (optional) value fields - the downside is it's became unclear from protocol that key must be unique
  • using value outside of allowed range to signal "disabled" - I believe downsides of such a "special" values are clear to anyone

Additional context
At a glance, adding optional support for map values feels natural, compatible with existing wire and proto formats and should make proto2/3 optional support more complete.

@deannagarcia
Copy link
Member

Sorry for the long delay.

Unfortunately this change is very unlikely. Under the hood, maps are implemented in language idiomatically which means that some languages wouldn't have proper data structures to store this data.

I think this is the best solution to your problem:

message MyMessage {
  map<string, ValueField> mymap = 1;
  
  message ValueField {
    optional int32 value = 1;
  }
}

This will still keep your keys unique, not require any extra knowledge of special values, and will provide you the conveniences of other optional fields.

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

No branches or pull requests

2 participants