hi Josh,
Thank you for the reply, I tried your approach, but still doesn't works. it
throws error when I using those code to test
func TestABCMessage(t *testing.T) {
var msg Bar
_, md := ForMessage(&msg)
opts := md.Field[0].GetOptions()
foo, err := proto.GetExtension(opts, E_FooOptions)
if err != nil {
spew.Dump(err)
spew.Dump(foo) // 123
}
}
protoc --version
libprotoc 3.4.0
go version go1.8.3 linux/amd64
command I use protoc --go_out=. ./test.proto
proto file
syntax = "proto2";
package cmd;
import "google/protobuf/descriptor.proto";
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" }];
}
generated file
// Code generated by protoc-gen-go.
// source: version.proto
// DO NOT EDIT!
/*
Package cmd is a generated protocol buffer package.
It is generated from these files:
version.proto
It has these top-level messages:
FooOptions
Bar
*/
package cmd
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import google_protobuf "github.com/golang/protobuf/protoc-gen-go/descriptor"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type FooOptions struct {
Opt1 *int32 `protobuf:"varint,1,opt,name=opt1"
json:"opt1,omitempty"`
Opt2 *string `protobuf:"bytes,2,opt,name=opt2"
json:"opt2,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *FooOptions) Reset() { *m = FooOptions{} }
func (m *FooOptions) String() string { return
proto.CompactTextString(m) }
func (*FooOptions) ProtoMessage() {}
func (*FooOptions) Descriptor() ([]byte, []int) { return fileDescriptor0,
[]int{0} }
func (m *FooOptions) GetOpt1() int32 {
if m != nil && m.Opt1 != nil {
return *m.Opt1
}
return 0
}
func (m *FooOptions) GetOpt2() string {
if m != nil && m.Opt2 != nil {
return *m.Opt2
}
return ""
}
// usage:
type Bar struct {
A *int32 `protobuf:"varint,1,opt,name=a" json:"a,omitempty"`
// alternative aggregate syntax (uses TextFormat):
B *int32 `protobuf:"varint,2,opt,name=b" json:"b,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *Bar) Reset() { *m = Bar{} }
func (m *Bar) String() string { return proto.CompactTextString(m) }
func (*Bar) ProtoMessage() {}
func (*Bar) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
func (m *Bar) GetA() int32 {
if m != nil && m.A != nil {
return *m.A
}
return 0
}
func (m *Bar) GetB() int32 {
if m != nil && m.B != nil {
return *m.B
}
return 0
}
var E_FooOptions = &proto.ExtensionDesc{
ExtendedType: (*google_protobuf.FieldOptions)(nil),
ExtensionType: (*FooOptions)(nil),
Field: 1234,
Name: "cmd.foo_options",
Tag: "bytes,1234,opt,name=foo_options,json=fooOptions",
}
func init() {
proto.RegisterType((*FooOptions)(nil), "cmd.FooOptions")
proto.RegisterType((*Bar)(nil), "cmd.Bar")
proto.RegisterExtension(E_FooOptions)
}
func init() { proto.RegisterFile("version.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 218 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2,
0x2d, 0x4b, 0x2d, 0x2a,
0xce, 0xcc, 0xcf, 0xd3, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x4e,
0xce, 0x4d, 0x91, 0x52,
0x48, 0xcf, 0xcf, 0x4f, 0xcf, 0x49, 0xd5, 0x07, 0x0b, 0x25, 0x95, 0xa6,
0xe9, 0xa7, 0xa4, 0x16,
0x27, 0x17, 0x65, 0x16, 0x94, 0xe4, 0x17, 0x41, 0x94, 0x29, 0x99, 0x70,
0x71, 0xb9, 0xe5, 0xe7,
0xfb, 0x17, 0x94, 0x64, 0xe6, 0xe7, 0x15, 0x0b, 0x09, 0x71, 0xb1, 0xe4,
0x17, 0x94, 0x18, 0x4a,
0x30, 0x2a, 0x30, 0x6a, 0xb0, 0x06, 0x81, 0xd9, 0x50, 0x31, 0x23, 0x09,
0x26, 0x05, 0x46, 0x0d,
0x4e, 0xb0, 0x98, 0x91, 0x92, 0x2d, 0x17, 0xb3, 0x53, 0x62, 0x91, 0x90,
0x34, 0x17, 0x63, 0x22,
0x44, 0xad, 0x13, 0xef, 0x24, 0x5f, 0x26, 0x8e, 0xea, 0x49, 0xbe, 0xac,
0x42, 0xcc, 0x49, 0x89,
0x55, 0x41, 0x8c, 0x89, 0x42, 0x92, 0x5c, 0x8c, 0x49, 0x60, 0x4d, 0xac,
0x4e, 0xdc, 0x93, 0x7c,
0x39, 0x38, 0x4e, 0x30, 0x0b, 0x31, 0x57, 0x24, 0x26, 0x06, 0x31, 0x26,
0x59, 0x05, 0x70, 0x71,
0xa7, 0xe5, 0xe7, 0xc7, 0xe7, 0x43, 0x6d, 0x95, 0xd5, 0x83, 0x38, 0x53,
0x0f, 0xe6, 0x4c, 0x3d,
0xb7, 0xcc, 0xd4, 0x9c, 0x14, 0xa8, 0xa3, 0x24, 0x2e, 0x71, 0x2a, 0x30,
0x6a, 0x70, 0x1b, 0xf1,
0xeb, 0x25, 0xe7, 0xa6, 0xe8, 0x21, 0x1c, 0x1b, 0xc4, 0x95, 0x06, 0x67,
0x03, 0x02, 0x00, 0x00,
0xff, 0xff, 0xc1, 0xd1, 0xc2, 0x4e, 0xfd, 0x00, 0x00, 0x00,
}
/tmp/TestABCMessage_in_version_abc_test_gogo -test.v -test.run
^TestABCMessage$
(*errors.errorString)(0xc4201731c0)(proto: not an extendable proto)
(interface {}) <nil>
Process finished with exit code 0
*proto: not an extendable proto*
On Thursday, October 26, 2017 at 3:15:20 AM UTC+8, Josh Humphries wrote:
>
> 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] <javascript:>
>
> On Wed, Oct 25, 2017 at 8:38 AM, <[email protected] <javascript:>> 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] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> 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.