The proto package has methods for accessing extensions. And the generated
.pb.go file contains exported symbols that describe those extensions. Put
them together, and you can get the custom options out -- something like so:

opts := md.Field[0].GetOptions()
foo, err := proto.GetExtension(opts, E_FooOptions)
if err != nil {
  fmt.Println(foo.GetOpt1()) // 123
  fmt.Println(foo.GetOpt2()) // baz
}

opts = md.Field[1].GetOptions()
foo, err = proto.GetExtension(opts, E_FooOptions)
if err != nil {
  fmt.Println(foo.GetOpt1()) // 456
  fmt.Println(foo.GetOpt2()) // xaa
}

----
*Josh Humphries*
[email protected]

On Wed, Oct 25, 2017 at 8:38 AM, <[email protected]> wrote:

> the example in https://developers.google.com/protocol-buffers/docs/
> proto#customoptions below
>
> message FooOptions {
>   optional int32 opt1 = 1;
>   optional string opt2 = 2;
> }
>
> extend google.protobuf.FieldOptions {
>   optional FooOptions foo_options = 1234;
> }
>
> // usage:
> message Bar {
>   optional int32 a = 1 [(foo_options).opt1 = 123, (foo_options).opt2 = "baz"];
>   // alternative aggregate syntax (uses TextFormat):
>   optional int32 b = 2 [(foo_options) = { opt1: 456 opt2: "xaa" }];
> }
>
> assume I have a message Bar here:
>
> var msg Bar
> _, md := ForMessage(&msg)
> md.Field[0].GetOptions().String()  only a string, How do i parse that ??
>
> How to I get the exact value of Bar.b.opt1 which is 123 (default value)
>
> What I want is like this
>
> md.Field[0].GetOptions().GetOpt1()   // 123
> md.Field[0].GetOptions().GetOpt2()    // baz
> md.Field[1].GetOptions().GetOpt1()   // 456
> md.Field[1].GetOptions().GetOpt2()   // xaa
>
> Can we achieve that ?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to