mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
Remove Selfer codec impl from Verbs and add regression tests
This commit is contained in:
parent
9045892464
commit
59dfbe44f7
@ -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.
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user