Switch API objects to not register per version codecs

Remove Codec from versionInterfaces in meta (RESTMapper is now agnostic
to codec and serialization). Register api/latest.Codecs as the codec
factory and use latest.Codecs.LegacyCodec(version) as an equvialent to
the previous codec.
This commit is contained in:
Clayton Coleman
2015-12-21 00:21:26 -05:00
parent 125ef6fbc8
commit c1d932e44a
54 changed files with 258 additions and 239 deletions

View File

@@ -61,6 +61,8 @@ func fuzzInternalObject(t *testing.T, forVersion unversioned.GroupVersion, item
}
func roundTrip(t *testing.T, codec runtime.Codec, item runtime.Object) {
t.Logf("codec: %#v", codec)
printer := spew.ConfigState{DisableMethods: true}
name := reflect.TypeOf(item).Elem().Name()
@@ -81,8 +83,7 @@ func roundTrip(t *testing.T, codec runtime.Codec, item runtime.Object) {
}
obj3 := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object)
err = runtime.DecodeInto(codec, data, obj3)
if err != nil {
if err := runtime.DecodeInto(codec, data, obj3); err != nil {
t.Errorf("2: %v: %v", name, err)
return
}
@@ -122,7 +123,7 @@ func TestSpecificKind(t *testing.T) {
// api.Scheme.Log(t)
// defer api.Scheme.Log(nil)
kind := "Pod"
kind := "List"
for i := 0; i < *fuzzIters; i++ {
doRoundTripTest(kind, t)
if t.Failed() {
@@ -180,7 +181,7 @@ func doRoundTripTest(kind string, t *testing.T) {
roundTripSame(t, item, nonRoundTrippableTypesByVersion[kind]...)
}
if !nonInternalRoundTrippableTypes.Has(kind) {
roundTrip(t, api.Codec, fuzzInternalObject(t, testapi.Default.InternalGroupVersion(), item, rand.Int63()))
roundTrip(t, testapi.Default.Codec(), fuzzInternalObject(t, testapi.Default.InternalGroupVersion(), item, rand.Int63()))
}
}
@@ -200,8 +201,8 @@ func TestEncode_Ptr(t *testing.T) {
},
}
obj := runtime.Object(pod)
data, err := testapi.Default.Codec().Encode(obj)
obj2, err2 := testapi.Default.Codec().Decode(data)
data, err := runtime.Encode(testapi.Default.Codec(), obj)
obj2, err2 := runtime.Decode(testapi.Default.Codec(), data)
if err != nil || err2 != nil {
t.Fatalf("Failure: '%v' '%v'", err, err2)
}
@@ -216,11 +217,11 @@ func TestEncode_Ptr(t *testing.T) {
func TestBadJSONRejection(t *testing.T) {
badJSONMissingKind := []byte(`{ }`)
if _, err := testapi.Default.Codec().Decode(badJSONMissingKind); err == nil {
if _, err := runtime.Decode(testapi.Default.Codec(), badJSONMissingKind); err == nil {
t.Errorf("Did not reject despite lack of kind field: %s", badJSONMissingKind)
}
badJSONUnknownType := []byte(`{"kind": "bar"}`)
if _, err1 := testapi.Default.Codec().Decode(badJSONUnknownType); err1 == nil {
if _, err1 := runtime.Decode(testapi.Default.Codec(), badJSONUnknownType); err1 == nil {
t.Errorf("Did not reject despite use of unknown type: %s", badJSONUnknownType)
}
/*badJSONKindMismatch := []byte(`{"kind": "Pod"}`)
@@ -240,14 +241,14 @@ func TestUnversionedTypes(t *testing.T) {
for _, obj := range testcases {
// Make sure the unversioned codec can encode
unversionedJSON, err := api.Codec.Encode(obj)
unversionedJSON, err := runtime.Encode(testapi.Default.Codec(), obj)
if err != nil {
t.Errorf("%v: unexpected error: %v", obj, err)
continue
}
// Make sure the versioned codec under test can decode
versionDecodedObject, err := testapi.Default.Codec().Decode(unversionedJSON)
versionDecodedObject, err := runtime.Decode(testapi.Default.Codec(), unversionedJSON)
if err != nil {
t.Errorf("%v: unexpected error: %v", obj, err)
continue
@@ -278,7 +279,7 @@ func BenchmarkEncodeCodec(b *testing.B) {
width := len(items)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := testapi.Default.Codec().Encode(&items[i%width]); err != nil {
if _, err := runtime.Encode(testapi.Default.Codec(), &items[i%width]); err != nil {
b.Fatal(err)
}
}
@@ -320,7 +321,7 @@ func BenchmarkDecodeCodec(b *testing.B) {
b.StopTimer()
}
func BenchmarkDecodeIntoCodec(b *testing.B) {
func BenchmarkDecodeIntoExternalCodec(b *testing.B) {
codec := testapi.Default.Codec()
items := benchmarkItems()
width := len(items)
@@ -343,6 +344,29 @@ func BenchmarkDecodeIntoCodec(b *testing.B) {
b.StopTimer()
}
func BenchmarkDecodeIntoInternalCodec(b *testing.B) {
codec := testapi.Default.Codec()
items := benchmarkItems()
width := len(items)
encoded := make([][]byte, width)
for i := range items {
data, err := runtime.Encode(codec, &items[i])
if err != nil {
b.Fatal(err)
}
encoded[i] = data
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
obj := api.Pod{}
if err := runtime.DecodeInto(codec, encoded[i%width], &obj); err != nil {
b.Fatal(err)
}
}
b.StopTimer()
}
// BenchmarkDecodeJSON provides a baseline for regular JSON decode performance
func BenchmarkDecodeIntoJSON(b *testing.B) {
codec := testapi.Default.Codec()