本文整理匯總了Golang中github.com/gogo/protobuf/proto.SetExtension函數的典型用法代碼示例。如果您正苦於以下問題:Golang SetExtension函數的具體用法?Golang SetExtension怎麽用?Golang SetExtension使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了SetExtension函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: init
func init() {
if err := proto.SetExtension(realNumber, pb.E_Name, &realNumberName); err != nil {
panic(err)
}
if err := proto.SetExtension(realNumber, pb.E_Complex_RealExtension, complexNumber); err != nil {
panic(err)
}
}
示例2: newTestMessage
func newTestMessage() *pb.MyMessage {
msg := &pb.MyMessage{
Count: proto.Int32(42),
Name: proto.String("Dave"),
Quote: proto.String(`"I didn't want to go."`),
Pet: []string{"bunny", "kitty", "horsey"},
Inner: &pb.InnerMessage{
Host: proto.String("footrest.syd"),
Port: proto.Int32(7001),
Connected: proto.Bool(true),
},
Others: []*pb.OtherMessage{
{
Key: proto.Int64(0xdeadbeef),
Value: []byte{1, 65, 7, 12},
},
{
Weight: proto.Float32(6.022),
Inner: &pb.InnerMessage{
Host: proto.String("lesha.mtv"),
Port: proto.Int32(8002),
},
},
},
Bikeshed: pb.MyMessage_BLUE.Enum(),
Somegroup: &pb.MyMessage_SomeGroup{
GroupField: proto.Int32(8),
},
// One normally wouldn't do this.
// This is an undeclared tag 13, as a varint (wire type 0) with value 4.
XXX_unrecognized: []byte{13<<3 | 0, 4},
}
ext := &pb.Ext{
Data: proto.String("Big gobs for big rats"),
}
if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil {
panic(err)
}
greetings := []string{"adg", "easy", "cow"}
if err := proto.SetExtension(msg, pb.E_Greeting, greetings); err != nil {
panic(err)
}
// Add an unknown extension. We marshal a pb.Ext, and fake the ID.
b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")})
if err != nil {
panic(err)
}
b = append(proto.EncodeVarint(201<<3|proto.WireBytes), b...)
proto.SetRawExtension(msg, 201, b)
// Extensions can be plain fields, too, so let's test that.
b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19)
proto.SetRawExtension(msg, 202, b)
return msg
}
示例3: init
func init() {
f := float64(0.123)
if err := proto.SetExtension(AContainer, E_FieldA, &f); err != nil {
panic(err)
}
if err := proto.SetExtension(AContainer, E_FieldB, &Small{SmallField: proto.Int64(456)}); err != nil {
panic(err)
}
if err := proto.SetExtension(AContainer, E_FieldC, &Big{BigField: proto.Int64(789)}); err != nil {
panic(err)
}
}
示例4: TestNilExtension
func TestNilExtension(t *testing.T) {
msg := &pb.MyMessage{
Count: proto.Int32(1),
}
if err := proto.SetExtension(msg, pb.E_Ext_Text, proto.String("hello")); err != nil {
t.Fatal(err)
}
if err := proto.SetExtension(msg, pb.E_Ext_More, (*pb.Ext)(nil)); err == nil {
t.Error("expected SetExtension to fail due to a nil extension")
} else if want := "proto: SetExtension called with nil value of type *testdata.Ext"; err.Error() != want {
t.Errorf("expected error %v, got %v", want, err)
}
// Note: if the behavior of Marshal is ever changed to ignore nil extensions, update
// this test to verify that E_Ext_Text is properly propagated through marshal->unmarshal.
}
示例5: TestExtend
func TestExtend(t *testing.T) {
fp, err := fieldpath.NewFloat64Path("test", "MyExtendable", test.ThetestDescription(), "FieldA")
if err != nil {
panic(err)
}
m := &test.MyExtendable{}
err = proto.SetExtension(m, test.E_FieldA, proto.Float64(10.0))
if err != nil {
panic(err)
}
buf, err := proto.Marshal(m)
if err != nil {
panic(err)
}
var unmarshalled float64
f := FuncHandler{
Float64Func: func(v float64) {
t.Logf("unmarshalled %v", v)
unmarshalled = v
},
}
unmarshaler := fieldpath.NewFloat64Unmarshaler(fp, f)
err = unmarshaler.Unmarshal(buf)
if err != nil {
panic(err)
}
if unmarshalled != float64(10.0) {
panic(fmt.Errorf("wtf %v", unmarshalled))
}
}
示例6: TestNoMergeExtensionMerge
func TestNoMergeExtensionMerge(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
bigm := test.NewPopulatedMyExtendable(r, true)
m := test.NewPopulatedNinOptNative(r, true)
err := proto.SetExtension(bigm, test.E_FieldB, m)
if err != nil {
panic(err)
}
data, err := proto.Marshal(bigm)
if err != nil {
panic(err)
}
key := uint32(101)<<3 | uint32(2)
data2 := make([]byte, 10)
n := binary.PutUvarint(data2, uint64(key))
data2 = data2[:n]
data = append(data, data2...)
data4, err := proto.Marshal(test.NewPopulatedNinOptNative(r, true))
if err != nil {
panic(err)
}
data3 := make([]byte, 10)
n = binary.PutUvarint(data3, uint64(len(data4)))
data3 = data3[:n]
data = append(data, data3...)
data = append(data, data4...)
err = fieldpath.NoMerge(data, test.ThetestDescription(), "test", "MyExtendable")
if err == nil || !strings.Contains(err.Error(), "requires merging") {
t.Fatalf("should require merging")
}
}
示例7: TestGetExtensionStability
//See another version of this test in proto/extensions_test.go
func TestGetExtensionStability(t *testing.T) {
check := func(m *NoExtensionsMap) bool {
ext1, err := proto.GetExtension(m, E_FieldB1)
if err != nil {
t.Fatalf("GetExtension() failed: %s", err)
}
ext2, err := proto.GetExtension(m, E_FieldB1)
if err != nil {
t.Fatalf("GetExtension() failed: %s", err)
}
return ext1.(*NinOptNative).Equal(ext2)
}
msg := &NoExtensionsMap{Field1: proto.Int64(2)}
ext0 := &NinOptNative{Field1: proto.Float64(1)}
if err := proto.SetExtension(msg, E_FieldB1, ext0); err != nil {
t.Fatalf("Could not set ext1: %s", ext0)
}
if !check(msg) {
t.Errorf("GetExtension() not stable before marshaling")
}
bb, err := proto.Marshal(msg)
if err != nil {
t.Fatalf("Marshal() failed: %s", err)
}
msg1 := &NoExtensionsMap{}
err = proto.Unmarshal(bb, msg1)
if err != nil {
t.Fatalf("Unmarshal() failed: %s", err)
}
if !check(msg1) {
t.Errorf("GetExtension() not stable after unmarshaling")
}
}
示例8: TestRepeatedExtensionsFieldsIssue161
func TestRepeatedExtensionsFieldsIssue161(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
rep := 10
ints := make([]int64, rep)
for i := range ints {
ints[i] = r.Int63()
}
input := &MyExtendable{}
if err := proto.SetExtension(input, E_FieldD, ints); err != nil {
t.Fatal(err)
}
data, err := proto.Marshal(input)
if err != nil {
t.Fatal(err)
}
output := &MyExtendable{}
if err := proto.Unmarshal(data, output); err != nil {
t.Fatal(err)
}
if !input.Equal(output) {
t.Fatal("expected equal")
}
data2, err2 := proto.Marshal(output)
if err2 != nil {
t.Fatal(err2)
}
if len(data) != len(data2) {
t.Fatal("expected equal length buffers")
}
}
示例9: TestMarshalRace
func TestMarshalRace(t *testing.T) {
// unregistered extension
desc := &proto.ExtensionDesc{
ExtendedType: (*pb.MyMessage)(nil),
ExtensionType: (*bool)(nil),
Field: 101010100,
Name: "emptyextension",
Tag: "varint,0,opt",
}
m := &pb.MyMessage{Count: proto.Int32(4)}
if err := proto.SetExtension(m, desc, proto.Bool(true)); err != nil {
t.Errorf("proto.SetExtension(m, desc, true): got error %q, want nil", err)
}
errChan := make(chan error, 3)
for n := 3; n > 0; n-- {
go func() {
_, err := proto.Marshal(m)
errChan <- err
}()
}
for i := 0; i < 3; i++ {
err := <-errChan
if err != nil {
t.Fatal(err)
}
}
}
示例10: TestGetExtensionStability
func TestGetExtensionStability(t *testing.T) {
check := func(m *pb.MyMessage) bool {
ext1, err := proto.GetExtension(m, pb.E_Ext_More)
if err != nil {
t.Fatalf("GetExtension() failed: %s", err)
}
ext2, err := proto.GetExtension(m, pb.E_Ext_More)
if err != nil {
t.Fatalf("GetExtension() failed: %s", err)
}
return ext1 == ext2
}
msg := &pb.MyMessage{Count: proto.Int32(4)}
ext0 := &pb.Ext{}
if err := proto.SetExtension(msg, pb.E_Ext_More, ext0); err != nil {
t.Fatalf("Could not set ext1: %s", ext0)
}
if !check(msg) {
t.Errorf("GetExtension() not stable before marshaling")
}
bb, err := proto.Marshal(msg)
if err != nil {
t.Fatalf("Marshal() failed: %s", err)
}
msg1 := &pb.MyMessage{}
err = proto.Unmarshal(bb, msg1)
if err != nil {
t.Fatalf("Unmarshal() failed: %s", err)
}
if !check(msg1) {
t.Errorf("GetExtension() not stable after unmarshaling")
}
}
示例11: TestRepeatedExtensionsMsgsIssue161
func TestRepeatedExtensionsMsgsIssue161(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
rep := 10
nins := make([]*NinOptNative, rep)
for i := range nins {
nins[i] = NewPopulatedNinOptNative(r, true)
}
input := &MyExtendable{}
if err := proto.SetExtension(input, E_FieldE, nins); err != nil {
t.Fatal(err)
}
data, err := proto.Marshal(input)
if err != nil {
t.Fatal(err)
}
output := &MyExtendable{}
if err := proto.Unmarshal(data, output); err != nil {
t.Fatal(err)
}
if !input.Equal(output) {
t.Fatal("expected equal")
}
data2, err2 := proto.Marshal(output)
if err2 != nil {
t.Fatal(err2)
}
if len(data) != len(data2) {
t.Fatal("expected equal length buffers")
}
}
示例12: init
func init() {
ext := &pb.Ext{
Data: proto.String("extension"),
}
if err := proto.SetExtension(cloneTestMessage, pb.E_Ext_More, ext); err != nil {
panic("SetExtension: " + err.Error())
}
}
示例13: TestExtensionsNoExtensionsMapSetExtension
func TestExtensionsNoExtensionsMapSetExtension(t *testing.T) {
m := NewPopulatedNoExtensionsMap(extr, false)
err := proto.SetExtension(m, E_FieldA1, &fieldA)
if err != nil {
panic(err)
}
check(t, m, fieldA, E_FieldA1)
}
示例14: TestExtensionsMyExtendable
func TestExtensionsMyExtendable(t *testing.T) {
m := NewPopulatedMyExtendable(extr, false)
err := proto.SetExtension(m, E_FieldA, &fieldA)
if err != nil {
panic(err)
}
check(t, m, fieldA, E_FieldA)
proto.SetRawExtension(m, 100, fieldABytes)
check(t, m, fieldA, E_FieldA)
}
示例15: TestExtensionsRoundTrip
func TestExtensionsRoundTrip(t *testing.T) {
msg := &pb.MyMessage{}
ext1 := &pb.Ext{
Data: proto.String("hi"),
}
ext2 := &pb.Ext{
Data: proto.String("there"),
}
exists := proto.HasExtension(msg, pb.E_Ext_More)
if exists {
t.Error("Extension More present unexpectedly")
}
if err := proto.SetExtension(msg, pb.E_Ext_More, ext1); err != nil {
t.Error(err)
}
if err := proto.SetExtension(msg, pb.E_Ext_More, ext2); err != nil {
t.Error(err)
}
e, err := proto.GetExtension(msg, pb.E_Ext_More)
if err != nil {
t.Error(err)
}
x, ok := e.(*pb.Ext)
if !ok {
t.Errorf("e has type %T, expected testdata.Ext", e)
} else if *x.Data != "there" {
t.Errorf("SetExtension failed to overwrite, got %+v, not 'there'", x)
}
proto.ClearExtension(msg, pb.E_Ext_More)
if _, err = proto.GetExtension(msg, pb.E_Ext_More); err != proto.ErrMissingExtension {
t.Errorf("got %v, expected ErrMissingExtension", e)
}
if _, err := proto.GetExtension(msg, pb.E_X215); err == nil {
t.Error("expected bad extension error, got nil")
}
if err := proto.SetExtension(msg, pb.E_X215, 12); err == nil {
t.Error("expected extension err")
}
if err := proto.SetExtension(msg, pb.E_Ext_More, 12); err == nil {
t.Error("expected some sort of type mismatch error, got nil")
}
}