replace ConfigFast with ConfigCompatibleWithStandardLibrary

This commit is contained in:
Nikhita Raghunath 2017-12-13 16:05:34 +05:30
parent 5da7b11a31
commit 9c17635802
4 changed files with 6 additions and 32 deletions

View File

@ -60,22 +60,6 @@ func Funcs(codecs runtimeserializer.CodecFactory) []interface{} {
} }
if isValue || c.Intn(10) == 0 { if isValue || c.Intn(10) == 0 {
c.Fuzz(vobj.Field(i).Addr().Interface()) c.Fuzz(vobj.Field(i).Addr().Interface())
// JSON keys must not contain escape char with our JSON codec (jsoniter)
// TODO: remove this when/if we moved from jsoniter.ConfigFastest to ConfigCompatibleWithStandardLibrary
if field.Type.Kind() == reflect.Map {
keys := append([]reflect.Value(nil), vobj.Field(i).MapKeys()...)
for _, k := range keys {
stripped := toJSONString(k.String())
if stripped == k.String() {
continue
}
// set new key
vobj.Field(i).SetMapIndex(reflect.ValueOf(stripped), vobj.Field(i).MapIndex(k))
// remove old
vobj.Field(i).SetMapIndex(k, reflect.Value{})
}
}
} }
} }
} }
@ -125,13 +109,3 @@ func Funcs(codecs runtimeserializer.CodecFactory) []interface{} {
}, },
} }
} }
func toJSONString(s string) string {
return strings.Map(func(r rune) rune {
// replace chars which are not supported in keys by jsoniter.ConfigFastest
if r == '\\' || r == '"' {
return 'x'
}
return r
}, s)
}

View File

@ -47,7 +47,7 @@ func TestGroupVersionUnmarshalJSON(t *testing.T) {
t.Errorf("JSON codec 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 json-iterator codec // test the json-iterator codec
if err := jsoniter.ConfigFastest.Unmarshal(c.input, &result); err != nil { if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(c.input, &result); err != nil {
t.Errorf("json-iterator codec failed to unmarshal input '%v': %v", c.input, err) t.Errorf("json-iterator codec failed to unmarshal input '%v': %v", c.input, err)
} }
if !reflect.DeepEqual(result.GV, c.expect) { if !reflect.DeepEqual(result.GV, c.expect) {

View File

@ -58,7 +58,7 @@ func TestVerbsUgorjiUnmarshalJSON(t *testing.T) {
for i, c := range cases { for i, c := range cases {
var result APIResource var result APIResource
if err := jsoniter.ConfigFastest.Unmarshal([]byte(c.input), &result); err != nil { if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(c.input), &result); err != nil {
t.Errorf("[%d] Failed to unmarshal input '%v': %v", i, c.input, err) t.Errorf("[%d] Failed to unmarshal input '%v': %v", i, c.input, err)
} }
if !reflect.DeepEqual(result, c.result) { if !reflect.DeepEqual(result, c.result) {

View File

@ -154,7 +154,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
types, _, err := s.typer.ObjectKinds(into) types, _, err := s.typer.ObjectKinds(into)
switch { switch {
case runtime.IsNotRegisteredError(err), isUnstructured: case runtime.IsNotRegisteredError(err), isUnstructured:
if err := jsoniter.ConfigFastest.Unmarshal(data, into); err != nil { if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(data, into); err != nil {
return nil, actual, err return nil, actual, err
} }
return into, actual, nil return into, actual, nil
@ -188,7 +188,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
return nil, actual, err return nil, actual, err
} }
if err := jsoniter.ConfigFastest.Unmarshal(data, obj); err != nil { if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(data, obj); err != nil {
return nil, actual, err return nil, actual, err
} }
return obj, actual, nil return obj, actual, nil
@ -197,7 +197,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
// Encode serializes the provided object to the given writer. // Encode serializes the provided object to the given writer.
func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error { func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
if s.yaml { if s.yaml {
json, err := jsoniter.ConfigFastest.Marshal(obj) json, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(obj)
if err != nil { if err != nil {
return err return err
} }
@ -210,7 +210,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
} }
if s.pretty { if s.pretty {
data, err := jsoniter.ConfigFastest.MarshalIndent(obj, "", " ") data, err := jsoniter.ConfigCompatibleWithStandardLibrary.MarshalIndent(obj, "", " ")
if err != nil { if err != nil {
return err return err
} }