mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 07:47:56 +00:00
Merge pull request #55936 from shiywang/json_test_enhance
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Some test enhance, comments enhance and duplicate code reduce while I was learning the codebase, found some place could be enhance, not confident about the comments of `Decode` I changed isn't right. /assign @sttts PTAL, thanks ```release-note NONE ```
This commit is contained in:
commit
fce605fad1
@ -98,11 +98,29 @@ func init() {
|
|||||||
jsoniter.RegisterTypeDecoderFunc("interface {}", decodeNumberAsInt64IfPossible)
|
jsoniter.RegisterTypeDecoderFunc("interface {}", decodeNumberAsInt64IfPossible)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// gvkWithDefaults returns group kind and version defaulting from provided default
|
||||||
|
func gvkWithDefaults(actual, defaultGVK schema.GroupVersionKind) schema.GroupVersionKind {
|
||||||
|
if len(actual.Kind) == 0 {
|
||||||
|
actual.Kind = defaultGVK.Kind
|
||||||
|
}
|
||||||
|
if len(actual.Version) == 0 && len(actual.Group) == 0 {
|
||||||
|
actual.Group = defaultGVK.Group
|
||||||
|
actual.Version = defaultGVK.Version
|
||||||
|
}
|
||||||
|
if len(actual.Version) == 0 && actual.Group == defaultGVK.Group {
|
||||||
|
actual.Version = defaultGVK.Version
|
||||||
|
}
|
||||||
|
return actual
|
||||||
|
}
|
||||||
|
|
||||||
// Decode attempts to convert the provided data into YAML or JSON, extract the stored schema kind, apply the provided default gvk, and then
|
// Decode attempts to convert the provided data into YAML or JSON, extract the stored schema kind, apply the provided default gvk, and then
|
||||||
// load that data into an object matching the desired schema kind or the provided into. If into is *runtime.Unknown, the raw data will be
|
// load that data into an object matching the desired schema kind or the provided into.
|
||||||
// extracted and no decoding will be performed. If into is not registered with the typer, then the object will be straight decoded using
|
// If into is *runtime.Unknown, the raw data will be extracted and no decoding will be performed.
|
||||||
// normal JSON/YAML unmarshalling. If into is provided and the original data is not fully qualified with kind/version/group, the type of
|
// If into is not registered with the typer, then the object will be straight decoded using normal JSON/YAML unmarshalling.
|
||||||
// the into will be used to alter the returned gvk. On success or most errors, the method will return the calculated schema kind.
|
// If into is provided and the original data is not fully qualified with kind/version/group, the type of the into will be used to alter the returned gvk.
|
||||||
|
// If into is nil or data's gvk different from into's gvk, it will generate a new Object with ObjectCreater.New(gvk)
|
||||||
|
// On success or most errors, the method will return the calculated schema kind.
|
||||||
|
// The gvk calculate priority will be originalData > default gvk > into
|
||||||
func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
|
func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
|
||||||
if versioned, ok := into.(*runtime.VersionedObjects); ok {
|
if versioned, ok := into.(*runtime.VersionedObjects); ok {
|
||||||
into = versioned.Last()
|
into = versioned.Last()
|
||||||
@ -129,17 +147,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
|
|||||||
}
|
}
|
||||||
|
|
||||||
if gvk != nil {
|
if gvk != nil {
|
||||||
// apply kind and version defaulting from provided default
|
*actual = gvkWithDefaults(*actual, *gvk)
|
||||||
if len(actual.Kind) == 0 {
|
|
||||||
actual.Kind = gvk.Kind
|
|
||||||
}
|
|
||||||
if len(actual.Version) == 0 && len(actual.Group) == 0 {
|
|
||||||
actual.Group = gvk.Group
|
|
||||||
actual.Version = gvk.Version
|
|
||||||
}
|
|
||||||
if len(actual.Version) == 0 && actual.Group == gvk.Group {
|
|
||||||
actual.Version = gvk.Version
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if unk, ok := into.(*runtime.Unknown); ok && unk != nil {
|
if unk, ok := into.(*runtime.Unknown); ok && unk != nil {
|
||||||
@ -161,17 +169,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
|
|||||||
case err != nil:
|
case err != nil:
|
||||||
return nil, actual, err
|
return nil, actual, err
|
||||||
default:
|
default:
|
||||||
typed := types[0]
|
*actual = gvkWithDefaults(*actual, types[0])
|
||||||
if len(actual.Kind) == 0 {
|
|
||||||
actual.Kind = typed.Kind
|
|
||||||
}
|
|
||||||
if len(actual.Version) == 0 && len(actual.Group) == 0 {
|
|
||||||
actual.Group = typed.Group
|
|
||||||
actual.Version = typed.Version
|
|
||||||
}
|
|
||||||
if len(actual.Version) == 0 && actual.Group == typed.Group {
|
|
||||||
actual.Version = typed.Version
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,6 +94,32 @@ func TestDecode(t *testing.T) {
|
|||||||
expectedObject: &testDecodable{},
|
expectedObject: &testDecodable{},
|
||||||
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
},
|
},
|
||||||
|
// group version, kind is defaulted
|
||||||
|
{
|
||||||
|
data: []byte(`{"apiVersion":"other1/blah1"}`),
|
||||||
|
defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
|
creater: &mockCreater{obj: &testDecodable{}},
|
||||||
|
expectedObject: &testDecodable{},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other1", Version: "blah1"},
|
||||||
|
},
|
||||||
|
// gvk all provided then not defaulted at all
|
||||||
|
{
|
||||||
|
data: []byte(`{"kind":"Test","apiVersion":"other/blah"}`),
|
||||||
|
defaultGVK: &schema.GroupVersionKind{Kind: "Test1", Group: "other1", Version: "blah1"},
|
||||||
|
creater: &mockCreater{obj: &testDecodable{}},
|
||||||
|
expectedObject: &testDecodable{},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||||
|
},
|
||||||
|
//gvk defaulting if kind not provided in data and defaultGVK use into's kind
|
||||||
|
{
|
||||||
|
data: []byte(`{"apiVersion":"b1/c1"}`),
|
||||||
|
into: &testDecodable{gvk: schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}},
|
||||||
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}},
|
||||||
|
defaultGVK: nil,
|
||||||
|
creater: &mockCreater{obj: &testDecodable{}},
|
||||||
|
expectedObject: &testDecodable{gvk: schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}},
|
||||||
|
expectedGVK: &schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"},
|
||||||
|
},
|
||||||
|
|
||||||
// accept runtime.Unknown as into and bypass creator
|
// accept runtime.Unknown as into and bypass creator
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user