goprotobuf: Fix proto.Merge handling of scalar bytes fields.
Previously the []byte would be treated as a repeated field,
and the new data would be appended. However, []byte is a
scalar field and the old data should be replaced instead.
Fixes #51.
LGTM=r
R=r
CC=golang-codereviews
https://codereview.appspot.com/91820043
diff --git a/proto/clone.go b/proto/clone.go
index dcaf5ab..8b94d9e 100644
--- a/proto/clone.go
+++ b/proto/clone.go
@@ -131,8 +131,11 @@
}
switch in.Type().Elem().Kind() {
case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int32, reflect.Int64,
- reflect.String, reflect.Uint32, reflect.Uint64, reflect.Uint8:
+ reflect.String, reflect.Uint32, reflect.Uint64:
out.Set(reflect.AppendSlice(out, in))
+ case reflect.Uint8:
+ // []byte is a scalar bytes field.
+ out.Set(in)
default:
for i := 0; i < n; i++ {
x := reflect.Indirect(reflect.New(in.Type().Elem()))
diff --git a/proto/clone_test.go b/proto/clone_test.go
index 71343ea..39aaaf7 100644
--- a/proto/clone_test.go
+++ b/proto/clone_test.go
@@ -167,6 +167,12 @@
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")},
+ },
}
func TestMerge(t *testing.T) {