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

custom options doesn't seem to work for protobuf map fields #1624

Open
yangzh opened this issue Jul 3, 2024 · 2 comments
Open

custom options doesn't seem to work for protobuf map fields #1624

yangzh opened this issue Jul 3, 2024 · 2 comments

Comments

@yangzh
Copy link

yangzh commented Jul 3, 2024

What version of protobuf and what language are you using?
The latest release of Go package of "google.golang.org/protobuf/proto" and related packages

What did you do?
I was trying to use custom options to augment behavior, and this doesn't seem to work for map fields.

For example, in my .proto file:
`
enum Hint {
HINT_UNKNOWN = 0;
HINT_RED = 1;
HINT_BLUE = 2;
}

message SomeMessage {
map<uint32, string> attrs = 1 [(hint)=HINT_RED];
}
`

I would expect to retrieve the value of the custom_settings with the following code:

hint := proto.GetExtension(fd.Options(), E_Hint).(Hint)
where fd is the protoreflect.FieldDescriptor for the field.
This works fine for regular non-map fields (either scalar or message field), but doesn't seem to work for maps.

and I tried to use fd or fd.MapKey() or fd.MapValue(), but still no luck there.

What did you expect to see?
Retrieve the expected custom options, in above case, HINT_RED.

What did you see instead?
HINT_UNKNOWN

Make sure you include information that can help us debug (full error message, exception listing, stack trace, logs).

Anything else we should know about your project / environment?

@neild
Copy link
Contributor

neild commented Jul 4, 2024

Works for me. Can you provide the complete .proto file and complete Go code?

func Test(t *testing.T) {
        m := &testoptspb.TestMessageWithCustomOptions{}
        md := m.ProtoReflect().Descriptor()
        fd := md.Fields().ByName("map_field")
        v := proto.GetExtension(fd.Options(), testoptspb.E_FieldOpt1).(uint64)
        if v != 12345 {
                t.Errorf("v = %v, want 12345", v)
        }
}

(Using https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/unittest_custom_options.proto)

@aktau
Copy link

aktau commented Jul 9, 2024

@yangzh were you able to try again with the approach that @neild posted? If so can you comment or close the issue?

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

3 participants