Remove Selfer codec impl from Verbs and add regression tests

This commit is contained in:
Dr. Stefan Schimanski 2017-01-10 17:38:13 +01:00
parent 9045892464
commit 59dfbe44f7
2 changed files with 26 additions and 27 deletions

View File

@ -29,8 +29,6 @@ import (
"fmt"
"strings"
"github.com/ugorji/go/codec"
"k8s.io/apimachinery/pkg/types"
)
@ -445,27 +443,6 @@ func (vs Verbs) String() string {
return fmt.Sprintf("%v", []string(vs))
}
// CodecEncodeSelf is part of the codec.Selfer interface.
func (vs *Verbs) CodecEncodeSelf(encoder *codec.Encoder) {
encoder.Encode(vs)
}
// CodecDecodeSelf is part of the codec.Selfer interface. It is overwritten here to make sure
// that an empty verbs list is not decoded as nil. On the other hand, an undefined verbs list
// will lead to nil because this decoding for Verbs is not invoked.
//
// TODO(sttts): this is due to a ugorji regression: https://github.com/ugorji/go/issues/119. Remove the
// workaround when the regression is fixed.
func (vs *Verbs) CodecDecodeSelf(decoder *codec.Decoder) {
m := []string{}
decoder.Decode(&m)
if len(m) == 0 {
*vs = []string{}
} else {
*vs = m
}
}
// APIResourceList is a list of APIResource, it is used to expose the name of the
// resources supported in a specific group and version, and if the resource
// is namespaced.

View File

@ -24,7 +24,7 @@ import (
"github.com/ugorji/go/codec"
)
func TestVerbsMarshalJSON(t *testing.T) {
func TestVerbsUgorjiMarshalJSON(t *testing.T) {
cases := []struct {
input APIResource
result string
@ -40,12 +40,12 @@ func TestVerbsMarshalJSON(t *testing.T) {
t.Errorf("[%d] Failed to marshal input: '%v': %v", i, c.input, err)
}
if string(result) != c.result {
t.Errorf("[%d] Failed to marshal input: '%v': expected %+v, got %q", i, c.input, c.result, string(result))
t.Errorf("[%d] Failed to marshal input: '%v': expected '%v', got '%v'", i, c.input, c.result, string(result))
}
}
}
func TestVerbsUnmarshalJSON(t *testing.T) {
func TestVerbsUgorjiUnmarshalJSON(t *testing.T) {
cases := []struct {
input string
result APIResource
@ -67,7 +67,29 @@ func TestVerbsUnmarshalJSON(t *testing.T) {
}
}
func TestVerbsUgorjiUnmarshalJSON(t *testing.T) {
// TestUgorjiMarshalJSONWithOmit tests that we don't have regressions regarding nil and empty slices with "omit"
func TestUgorjiMarshalJSONWithOmit(t *testing.T) {
cases := []struct {
input LabelSelector
result string
}{
{LabelSelector{}, `{}`},
{LabelSelector{MatchExpressions: []LabelSelectorRequirement{}}, `{}`},
{LabelSelector{MatchExpressions: []LabelSelectorRequirement{{}}}, `{"matchExpressions":[{"key":"","operator":""}]}`},
}
for i, c := range cases {
result, err := json.Marshal(&c.input)
if err != nil {
t.Errorf("[%d] Failed to marshal input: '%v': %v", i, c.input, err)
}
if string(result) != c.result {
t.Errorf("[%d] Failed to marshal input: '%v': expected '%v', got '%v'", i, c.input, c.result, string(result))
}
}
}
func TestVerbsUnmarshalJSON(t *testing.T) {
cases := []struct {
input string
result APIResource