Skip to content

What is the idiomatic way to get the corresponding struct field for a FieldDescriptorProto? #457

Closed
@junghoahnsc

Description

@junghoahnsc

Hello,

I'm trying to loop each field of a message and check proto field option. For example,

rval := reflect.Value(m)
...
_, descProt := descriptor.ForMessage(m)
for _, fd := range d.GetField() {
  // check the field option
  opts := fd.GetOptions()
  c, err := proto.GetExtension(opts, xxx)
  ...
  if condition {
     field := rval.FieldByName(fd.GetName() ???)
  }
}

Here, fd.GetName() returns the proto field name, so we need to convert to Go name.
Should I use https://github.com/golang/protobuf/blob/master/protoc-gen-go/generator/generator.go#L2736 ? Or is there any better way to do that?

Thanks,

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions