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

Can proto.encode_text allow and ignore None values in the struct? #239

Open
ofk-goog opened this issue Jan 6, 2023 · 0 comments
Open

Can proto.encode_text allow and ignore None values in the struct? #239

ofk-goog opened this issue Jan 6, 2023 · 0 comments

Comments

@ofk-goog
Copy link

ofk-goog commented Jan 6, 2023

tl;dr: The result of proto.encode_text(struct(foo="hello", bar=None)) should be "{foo: "hello"} rather than an error. Otherwise I need to use hasattr a whole lot, which seems bad.

I'm ducktyping a proto in Starlark as part of a conversion to ITS that will likely be mandated all over the googlesphere, and I've hit this annoyance. My top-level message has an optional embedded message field, which has a required field. I need the values accessible in both Starlark and in C++, and wish to use proto.encode_text to avoid needing to maintain separate Starlark and textproto versions of each config file.

message SadLegacyProto {
  required string token = 1;
}
message MyConfigProto {
  optional SadLegacyProto legacy = 1;
  optional string other_data = 2;
}

The only way to represent the absence of the legacy field now is to omit it from my starlark struct, as in my_duck_proto = struct(other_data="blah"). The proto.encode_text of this looks like '{other_data: "blah"}' which is legal textproto. My annoyance with this is I have to check if hasattr(my_duck_proto, "legacy") instead of if my_duck_proto.legacy == None. Which goes against the python style guide, which we're supposed to be mostly using for Starlark.

Why other representations don't work: If I do proto.encode_text(struct(other_data="blah", legacy=struct())) then I get '{other_data: "blah" legacy {}} which can't be parsed because the required field is missing. Similarly proto.encode_text(struct(other_data="blah", legacy=struct(token=""))), I get {other_data: "blah" legacy {token: ""}} which is also an error.

Would this be okay?

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

1 participant