Change ObjectKind signature to avoid allocations

We don't need to pass a pointer into SetGroupKindVersion() - a
struct works just as well.
This commit is contained in:
Clayton Coleman
2016-04-30 20:34:40 -04:00
parent bffbc112f0
commit a84e62d9e6
22 changed files with 109 additions and 143 deletions

View File

@@ -129,10 +129,10 @@ var TestObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 100).Funcs(
)
func (obj *MyWeirdCustomEmbeddedVersionKindField) GetObjectKind() unversioned.ObjectKind { return obj }
func (obj *MyWeirdCustomEmbeddedVersionKindField) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) {
func (obj *MyWeirdCustomEmbeddedVersionKindField) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
obj.APIVersion, obj.ObjectKind = gvk.ToAPIVersionAndKind()
}
func (obj *MyWeirdCustomEmbeddedVersionKindField) GroupVersionKind() *unversioned.GroupVersionKind {
func (obj *MyWeirdCustomEmbeddedVersionKindField) GroupVersionKind() unversioned.GroupVersionKind {
return unversioned.FromAPIVersionAndKind(obj.APIVersion, obj.ObjectKind)
}

View File

@@ -111,7 +111,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *unversioned.GroupVersionKi
if unk, ok := into.(*runtime.Unknown); ok && unk != nil {
unk.Raw = originalData
unk.ContentType = runtime.ContentTypeJSON
unk.GetObjectKind().SetGroupVersionKind(actual)
unk.GetObjectKind().SetGroupVersionKind(*actual)
return unk, actual, nil
}

View File

@@ -31,12 +31,12 @@ import (
type testDecodable struct {
Other string
Value int `json:"value"`
gvk *unversioned.GroupVersionKind
gvk unversioned.GroupVersionKind
}
func (d *testDecodable) GetObjectKind() unversioned.ObjectKind { return d }
func (d *testDecodable) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) { d.gvk = gvk }
func (d *testDecodable) GroupVersionKind() *unversioned.GroupVersionKind { return d.gvk }
func (d *testDecodable) GetObjectKind() unversioned.ObjectKind { return d }
func (d *testDecodable) SetGroupVersionKind(gvk unversioned.GroupVersionKind) { d.gvk = gvk }
func (d *testDecodable) GroupVersionKind() unversioned.GroupVersionKind { return d.gvk }
func TestDecode(t *testing.T) {
testCases := []struct {
@@ -76,34 +76,28 @@ func TestDecode(t *testing.T) {
errFn: func(err error) bool { return err.Error() == "fake error" },
},
{
data: []byte("{}"),
defaultGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
creater: &mockCreater{obj: &testDecodable{}},
expectedObject: &testDecodable{
gvk: nil, // json serializer does NOT set GVK
},
expectedGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
data: []byte("{}"),
defaultGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
creater: &mockCreater{obj: &testDecodable{}},
expectedObject: &testDecodable{},
expectedGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
},
// version without group is not defaulted
{
data: []byte(`{"apiVersion":"blah"}`),
defaultGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
creater: &mockCreater{obj: &testDecodable{}},
expectedObject: &testDecodable{
gvk: nil, // json serializer does NOT set GVK
},
expectedGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "", Version: "blah"},
data: []byte(`{"apiVersion":"blah"}`),
defaultGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
creater: &mockCreater{obj: &testDecodable{}},
expectedObject: &testDecodable{},
expectedGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "", Version: "blah"},
},
// group without version is defaulted
{
data: []byte(`{"apiVersion":"other/"}`),
defaultGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
creater: &mockCreater{obj: &testDecodable{}},
expectedObject: &testDecodable{
gvk: nil, // json serializer does NOT set GVK
},
expectedGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
data: []byte(`{"apiVersion":"other/"}`),
defaultGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
creater: &mockCreater{obj: &testDecodable{}},
expectedObject: &testDecodable{},
expectedGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
},
// accept runtime.Unknown as into and bypass creator

View File

@@ -120,14 +120,14 @@ func (s *Serializer) Decode(originalData []byte, gvk *unversioned.GroupVersionKi
}
actual := unk.GroupVersionKind()
copyKindDefaults(actual, gvk)
copyKindDefaults(&actual, gvk)
if intoUnknown, ok := into.(*runtime.Unknown); ok && intoUnknown != nil {
*intoUnknown = unk
if len(intoUnknown.ContentType) == 0 {
intoUnknown.ContentType = s.contentType
}
return intoUnknown, actual, nil
return intoUnknown, &actual, nil
}
if into != nil {
@@ -136,16 +136,16 @@ func (s *Serializer) Decode(originalData []byte, gvk *unversioned.GroupVersionKi
case runtime.IsNotRegisteredError(err):
pb, ok := into.(proto.Message)
if !ok {
return nil, actual, errNotMarshalable{reflect.TypeOf(into)}
return nil, &actual, errNotMarshalable{reflect.TypeOf(into)}
}
if err := proto.Unmarshal(unk.Raw, pb); err != nil {
return nil, actual, err
return nil, &actual, err
}
return into, actual, nil
return into, &actual, nil
case err != nil:
return nil, actual, err
return nil, &actual, err
default:
copyKindDefaults(actual, typed)
copyKindDefaults(&actual, typed)
// if the result of defaulting did not set a version or group, ensure that at least group is set
// (copyKindDefaults will not assign Group if version is already set). This guarantees that the group
// of into is set if there is no better information from the caller or object.
@@ -156,25 +156,24 @@ func (s *Serializer) Decode(originalData []byte, gvk *unversioned.GroupVersionKi
}
if len(actual.Kind) == 0 {
return nil, actual, runtime.NewMissingKindErr(fmt.Sprintf("%#v", unk.TypeMeta))
return nil, &actual, runtime.NewMissingKindErr(fmt.Sprintf("%#v", unk.TypeMeta))
}
if len(actual.Version) == 0 {
return nil, actual, runtime.NewMissingVersionErr(fmt.Sprintf("%#v", unk.TypeMeta))
return nil, &actual, runtime.NewMissingVersionErr(fmt.Sprintf("%#v", unk.TypeMeta))
}
return unmarshalToObject(s.typer, s.creater, actual, into, unk.Raw)
return unmarshalToObject(s.typer, s.creater, &actual, into, unk.Raw)
}
// EncodeToStream serializes the provided object to the given writer. Overrides is ignored.
func (s *Serializer) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unversioned.GroupVersion) error {
var unk runtime.Unknown
if kind := obj.GetObjectKind().GroupVersionKind(); kind != nil {
unk = runtime.Unknown{
TypeMeta: runtime.TypeMeta{
Kind: kind.Kind,
APIVersion: kind.GroupVersion().String(),
},
}
kind := obj.GetObjectKind().GroupVersionKind()
unk = runtime.Unknown{
TypeMeta: runtime.TypeMeta{
Kind: kind.Kind,
APIVersion: kind.GroupVersion().String(),
},
}
prefixSize := uint64(len(s.prefix))
@@ -334,7 +333,7 @@ func (s *RawSerializer) Decode(originalData []byte, gvk *unversioned.GroupVersio
intoUnknown.Raw = data
intoUnknown.ContentEncoding = ""
intoUnknown.ContentType = s.contentType
intoUnknown.SetGroupVersionKind(actual)
intoUnknown.SetGroupVersionKind(*actual)
return intoUnknown, actual, nil
}

View File

@@ -34,12 +34,12 @@ import (
)
type testObject struct {
gvk *unversioned.GroupVersionKind
gvk unversioned.GroupVersionKind
}
func (d *testObject) GetObjectKind() unversioned.ObjectKind { return d }
func (d *testObject) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) { d.gvk = gvk }
func (d *testObject) GroupVersionKind() *unversioned.GroupVersionKind { return d.gvk }
func (d *testObject) GetObjectKind() unversioned.ObjectKind { return d }
func (d *testObject) SetGroupVersionKind(gvk unversioned.GroupVersionKind) { d.gvk = gvk }
func (d *testObject) GroupVersionKind() unversioned.GroupVersionKind { return d.gvk }
type testMarshalable struct {
testObject
@@ -106,7 +106,7 @@ func TestEncode(t *testing.T) {
0x22, 0x00, // content-encoding
}
obj2 := &testMarshalable{
testObject: testObject{gvk: &unversioned.GroupVersionKind{Kind: "test", Group: "other", Version: "version"}},
testObject: testObject{gvk: unversioned.GroupVersionKind{Kind: "test", Group: "other", Version: "version"}},
data: []byte{0x01, 0x02, 0x03},
}
wire2 := []byte{

View File

@@ -227,10 +227,12 @@ func (c *codec) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unv
}
if (c.encodeVersion == nil && len(overrides) == 0) || isUnversioned {
old := obj.GetObjectKind().GroupVersionKind()
obj.GetObjectKind().SetGroupVersionKind(gvk)
defer obj.GetObjectKind().SetGroupVersionKind(old)
return c.encoder.EncodeToStream(obj, w, overrides...)
objectKind := obj.GetObjectKind()
old := objectKind.GroupVersionKind()
objectKind.SetGroupVersionKind(*gvk)
err = c.encoder.EncodeToStream(obj, w, overrides...)
objectKind.SetGroupVersionKind(old)
return err
}
targetGV, ok := c.encodeVersion[gvk.Group]
@@ -270,13 +272,14 @@ func (c *codec) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unv
} else {
obj = out
}
} else {
old := obj.GetObjectKind().GroupVersionKind()
defer obj.GetObjectKind().SetGroupVersionKind(old)
obj.GetObjectKind().SetGroupVersionKind(&unversioned.GroupVersionKind{Group: targetGV.Group, Version: targetGV.Version, Kind: gvk.Kind})
}
return c.encoder.EncodeToStream(obj, w, overrides...)
objectKind := obj.GetObjectKind()
old := objectKind.GroupVersionKind()
objectKind.SetGroupVersionKind(unversioned.GroupVersionKind{Group: targetGV.Group, Version: targetGV.Version, Kind: gvk.Kind})
err = c.encoder.EncodeToStream(obj, w, overrides...)
objectKind.SetGroupVersionKind(old)
return err
}
// promoteOrPrependGroupVersion finds the group version in the provided group versions that has the same group as target.

View File

@@ -30,12 +30,12 @@ import (
type testDecodable struct {
Other string
Value int `json:"value"`
gvk *unversioned.GroupVersionKind
gvk unversioned.GroupVersionKind
}
func (d *testDecodable) GetObjectKind() unversioned.ObjectKind { return d }
func (d *testDecodable) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) { d.gvk = gvk }
func (d *testDecodable) GroupVersionKind() *unversioned.GroupVersionKind { return d.gvk }
func (d *testDecodable) GetObjectKind() unversioned.ObjectKind { return d }
func (d *testDecodable) SetGroupVersionKind(gvk unversioned.GroupVersionKind) { d.gvk = gvk }
func (d *testDecodable) GroupVersionKind() unversioned.GroupVersionKind { return d.gvk }
func TestDecode(t *testing.T) {
gvk1 := &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}