func eventToPbEvent(event *Event) (*proto.Event, error) { var e proto.Event if event.Host == "" { event.Host, _ = os.Hostname() } t := reflect.ValueOf(&e).Elem() s := reflect.ValueOf(event).Elem() typeOfEvent := s.Type() for i := 0; i < s.NumField(); i++ { f := s.Field(i) value := reflect.ValueOf(f.Interface()) if reflect.Zero(f.Type()) != value && f.Interface() != nil { name := typeOfEvent.Field(i).Name switch name { case "State", "Service", "Host", "Description": tmp := reflect.ValueOf(pb.String(value.String())) t.FieldByName(name).Set(tmp) case "Ttl": tmp := reflect.ValueOf(pb.Float32(float32(value.Float()))) t.FieldByName(name).Set(tmp) case "Time": tmp := reflect.ValueOf(pb.Int64(value.Int())) t.FieldByName(name).Set(tmp) case "Tags": tmp := reflect.ValueOf(value.Interface().([]string)) t.FieldByName(name).Set(tmp) case "Metric": switch reflect.TypeOf(f.Interface()).Kind() { case reflect.Int: tmp := reflect.ValueOf(pb.Int64(int64(value.Int()))) t.FieldByName("MetricSint64").Set(tmp) case reflect.Float32: tmp := reflect.ValueOf(pb.Float32(float32(value.Float()))) t.FieldByName("MetricF").Set(tmp) case reflect.Float64: tmp := reflect.ValueOf(pb.Float64(value.Float())) t.FieldByName("MetricD").Set(tmp) default: return nil, fmt.Errorf("Metric of invalid type (type %v)", reflect.TypeOf(f.Interface()).Kind()) } case "Attributes": var attrs []*proto.Attribute for k, v := range value.Interface().(map[string]string) { // Copy k,v so we can take // pointers to the new // temporaries k_, v_ := k, v attrs = append(attrs, &proto.Attribute{ Key: &k_, Value: &v_, }) } t.FieldByName(name).Set(reflect.ValueOf(attrs)) } } } return &e, nil }
GroupField: proto.Int32(6), }, RepBytes: [][]byte{[]byte("sham"), []byte("wow")}, }, }, // Check that a scalar bytes field replaces rather than appends. { src: &pb.OtherMessage{Value: []byte("foo")}, dst: &pb.OtherMessage{Value: []byte("bar")}, want: &pb.OtherMessage{Value: []byte("foo")}, }, { src: &pb.MessageWithMap{ NameMapping: map[int32]string{6: "Nigel"}, MsgMapping: map[int64]*pb.FloatingPoint{ 0x4001: &pb.FloatingPoint{F: proto.Float64(2.0)}, }, ByteMapping: map[bool][]byte{true: []byte("wowsa")}, }, dst: &pb.MessageWithMap{ NameMapping: map[int32]string{ 6: "Bruce", // should be overwritten 7: "Andrew", }, }, want: &pb.MessageWithMap{ NameMapping: map[int32]string{ 6: "Nigel", 7: "Andrew", }, MsgMapping: map[int64]*pb.FloatingPoint{