It works fine when I run it.

> *go test -v .*
=== RUN   TestABCMessage
opt1:123 opt2:"baz"
--- PASS: TestABCMessage (0.00s)
PASS
ok  test 0.007s



However, your test file was incomplete. Maybe you have imported the wrong
stuff? Double-check that you are linking against the latest version of the
github.com/golang/protobuf packages.
I had to add imports, and I also changed the handling of the output since I
don't know what "spew" is:



package cmd

import (
  "fmt"
  "testing"

  "github.com/golang/protobuf/descriptor"
  "github.com/golang/protobuf/proto"
)

func TestABCMessage(t *testing.T) {
   var msg Bar

   _, md := descriptor.ForMessage(&msg)

   opts := md.Field[0].GetOptions()
   foo, err := proto.GetExtension(opts, E_FooOptions)
   if err != nil {
      t.Errorf("failed: %v", err)
  } else {
      fmt.Println(foo)
   }
}



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

On Wed, Oct 25, 2017 at 10:54 PM, <[email protected]> wrote:

> /tmp/TestABCMessage_in_version_abc_test_gogo -test.v -test.run
> ^TestABCMessage$
> (*errors.errorString)(0xc420214af0)(proto: not an extendable proto)
> (interface {}) <nil>
>
> On Wednesday, October 25, 2017 at 8:38:51 PM UTC+8, [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