mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 06:27:05 +00:00
Merge pull request #105702 from liggitt/json-strict-test
JSON decoder fixup
This commit is contained in:
commit
3f40906dd8
@ -382,7 +382,7 @@ func (unstructuredJSONScheme) Identifier() runtime.Identifier {
|
|||||||
|
|
||||||
func (s unstructuredJSONScheme) decode(data []byte) (runtime.Object, error) {
|
func (s unstructuredJSONScheme) decode(data []byte) (runtime.Object, error) {
|
||||||
type detector struct {
|
type detector struct {
|
||||||
Items gojson.RawMessage
|
Items gojson.RawMessage `json:"items"`
|
||||||
}
|
}
|
||||||
var det detector
|
var det detector
|
||||||
if err := json.Unmarshal(data, &det); err != nil {
|
if err := json.Unmarshal(data, &det); err != nil {
|
||||||
@ -425,7 +425,7 @@ func (unstructuredJSONScheme) decodeToUnstructured(data []byte, unstruct *Unstru
|
|||||||
|
|
||||||
func (s unstructuredJSONScheme) decodeToList(data []byte, list *UnstructuredList) error {
|
func (s unstructuredJSONScheme) decodeToList(data []byte, list *UnstructuredList) error {
|
||||||
type decodeList struct {
|
type decodeList struct {
|
||||||
Items []gojson.RawMessage
|
Items []gojson.RawMessage `json:"items"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var dList decodeList
|
var dList decodeList
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/runtime/serializer/json"
|
"k8s.io/apimachinery/pkg/runtime/serializer/json"
|
||||||
@ -30,11 +31,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type testDecodable struct {
|
type testDecodable struct {
|
||||||
|
metav1.TypeMeta `json:",inline"`
|
||||||
|
|
||||||
Other string
|
Other string
|
||||||
Value int `json:"value"`
|
Value int `json:"value"`
|
||||||
Spec DecodableSpec `json:"spec"`
|
Spec DecodableSpec `json:"spec"`
|
||||||
Interface interface{} `json:"interface"`
|
Interface interface{} `json:"interface"`
|
||||||
gvk schema.GroupVersionKind
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DecodableSpec has 15 fields. json-iterator treats struct with more than 10
|
// DecodableSpec has 15 fields. json-iterator treats struct with more than 10
|
||||||
@ -57,9 +59,6 @@ type DecodableSpec struct {
|
|||||||
O int `json:"o"`
|
O int `json:"o"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *testDecodable) GetObjectKind() schema.ObjectKind { return d }
|
|
||||||
func (d *testDecodable) SetGroupVersionKind(gvk schema.GroupVersionKind) { d.gvk = gvk }
|
|
||||||
func (d *testDecodable) GroupVersionKind() schema.GroupVersionKind { return d.gvk }
|
|
||||||
func (d *testDecodable) DeepCopyObject() runtime.Object {
|
func (d *testDecodable) DeepCopyObject() runtime.Object {
|
||||||
if d == nil {
|
if d == nil {
|
||||||
return nil
|
return nil
|
||||||
@ -74,7 +73,39 @@ func (d *testDecodable) DeepCopyInto(out *testDecodable) {
|
|||||||
out.Value = d.Value
|
out.Value = d.Value
|
||||||
out.Spec = d.Spec
|
out.Spec = d.Spec
|
||||||
out.Interface = d.Interface
|
out.Interface = d.Interface
|
||||||
out.gvk = d.gvk
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type testDecodeCoercion struct {
|
||||||
|
metav1.TypeMeta `json:",inline"`
|
||||||
|
|
||||||
|
Bool bool `json:"bool"`
|
||||||
|
|
||||||
|
Int int `json:"int"`
|
||||||
|
Int32 int `json:"int32"`
|
||||||
|
Int64 int `json:"int64"`
|
||||||
|
|
||||||
|
Float32 float32 `json:"float32"`
|
||||||
|
Float64 float64 `json:"float64"`
|
||||||
|
|
||||||
|
String string `json:"string"`
|
||||||
|
|
||||||
|
Struct testDecodable `json:"struct"`
|
||||||
|
|
||||||
|
Array []string `json:"array"`
|
||||||
|
Map map[string]string `json:"map"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *testDecodeCoercion) DeepCopyObject() runtime.Object {
|
||||||
|
if d == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
out := new(testDecodeCoercion)
|
||||||
|
d.DeepCopyInto(out)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
func (d *testDecodeCoercion) DeepCopyInto(out *testDecodeCoercion) {
|
||||||
|
*out = *d
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,7 +152,7 @@ func TestDecode(t *testing.T) {
|
|||||||
data: []byte(`{"apiVersion":"blah"}`),
|
data: []byte(`{"apiVersion":"blah"}`),
|
||||||
defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
creater: &mockCreater{obj: &testDecodable{}},
|
creater: &mockCreater{obj: &testDecodable{}},
|
||||||
expectedObject: &testDecodable{},
|
expectedObject: &testDecodable{TypeMeta: metav1.TypeMeta{APIVersion: "blah"}},
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "", Version: "blah"},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "", Version: "blah"},
|
||||||
},
|
},
|
||||||
// group without version is defaulted
|
// group without version is defaulted
|
||||||
@ -129,7 +160,7 @@ func TestDecode(t *testing.T) {
|
|||||||
data: []byte(`{"apiVersion":"other/"}`),
|
data: []byte(`{"apiVersion":"other/"}`),
|
||||||
defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
creater: &mockCreater{obj: &testDecodable{}},
|
creater: &mockCreater{obj: &testDecodable{}},
|
||||||
expectedObject: &testDecodable{},
|
expectedObject: &testDecodable{TypeMeta: metav1.TypeMeta{APIVersion: "other/"}},
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
},
|
},
|
||||||
// group version, kind is defaulted
|
// group version, kind is defaulted
|
||||||
@ -137,7 +168,7 @@ func TestDecode(t *testing.T) {
|
|||||||
data: []byte(`{"apiVersion":"other1/blah1"}`),
|
data: []byte(`{"apiVersion":"other1/blah1"}`),
|
||||||
defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
creater: &mockCreater{obj: &testDecodable{}},
|
creater: &mockCreater{obj: &testDecodable{}},
|
||||||
expectedObject: &testDecodable{},
|
expectedObject: &testDecodable{TypeMeta: metav1.TypeMeta{APIVersion: "other1/blah1"}},
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other1", Version: "blah1"},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other1", Version: "blah1"},
|
||||||
},
|
},
|
||||||
// gvk all provided then not defaulted at all
|
// gvk all provided then not defaulted at all
|
||||||
@ -145,17 +176,17 @@ func TestDecode(t *testing.T) {
|
|||||||
data: []byte(`{"kind":"Test","apiVersion":"other/blah"}`),
|
data: []byte(`{"kind":"Test","apiVersion":"other/blah"}`),
|
||||||
defaultGVK: &schema.GroupVersionKind{Kind: "Test1", Group: "other1", Version: "blah1"},
|
defaultGVK: &schema.GroupVersionKind{Kind: "Test1", Group: "other1", Version: "blah1"},
|
||||||
creater: &mockCreater{obj: &testDecodable{}},
|
creater: &mockCreater{obj: &testDecodable{}},
|
||||||
expectedObject: &testDecodable{},
|
expectedObject: &testDecodable{TypeMeta: metav1.TypeMeta{APIVersion: "other/blah", Kind: "Test"}},
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
},
|
},
|
||||||
//gvk defaulting if kind not provided in data and defaultGVK use into's kind
|
//gvk defaulting if kind not provided in data and defaultGVK use into's kind
|
||||||
{
|
{
|
||||||
data: []byte(`{"apiVersion":"b1/c1"}`),
|
data: []byte(`{"apiVersion":"b1/c1"}`),
|
||||||
into: &testDecodable{gvk: schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}},
|
into: &testDecodable{TypeMeta: metav1.TypeMeta{Kind: "a3", APIVersion: "b1/c1"}},
|
||||||
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}},
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}},
|
||||||
defaultGVK: nil,
|
defaultGVK: nil,
|
||||||
creater: &mockCreater{obj: &testDecodable{}},
|
creater: &mockCreater{obj: &testDecodable{}},
|
||||||
expectedObject: &testDecodable{gvk: schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}},
|
expectedObject: &testDecodable{TypeMeta: metav1.TypeMeta{Kind: "a3", APIVersion: "b1/c1"}},
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"},
|
expectedGVK: &schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"},
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -199,6 +230,7 @@ func TestDecode(t *testing.T) {
|
|||||||
typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},
|
typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
expectedObject: &testDecodable{
|
expectedObject: &testDecodable{
|
||||||
|
TypeMeta: metav1.TypeMeta{APIVersion: "other/blah", Kind: "Test"},
|
||||||
Other: "test",
|
Other: "test",
|
||||||
Value: 1,
|
Value: 1,
|
||||||
},
|
},
|
||||||
@ -243,6 +275,7 @@ func TestDecode(t *testing.T) {
|
|||||||
typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},
|
typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
expectedObject: &testDecodable{
|
expectedObject: &testDecodable{
|
||||||
|
TypeMeta: metav1.TypeMeta{APIVersion: "other/blah", Kind: "Test"},
|
||||||
Other: "test",
|
Other: "test",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -254,6 +287,7 @@ func TestDecode(t *testing.T) {
|
|||||||
typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},
|
typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
expectedObject: &testDecodable{
|
expectedObject: &testDecodable{
|
||||||
|
TypeMeta: metav1.TypeMeta{APIVersion: "other/blah", Kind: "Test"},
|
||||||
Spec: DecodableSpec{A: 1, H: 3},
|
Spec: DecodableSpec{A: 1, H: 3},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -275,7 +309,7 @@ func TestDecode(t *testing.T) {
|
|||||||
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
errFn: func(err error) bool {
|
errFn: func(err error) bool {
|
||||||
return strings.Contains(err.Error(), "found unknown field")
|
return strings.Contains(err.Error(), "found unknown field: unknown")
|
||||||
},
|
},
|
||||||
yaml: true,
|
yaml: true,
|
||||||
strict: true,
|
strict: true,
|
||||||
@ -287,7 +321,7 @@ func TestDecode(t *testing.T) {
|
|||||||
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
errFn: func(err error) bool {
|
errFn: func(err error) bool {
|
||||||
return strings.Contains(err.Error(), "already set in map")
|
return strings.Contains(err.Error(), `"value" already set in map`)
|
||||||
},
|
},
|
||||||
strict: true,
|
strict: true,
|
||||||
},
|
},
|
||||||
@ -299,33 +333,7 @@ func TestDecode(t *testing.T) {
|
|||||||
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
errFn: func(err error) bool {
|
errFn: func(err error) bool {
|
||||||
return strings.Contains(err.Error(), "already set in map")
|
return strings.Contains(err.Error(), `"value" already set in map`)
|
||||||
},
|
|
||||||
yaml: true,
|
|
||||||
strict: true,
|
|
||||||
},
|
|
||||||
// Strict JSON decode should fail for untagged fields.
|
|
||||||
{
|
|
||||||
data: []byte(`{"kind":"Test","apiVersion":"other/blah","value":1,"Other":"test"}`),
|
|
||||||
into: &testDecodable{},
|
|
||||||
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
|
||||||
errFn: func(err error) bool {
|
|
||||||
return strings.Contains(err.Error(), "found unknown field")
|
|
||||||
},
|
|
||||||
strict: true,
|
|
||||||
},
|
|
||||||
// Strict YAML decode should fail for untagged fields.
|
|
||||||
{
|
|
||||||
data: []byte("kind: Test\n" +
|
|
||||||
"apiVersion: other/blah\n" +
|
|
||||||
"value: 1\n" +
|
|
||||||
"Other: test\n"),
|
|
||||||
into: &testDecodable{},
|
|
||||||
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
|
||||||
errFn: func(err error) bool {
|
|
||||||
return strings.Contains(err.Error(), "found unknown field")
|
|
||||||
},
|
},
|
||||||
yaml: true,
|
yaml: true,
|
||||||
strict: true,
|
strict: true,
|
||||||
@ -337,6 +345,7 @@ func TestDecode(t *testing.T) {
|
|||||||
typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},
|
typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
expectedObject: &testDecodable{
|
expectedObject: &testDecodable{
|
||||||
|
TypeMeta: metav1.TypeMeta{APIVersion: "other/blah", Kind: "Test"},
|
||||||
Other: "test",
|
Other: "test",
|
||||||
Value: 1,
|
Value: 1,
|
||||||
},
|
},
|
||||||
@ -352,6 +361,7 @@ func TestDecode(t *testing.T) {
|
|||||||
typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},
|
typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
expectedObject: &testDecodable{
|
expectedObject: &testDecodable{
|
||||||
|
TypeMeta: metav1.TypeMeta{APIVersion: "other/blah", Kind: "Test"},
|
||||||
Other: "test",
|
Other: "test",
|
||||||
Value: 1,
|
Value: 1,
|
||||||
},
|
},
|
||||||
@ -381,6 +391,115 @@ func TestDecode(t *testing.T) {
|
|||||||
yaml: true,
|
yaml: true,
|
||||||
strict: true,
|
strict: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// coerce from null
|
||||||
|
{
|
||||||
|
data: []byte(`{"bool":null,"int":null,"int32":null,"int64":null,"float32":null,"float64":null,"string":null,"array":null,"map":null,"struct":null}`),
|
||||||
|
into: &testDecodeCoercion{},
|
||||||
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
|
expectedObject: &testDecodeCoercion{},
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: []byte(`{"bool":null,"int":null,"int32":null,"int64":null,"float32":null,"float64":null,"string":null,"array":null,"map":null,"struct":null}`),
|
||||||
|
into: &testDecodeCoercion{},
|
||||||
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
|
expectedObject: &testDecodeCoercion{},
|
||||||
|
yaml: true,
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
|
// coerce from string
|
||||||
|
{
|
||||||
|
data: []byte(`{"string":""}`),
|
||||||
|
into: &testDecodeCoercion{},
|
||||||
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
|
expectedObject: &testDecodeCoercion{},
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: []byte(`{"string":""}`),
|
||||||
|
into: &testDecodeCoercion{},
|
||||||
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
|
expectedObject: &testDecodeCoercion{},
|
||||||
|
yaml: true,
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
|
// coerce from array
|
||||||
|
{
|
||||||
|
data: []byte(`{"array":[]}`),
|
||||||
|
into: &testDecodeCoercion{},
|
||||||
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
|
expectedObject: &testDecodeCoercion{Array: []string{}},
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: []byte(`{"array":[]}`),
|
||||||
|
into: &testDecodeCoercion{},
|
||||||
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
|
expectedObject: &testDecodeCoercion{Array: []string{}},
|
||||||
|
yaml: true,
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
|
// coerce from map
|
||||||
|
{
|
||||||
|
data: []byte(`{"map":{},"struct":{}}`),
|
||||||
|
into: &testDecodeCoercion{},
|
||||||
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
|
expectedObject: &testDecodeCoercion{Map: map[string]string{}},
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: []byte(`{"map":{},"struct":{}}`),
|
||||||
|
into: &testDecodeCoercion{},
|
||||||
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
|
expectedObject: &testDecodeCoercion{Map: map[string]string{}},
|
||||||
|
yaml: true,
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
|
// coerce from int
|
||||||
|
{
|
||||||
|
data: []byte(`{"int":1,"int32":1,"int64":1,"float32":1,"float64":1}`),
|
||||||
|
into: &testDecodeCoercion{},
|
||||||
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
|
expectedObject: &testDecodeCoercion{Int: 1, Int32: 1, Int64: 1, Float32: 1, Float64: 1},
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: []byte(`{"int":1,"int32":1,"int64":1,"float32":1,"float64":1}`),
|
||||||
|
into: &testDecodeCoercion{},
|
||||||
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
|
expectedObject: &testDecodeCoercion{Int: 1, Int32: 1, Int64: 1, Float32: 1, Float64: 1},
|
||||||
|
yaml: true,
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
|
// coerce from float
|
||||||
|
{
|
||||||
|
data: []byte(`{"float32":1.0,"float64":1.0}`),
|
||||||
|
into: &testDecodeCoercion{},
|
||||||
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
|
expectedObject: &testDecodeCoercion{Float32: 1, Float64: 1},
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: []byte(`{"int":1.0,"int32":1.0,"int64":1.0,"float32":1.0,"float64":1.0}`), // floating point gets dropped in yaml -> json step
|
||||||
|
into: &testDecodeCoercion{},
|
||||||
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
|
expectedObject: &testDecodeCoercion{Int: 1, Int32: 1, Int64: 1, Float32: 1, Float64: 1},
|
||||||
|
yaml: true,
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range testCases {
|
for i, test := range testCases {
|
||||||
|
Loading…
Reference in New Issue
Block a user