Browse Source

Merge into existing oneofs that are set to the same type.

David Symonds 10 years ago
parent
commit
61b9593c6e
1 changed files with 5 additions and 1 deletions
  1. 5 1
      proto/clone.go

+ 5 - 1
proto/clone.go

@@ -125,7 +125,11 @@ func mergeAny(out, in reflect.Value, viaPtr bool, prop *Properties) {
 		if in.IsNil() {
 		if in.IsNil() {
 			return
 			return
 		}
 		}
-		out.Set(reflect.New(in.Elem().Elem().Type())) // interface -> *T -> T -> new(T)
+		// Allocate destination if it is not set, or set to a different type.
+		// Otherwise we will merge as normal.
+		if out.IsNil() || out.Elem().Type() != in.Elem().Type() {
+			out.Set(reflect.New(in.Elem().Elem().Type())) // interface -> *T -> T -> new(T)
+		}
 		mergeAny(out.Elem(), in.Elem(), false, nil)
 		mergeAny(out.Elem(), in.Elem(), false, nil)
 	case reflect.Map:
 	case reflect.Map:
 		if in.Len() == 0 {
 		if in.Len() == 0 {