tokio-rs / prost

PROST! a Protocol Buffers implementation for the Rust Language
Apache License 2.0
3.86k stars 500 forks source link

fix getter/setter generated for optional enum fields should use option #1060

Open giangndm opened 4 months ago

giangndm commented 4 months ago

This PR fixed mismatch between getter/setter generated for optional enum fields and enum value i32 as described in #1027

giangndm commented 4 months ago

Why is the name_fallback getter nessesary? You could do .name().unwrap_or_default() to get the same result, right? I think that will result in more idiomatic code.

If we use unwrap_or_default, I don't think it can work with some custom default enum like this:

enum PrivacyLevel {
  PRIVACY_LEVEL_ONE = 1;
  PRIVACY_LEVEL_TWO = 2;
  PRIVACY_LEVEL_PRIVACY_LEVEL_THREE = 3;
  PRIVACY_LEVELPRIVACY_LEVEL_FOUR = 4;
}

message Test {
  optional PrivacyLevel privacy_level_1 = 1 [default = PRIVACY_LEVEL_ONE];
  optional PrivacyLevel privacy_level_3 = 2 [default = PRIVACY_LEVEL_PRIVACY_LEVEL_THREE];
  optional PrivacyLevel privacy_level_4 = 3 [default = PRIVACY_LEVELPRIVACY_LEVEL_FOUR];
}

Java implementation use has_ for checking and the field is set with default value in this case. How about think about has_ function?