mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-14 22:33:34 +00:00
Make CBOR roundtrip cases pass through interface{} as well.
This commit is contained in:
parent
374e4b5686
commit
1503997b4f
@ -24,12 +24,14 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fxamacker/cbor/v2"
|
"github.com/fxamacker/cbor/v2"
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func nilPointerFor[T interface{}]() *T {
|
func nilPointerFor[T interface{}]() *T {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestRoundtrip roundtrips object serialization to interface{} and back via CBOR.
|
||||||
func TestRoundtrip(t *testing.T) {
|
func TestRoundtrip(t *testing.T) {
|
||||||
type modePair struct {
|
type modePair struct {
|
||||||
enc cbor.EncMode
|
enc cbor.EncMode
|
||||||
@ -101,10 +103,6 @@ func TestRoundtrip(t *testing.T) {
|
|||||||
name: "int64 zero",
|
name: "int64 zero",
|
||||||
obj: int64(math.MinInt64),
|
obj: int64(math.MinInt64),
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "uint64 max",
|
|
||||||
obj: uint64(math.MaxUint64),
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "uint64 zero",
|
name: "uint64 zero",
|
||||||
obj: uint64(0),
|
obj: uint64(0),
|
||||||
@ -285,18 +283,54 @@ func TestRoundtrip(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("enc=%s/dec=%s/%s", encModeName, decModeName, tc.name), func(t *testing.T) {
|
t.Run(fmt.Sprintf("enc=%s/dec=%s/%s", encModeName, decModeName, tc.name), func(t *testing.T) {
|
||||||
original := tc.obj
|
original := tc.obj
|
||||||
|
|
||||||
b, err := modePair.enc.Marshal(original)
|
cborFromOriginal, err := modePair.enc.Marshal(original)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error from Marshal: %v", err)
|
t.Fatalf("unexpected error from Marshal of original: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
final := reflect.New(reflect.TypeOf(original))
|
var iface interface{}
|
||||||
err = modePair.dec.Unmarshal(b, final.Interface())
|
if err := modePair.dec.Unmarshal(cborFromOriginal, &iface); err != nil {
|
||||||
if err != nil {
|
t.Fatalf("unexpected error from Unmarshal into %T: %v", &iface, err)
|
||||||
t.Fatalf("unexpected error from Unmarshal: %v", err)
|
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(original, final.Elem().Interface()) {
|
|
||||||
t.Errorf("roundtrip difference:\nwant: %#v\ngot: %#v", original, final)
|
cborFromIface, err := modePair.enc.Marshal(iface)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error from Marshal of iface: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// interface{} to interface{}
|
||||||
|
var iface2 interface{}
|
||||||
|
if err := modePair.dec.Unmarshal(cborFromIface, &iface2); err != nil {
|
||||||
|
t.Fatalf("unexpected error from Unmarshal into %T: %v", &iface2, err)
|
||||||
|
}
|
||||||
|
if diff := cmp.Diff(iface, iface2); diff != "" {
|
||||||
|
t.Errorf("unexpected difference on roundtrip from interface{} to interface{}:\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// original to original
|
||||||
|
final := reflect.New(reflect.TypeOf(original))
|
||||||
|
err = modePair.dec.Unmarshal(cborFromOriginal, final.Interface())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error from Unmarshal into %T: %v", final.Interface(), err)
|
||||||
|
}
|
||||||
|
if diff := cmp.Diff(original, final.Elem().Interface()); diff != "" {
|
||||||
|
t.Errorf("unexpected difference on roundtrip from original to original:\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// original to interface{} to original
|
||||||
|
finalViaIface := reflect.New(reflect.TypeOf(original))
|
||||||
|
err = modePair.dec.Unmarshal(cborFromIface, finalViaIface.Interface())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error from Unmarshal into %T: %v", finalViaIface.Interface(), err)
|
||||||
|
}
|
||||||
|
if diff := cmp.Diff(original, finalViaIface.Elem().Interface()); diff != "" {
|
||||||
|
t.Errorf("unexpected difference on roundtrip from original to interface{} to original:\n%s", diff)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user