Skip to content

Commit

Permalink
apimachinery: simplify deepcopy calls
Browse files Browse the repository at this point in the history
Kubernetes-commit: e7424b64ce4bf26b7424b858dc43e40c94541ee9
  • Loading branch information
sttts authored and k8s-publish-robot committed Aug 29, 2017
1 parent ce3cdfe commit 1992d40
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 23 deletions.
7 changes: 1 addition & 6 deletions pkg/api/testing/roundtrip/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package roundtrip
import (
"bytes"
"encoding/hex"
"fmt"
"math/rand"
"reflect"
"strings"
Expand Down Expand Up @@ -269,11 +268,7 @@ func roundTrip(t *testing.T, scheme *runtime.Scheme, codec runtime.Codec, object
original := object

// deep copy the original object
copied, err := scheme.DeepCopy(object)
if err != nil {
panic(fmt.Sprintf("unable to copy: %v", err))
}
object = copied.(runtime.Object)
object = object.DeepCopyObject()
name := reflect.TypeOf(object).Elem().Name()

// catch deepcopy errors early
Expand Down
8 changes: 2 additions & 6 deletions pkg/runtime/embedded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,8 @@ func TestDeepCopyOfRuntimeObject(t *testing.T) {
}
t.Logf("originalRole = %v\n", string(originalData))

copyOfOriginal, err := s.DeepCopy(original)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

copiedData, err := runtime.Encode(codec, copyOfOriginal.(runtime.Object))
copyOfOriginal := original.DeepCopy()
copiedData, err := runtime.Encode(codec, copyOfOriginal)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand Down
12 changes: 2 additions & 10 deletions pkg/runtime/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,7 @@ func (s *Scheme) convertToVersion(copy bool, in Object, target GroupVersioner) (
}

if copy {
copied, err := s.Copy(in)
if err != nil {
return nil, err
}
in = copied
in = in.DeepCopyObject()
}

flags, meta := s.generateConvertMeta(in)
Expand All @@ -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.
func copyAndSetTargetKind(copy bool, copier ObjectCopier, obj Object, kind schema.GroupVersionKind) (Object, error) {
if copy {
copied, err := copier.Copy(obj)
if err != nil {
return nil, err
}
obj = copied
obj = obj.DeepCopyObject()
}
setTargetKind(obj, kind)
return obj, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/scheme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ func TestConvertToVersion(t *testing.T) {
},
}
for i, test := range testCases {
original, _ := test.scheme.DeepCopy(test.in)
original := test.in.DeepCopyObject()
out, err := test.scheme.ConvertToVersion(test.in, test.gv)
switch {
case test.errFn != nil:
Expand Down

0 comments on commit 1992d40

Please sign in to comment.