mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 18:24:07 +00:00
add ugorji unmarshaller and address comments
This commit is contained in:
parent
9d52b0fc08
commit
6419924a5e
@ -30,7 +30,7 @@ type GroupAndVersion struct {
|
|||||||
|
|
||||||
func (gv *GroupAndVersion) String() string {
|
func (gv *GroupAndVersion) String() string {
|
||||||
// special case of "v1" for backward compatibility
|
// special case of "v1" for backward compatibility
|
||||||
if gv.Group == "" {
|
if gv.Group == "" && gv.Version == "v1" {
|
||||||
return gv.Version
|
return gv.Version
|
||||||
} else {
|
} else {
|
||||||
return gv.Group + "/" + gv.Version
|
return gv.Group + "/" + gv.Version
|
||||||
@ -41,11 +41,12 @@ func ParseGroupVersion(gv string) (GroupAndVersion, error) {
|
|||||||
s := strings.Split(gv, "/")
|
s := strings.Split(gv, "/")
|
||||||
// "v1" is the only special case. Otherwise GroupVersion is expected to contain
|
// "v1" is the only special case. Otherwise GroupVersion is expected to contain
|
||||||
// one "/" dividing the string into two parts.
|
// one "/" dividing the string into two parts.
|
||||||
if len(s) == 1 && gv == "v1" {
|
switch {
|
||||||
|
case len(s) == 1 && gv == "v1":
|
||||||
return GroupAndVersion{"", "v1"}, nil
|
return GroupAndVersion{"", "v1"}, nil
|
||||||
} else if len(s) == 2 {
|
case len(s) == 2:
|
||||||
return GroupAndVersion{s[0], s[1]}, nil
|
return GroupAndVersion{s[0], s[1]}, nil
|
||||||
} else {
|
default:
|
||||||
return GroupAndVersion{}, fmt.Errorf("Unexpected GroupVersion string: %v", gv)
|
return GroupAndVersion{}, fmt.Errorf("Unexpected GroupVersion string: %v", gv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,8 +60,7 @@ func (gv GroupAndVersion) MarshalJSON() ([]byte, error) {
|
|||||||
return json.Marshal(s)
|
return json.Marshal(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON implements the json.Unmarshaller interface.
|
func (gv *GroupAndVersion) unmarshal(value []byte) error {
|
||||||
func (gv *GroupAndVersion) UnmarshalJSON(value []byte) error {
|
|
||||||
var s string
|
var s string
|
||||||
if err := json.Unmarshal(value, &s); err != nil {
|
if err := json.Unmarshal(value, &s); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -69,7 +69,16 @@ func (gv *GroupAndVersion) UnmarshalJSON(value []byte) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
gv.Group = parsed.Group
|
*gv = parsed
|
||||||
gv.Version = parsed.Version
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements the json.Unmarshaller interface.
|
||||||
|
func (gv *GroupAndVersion) UnmarshalJSON(value []byte) error {
|
||||||
|
return gv.unmarshal(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalTEXT implements the Ugorji's encoding.TextUnmarshaler interface.
|
||||||
|
func (gv *GroupAndVersion) UnmarshalText(value []byte) error {
|
||||||
|
return gv.unmarshal(value)
|
||||||
|
}
|
||||||
|
@ -20,6 +20,8 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ugorji/go/codec"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GroupVersionHolder struct {
|
type GroupVersionHolder struct {
|
||||||
@ -37,11 +39,19 @@ func TestGroupVersionUnmarshalJSON(t *testing.T) {
|
|||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
var result GroupVersionHolder
|
var result GroupVersionHolder
|
||||||
|
// test golang lib's JSON codec
|
||||||
if err := json.Unmarshal([]byte(c.input), &result); err != nil {
|
if err := json.Unmarshal([]byte(c.input), &result); err != nil {
|
||||||
t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
|
t.Errorf("JSON codec failed to unmarshal input '%v': %v", c.input, err)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(result.GV, c.expect) {
|
if !reflect.DeepEqual(result.GV, c.expect) {
|
||||||
t.Errorf("Failed to unmarshal input '%s': expected %+v, got %+v", c.input, c.expect, result.GV)
|
t.Errorf("JSON codec failed to unmarshal input '%s': expected %+v, got %+v", c.input, c.expect, result.GV)
|
||||||
|
}
|
||||||
|
// test the Ugorji codec
|
||||||
|
if err := codec.NewDecoderBytes(c.input, new(codec.JsonHandle)).Decode(&result); err != nil {
|
||||||
|
t.Errorf("Ugorji codec failed to unmarshal input '%v': %v", c.input, err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(result.GV, c.expect) {
|
||||||
|
t.Errorf("Ugorji codec failed to unmarshal input '%s': expected %+v, got %+v", c.input, c.expect, result.GV)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user