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) } }
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 }
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) } }
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. }
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)) } }
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") } }
//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") } }
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") } }
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) } } }
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") } }
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") } }
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()) } }
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) }
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) }
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") } }
func (g *Generator) GetMapValueField(field, valField *descriptor.FieldDescriptorProto) *descriptor.FieldDescriptorProto { if !gogoproto.IsCastValue(field) && gogoproto.IsNullable(field) { return valField } valField = proto.Clone(valField).(*descriptor.FieldDescriptorProto) if valField.Options == nil { valField.Options = &descriptor.FieldOptions{} } if valType := gogoproto.GetCastValue(field); len(valType) > 0 { if err := proto.SetExtension(valField.Options, gogoproto.E_Casttype, &valType); err != nil { g.Fail(err.Error()) } } nullable := gogoproto.IsNullable(field) if err := proto.SetExtension(valField.Options, gogoproto.E_Nullable, &nullable); err != nil { g.Fail(err.Error()) } return valField }
func TestExtensionDescsWithMissingExtensions(t *testing.T) { msg := &pb.MyMessage{Count: proto.Int32(0)} extdesc1 := pb.E_Ext_More if descs, err := proto.ExtensionDescs(msg); len(descs) != 0 || err != nil { t.Errorf("proto.ExtensionDescs: got %d descs, error %v; want 0, nil", len(descs), err) } ext1 := &pb.Ext{} if err := proto.SetExtension(msg, extdesc1, ext1); err != nil { t.Fatalf("Could not set ext1: %s", err) } extdesc2 := &proto.ExtensionDesc{ ExtendedType: (*pb.MyMessage)(nil), ExtensionType: (*bool)(nil), Field: 123456789, Name: "a.b", Tag: "varint,123456789,opt", } ext2 := proto.Bool(false) if err := proto.SetExtension(msg, extdesc2, ext2); err != nil { t.Fatalf("Could not set ext2: %s", err) } b, err := proto.Marshal(msg) if err != nil { t.Fatalf("Could not marshal msg: %v", err) } if err = proto.Unmarshal(b, msg); err != nil { t.Fatalf("Could not unmarshal into msg: %v", err) } descs, err := proto.ExtensionDescs(msg) if err != nil { t.Fatalf("proto.ExtensionDescs: got error %v", err) } sortExtDescs(descs) wantDescs := []*proto.ExtensionDesc{extdesc1, {Field: extdesc2.Field}} if !reflect.DeepEqual(descs, wantDescs) { t.Errorf("proto.ExtensionDescs(msg) sorted extension ids: got %+v, want %+v", descs, wantDescs) } }
func (g *Generator) GetMapValueField(field, valField *descriptor.FieldDescriptorProto) *descriptor.FieldDescriptorProto { if gogoproto.IsCustomType(field) && gogoproto.IsCastValue(field) { g.Fail("cannot have a customtype and casttype: ", field.String()) } valField = proto.Clone(valField).(*descriptor.FieldDescriptorProto) if valField.Options == nil { valField.Options = &descriptor.FieldOptions{} } stdtime := gogoproto.IsStdTime(field) if stdtime { if err := proto.SetExtension(valField.Options, gogoproto.E_Stdtime, &stdtime); err != nil { g.Fail(err.Error()) } } stddur := gogoproto.IsStdDuration(field) if stddur { if err := proto.SetExtension(valField.Options, gogoproto.E_Stdduration, &stddur); err != nil { g.Fail(err.Error()) } } if valType := gogoproto.GetCastValue(field); len(valType) > 0 { if err := proto.SetExtension(valField.Options, gogoproto.E_Casttype, &valType); err != nil { g.Fail(err.Error()) } } if valType := gogoproto.GetCustomType(field); len(valType) > 0 { if err := proto.SetExtension(valField.Options, gogoproto.E_Customtype, &valType); err != nil { g.Fail(err.Error()) } } nullable := gogoproto.IsNullable(field) if err := proto.SetExtension(valField.Options, gogoproto.E_Nullable, &nullable); err != nil { g.Fail(err.Error()) } return valField }
func SetBoolFileOption(extension *proto.ExtensionDesc, value bool) func(file *descriptor.FileDescriptorProto) { return func(file *descriptor.FileDescriptorProto) { if FileHasBoolExtension(file, extension) { return } if file.Options == nil { file.Options = &descriptor.FileOptions{} } if err := proto.SetExtension(file.Options, extension, &value); err != nil { panic(err) } } }
func SetBoolEnumOption(extension *proto.ExtensionDesc, value bool) func(enum *descriptor.EnumDescriptorProto) { return func(enum *descriptor.EnumDescriptorProto) { if EnumHasBoolExtension(enum, extension) { return } if enum.Options == nil { enum.Options = &descriptor.EnumOptions{} } if err := proto.SetExtension(enum.Options, extension, &value); err != nil { panic(err) } } }
func SetBoolMessageOption(extension *proto.ExtensionDesc, value bool) func(msg *descriptor.DescriptorProto) { return func(msg *descriptor.DescriptorProto) { if MessageHasBoolExtension(msg, extension) { return } if msg.Options == nil { msg.Options = &descriptor.MessageOptions{} } if err := proto.SetExtension(msg.Options, extension, &value); err != nil { panic(err) } } }
func (g *Generator) GetMapKeyField(field, keyField *descriptor.FieldDescriptorProto) *descriptor.FieldDescriptorProto { if !gogoproto.IsCastKey(field) { return keyField } keyField = proto.Clone(keyField).(*descriptor.FieldDescriptorProto) if keyField.Options == nil { keyField.Options = &descriptor.FieldOptions{} } keyType := gogoproto.GetCastKey(field) if err := proto.SetExtension(keyField.Options, gogoproto.E_Casttype, &keyType); err != nil { g.Fail(err.Error()) } return keyField }
// fixFieldName changes field names like // 'id' -> 'ID' func fixFieldName(field *gogo.FieldDescriptorProto) { if gogoproto.IsCustomName(field) { return // Skip if a custom name is specified. } if field.Options == nil { field.Options = new(gogo.FieldOptions) } name := field.GetName() name = snaker.SnakeToCamel(name) name = lintName(name) // Use gogoproto.customname proto.SetExtension(field.Options, gogoproto.E_Customname, &name) }
func (s *Store) exec(typ internal.Command_Type, desc *proto.ExtensionDesc, value interface{}) error { // Create command. cmd := &internal.Command{Type: &typ} err := proto.SetExtension(cmd, desc, value) assert(err == nil, "proto.SetExtension: %s", err) // Marshal to a byte slice. b, err := proto.Marshal(cmd) assert(err == nil, "proto.Marshal: %s", err) // Apply the command if this is the leader. // Otherwise remotely execute the command against the current leader. if s.raft.State() == raft.Leader { return s.apply(b) } return s.remoteExec(b) }
func TestNoMergeExtensionNoMerge(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) } err = fieldpath.NoMerge(data, test.ThetestDescription(), "test", "MyExtendable") if err != nil { panic(err) } }
// setMetaNode is used when the raft group has only a single peer. It will // either create a metanode or update the information for the one metanode // that is there. It's used because hostnames can change func (s *store) setMetaNode(addr, raftAddr string) error { val := &internal.SetMetaNodeCommand{ HTTPAddr: proto.String(addr), TCPAddr: proto.String(raftAddr), Rand: proto.Uint64(uint64(rand.Int63())), } t := internal.Command_SetMetaNodeCommand cmd := &internal.Command{Type: &t} if err := proto.SetExtension(cmd, internal.E_SetMetaNodeCommand_Command, val); err != nil { panic(err) } b, err := proto.Marshal(cmd) if err != nil { return err } return s.apply(b) }
func (c *Client) exec(url string, typ internal.Command_Type, desc *proto.ExtensionDesc, value interface{}) (index uint64, err error) { // Create command. cmd := &internal.Command{Type: &typ} if err := proto.SetExtension(cmd, desc, value); err != nil { panic(err) } b, err := proto.Marshal(cmd) if err != nil { return 0, err } resp, err := http.Post(url, "application/octet-stream", bytes.NewBuffer(b)) if err != nil { return 0, err } defer resp.Body.Close() // read the response if resp.StatusCode == http.StatusTemporaryRedirect { return 0, errRedirect{host: resp.Header.Get("Location")} } else if resp.StatusCode != http.StatusOK { return 0, fmt.Errorf("meta service returned %s", resp.Status) } res := &internal.Response{} b, err = ioutil.ReadAll(resp.Body) if err != nil { return 0, err } if err := proto.Unmarshal(b, res); err != nil { return 0, err } es := res.GetError() if es != "" { return 0, fmt.Errorf(es) } return res.GetIndex(), nil }
// CustomNameID preprocess the field, and set the [(gogoproto.customname) = "..."] // if necessary, in order to avoid setting `gogoproto.customname` manually. // The automatically assigned name should conform to Golang convention. func CustomNameID(file *descriptor.FileDescriptorProto) { f := func(field *descriptor.FieldDescriptorProto) { // Skip if [(gogoproto.customname) = "..."] has already been set. if gogoproto.IsCustomName(field) { return } // Skip if embedded if gogoproto.IsEmbed(field) { return } if field.OneofIndex != nil { return } fieldName := generator.CamelCase(*field.Name) switch { case *field.Name == "id": // id -> ID fieldName = "ID" case strings.HasPrefix(*field.Name, "id_"): // id_some -> IDSome fieldName = "ID" + fieldName[2:] case strings.HasSuffix(*field.Name, "_id"): // some_id -> SomeID fieldName = fieldName[:len(fieldName)-2] + "ID" case strings.HasSuffix(*field.Name, "_ids"): // some_ids -> SomeIDs fieldName = fieldName[:len(fieldName)-3] + "IDs" default: return } if field.Options == nil { field.Options = &descriptor.FieldOptions{} } if err := proto.SetExtension(field.Options, gogoproto.E_Customname, &fieldName); err != nil { panic(err) } } // Iterate through all fields in file vanity.ForEachFieldExcludingExtensions(file.MessageType, f) }
func TestGetExtensionsWithMissingExtensions(t *testing.T) { msg := &pb.MyMessage{} ext1 := &pb.Ext{} if err := proto.SetExtension(msg, pb.E_Ext_More, ext1); err != nil { t.Fatalf("Could not set ext1: %s", err) } exts, err := proto.GetExtensions(msg, []*proto.ExtensionDesc{ pb.E_Ext_More, pb.E_Ext_Text, }) if err != nil { t.Fatalf("GetExtensions() failed: %s", err) } if exts[0] != ext1 { t.Errorf("ext1 not in returned extensions: %T %v", exts[0], exts[0]) } if exts[1] != nil { t.Errorf("ext2 in returned extensions: %T %v", exts[1], exts[1]) } }
func TestClearAllExtensions(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{} if proto.HasExtension(m, desc) { t.Errorf("proto.HasExtension(%s): got true, want false", proto.MarshalTextString(m)) } if err := proto.SetExtension(m, desc, proto.Bool(true)); err != nil { t.Errorf("proto.SetExtension(m, desc, true): got error %q, want nil", err) } if !proto.HasExtension(m, desc) { t.Errorf("proto.HasExtension(%s): got false, want true", proto.MarshalTextString(m)) } proto.ClearAllExtensions(m) if proto.HasExtension(m, desc) { t.Errorf("proto.HasExtension(%s): got true, want false", proto.MarshalTextString(m)) } }