apimachinery: simplify deepcopy calls

This commit is contained in:
Dr. Stefan Schimanski 2017-08-15 14:09:51 +02:00
parent 3537f8fa34
commit e7424b64ce
4 changed files with 6 additions and 23 deletions

View File

@ -19,7 +19,6 @@ package roundtrip
import ( import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"fmt"
"math/rand" "math/rand"
"reflect" "reflect"
"strings" "strings"
@ -269,11 +268,7 @@ func roundTrip(t *testing.T, scheme *runtime.Scheme, codec runtime.Codec, object
original := object original := object
// deep copy the original object // deep copy the original object
copied, err := scheme.DeepCopy(object) object = object.DeepCopyObject()
if err != nil {
panic(fmt.Sprintf("unable to copy: %v", err))
}
object = copied.(runtime.Object)
name := reflect.TypeOf(object).Elem().Name() name := reflect.TypeOf(object).Elem().Name()
// catch deepcopy errors early // catch deepcopy errors early

View File

@ -243,12 +243,8 @@ func TestDeepCopyOfRuntimeObject(t *testing.T) {
} }
t.Logf("originalRole = %v\n", string(originalData)) t.Logf("originalRole = %v\n", string(originalData))
copyOfOriginal, err := s.DeepCopy(original) copyOfOriginal := original.DeepCopy()
if err != nil { copiedData, err := runtime.Encode(codec, copyOfOriginal)
t.Fatalf("unexpected error: %v", err)
}
copiedData, err := runtime.Encode(codec, copyOfOriginal.(runtime.Object))
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }

View File

@ -530,11 +530,7 @@ func (s *Scheme) convertToVersion(copy bool, in Object, target GroupVersioner) (
} }
if copy { if copy {
copied, err := s.Copy(in) in = in.DeepCopyObject()
if err != nil {
return nil, err
}
in = copied
} }
flags, meta := s.generateConvertMeta(in) flags, meta := s.generateConvertMeta(in)
@ -555,11 +551,7 @@ func (s *Scheme) generateConvertMeta(in interface{}) (conversion.FieldMatchingFl
// copyAndSetTargetKind performs a conditional copy before returning the object, or an error if copy was not successful. // copyAndSetTargetKind performs a conditional copy before returning the object, or an error if copy was not successful.
func copyAndSetTargetKind(copy bool, copier ObjectCopier, obj Object, kind schema.GroupVersionKind) (Object, error) { func copyAndSetTargetKind(copy bool, copier ObjectCopier, obj Object, kind schema.GroupVersionKind) (Object, error) {
if copy { if copy {
copied, err := copier.Copy(obj) obj = obj.DeepCopyObject()
if err != nil {
return nil, err
}
obj = copied
} }
setTargetKind(obj, kind) setTargetKind(obj, kind)
return obj, nil return obj, nil

View File

@ -711,7 +711,7 @@ func TestConvertToVersion(t *testing.T) {
}, },
} }
for i, test := range testCases { for i, test := range testCases {
original, _ := test.scheme.DeepCopy(test.in) original := test.in.DeepCopyObject()
out, err := test.scheme.ConvertToVersion(test.in, test.gv) out, err := test.scheme.ConvertToVersion(test.in, test.gv)
switch { switch {
case test.errFn != nil: case test.errFn != nil: