diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 07fc106..cb50479 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -9,7 +9,7 @@ jobs: build: strategy: matrix: - go-versions: [1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x] + go-versions: [1.17.x] runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 diff --git a/fields.go b/fields.go index f3c9079..ba3dd4d 100644 --- a/fields.go +++ b/fields.go @@ -46,11 +46,7 @@ func indirect(value reflect.Value, decodingNull bool) (json.Unmarshaler, encodin break } if value.IsNil() { - if value.CanSet() { - value.Set(reflect.New(value.Type().Elem())) - } else { - value = reflect.New(value.Type().Elem()) - } + value = reflect.New(value.Type().Elem()) } if value.Type().NumMethod() > 0 { if u, ok := value.Interface().(json.Unmarshaler); ok { diff --git a/go.mod b/go.mod index 818bbb5..40b5aed 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,10 @@ module sigs.k8s.io/yaml -go 1.12 +go 1.17 require ( - github.com/davecgh/go-spew v1.1.1 - gopkg.in/yaml.v2 v2.4.0 + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b + sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 ) + +replace gopkg.in/yaml.v3 => github.com/amurant/go-yaml v0.0.0-20211031110346-baaeb5da4f5b diff --git a/go.sum b/go.sum index b7b8cbb..630bbd0 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/amurant/go-yaml v0.0.0-20211031110346-baaeb5da4f5b h1:oFp8AMYmnVvsTyGQR4dEKki4JxRHpxUURat0tqhfl8s= +github.com/amurant/go-yaml v0.0.0-20211031110346-baaeb5da4f5b/go.mod h1:S0zpg6BiGH33bJz4jI0zkXVdP/pvBisCvtLNJ/AWKZw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 h1:fD1pz4yfdADVNfFmcP2aBEtudwUQ1AlLnRBALr33v3s= +sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6/go.mod h1:p4QtZmO4uMYipTQNzagwnNoseA6OxSUutVw05NhYDRs= diff --git a/yaml.go b/yaml.go index 04ed20f..b6c8a77 100644 --- a/yaml.go +++ b/yaml.go @@ -19,12 +19,24 @@ package yaml import ( "bytes" "encoding/json" + "errors" "fmt" - "io" "reflect" "strconv" - "gopkg.in/yaml.v2" + kubejson "sigs.k8s.io/json" + + "gopkg.in/yaml.v3" +) + +type yamlToTargetOption uint8 + +const ( + // no options enabled + none yamlToTargetOption = 0 + + // disallowUnknownFields returns strict errors if data contains unknown fields when decoding into typed structs + disallowUnknownFields yamlToTargetOption = 1 << iota ) // Marshal marshals obj into JSON using stdlib json.Marshal, and then converts JSON to YAML using JSONToYAML (see that method for more reference) @@ -37,79 +49,84 @@ func Marshal(obj interface{}) ([]byte, error) { return JSONToYAML(jsonBytes) } -// JSONOpt is a decoding option for decoding from JSON format. -type JSONOpt func(*json.Decoder) *json.Decoder - // Unmarshal first converts the given YAML to JSON, and then unmarshals the JSON into obj. Options for the // standard library json.Decoder can be optionally specified, e.g. to decode untyped numbers into json.Number instead of float64, or to disallow unknown fields (but for that purpose, see also UnmarshalStrict). obj must be a non-nil pointer. // // Important notes about the Unmarshal logic: // -// - Decoding is case-insensitive, unlike the rest of Kubernetes API machinery, as this is using the stdlib json library. This might be confusing to users. -// - This decodes any number (although it is an integer) into a float64 if the type of obj is unknown, e.g. *map[string]interface{}, *interface{}, or *[]interface{}. This means integers above +/- 2^53 will lose precision when round-tripping. Make a JSONOpt that calls d.UseNumber() to avoid this. -// - Duplicate fields, including in-case-sensitive matches, are ignored in an undefined order. Note that the YAML specification forbids duplicate fields, so this logic is more permissive than it needs to. See UnmarshalStrict for an alternative. -// - Unknown fields, i.e. serialized data that do not map to a field in obj, are ignored. Use d.DisallowUnknownFields() or UnmarshalStrict to override. -// - As per the YAML 1.1 specification, which yaml.v2 used underneath implements, literal 'yes' and 'no' strings without quotation marks will be converted to true/false implicitly. +// - Decoding is sensitive +// - Duplicate fields (only case-sensitive matches), result in a fatal error +// - Unknown fields, i.e. serialized data that do not map to a field in obj, are ignored. Use UnmarshalStrict to override. // - YAML non-string keys, e.g. ints, bools and floats, are converted to strings implicitly during the YAML to JSON conversion process. // - There are no compatibility guarantees for returned error values. -func Unmarshal(yamlBytes []byte, obj interface{}, opts ...JSONOpt) error { - return unmarshal(yamlBytes, obj, yaml.Unmarshal, opts...) +func Unmarshal(yamlBytes []byte, obj interface{}) error { + _, err := unmarshal(yamlBytes, obj, none) + return err } // UnmarshalStrict is similar to Unmarshal (please read its documentation for reference), with the following exceptions: // -// - Duplicate fields in an object yield an error. This is according to the YAML specification. -// - If obj, or any of its recursive children, is a struct, presence of fields in the serialized data unknown to the struct will yield an error. -func UnmarshalStrict(yamlBytes []byte, obj interface{}, opts ...JSONOpt) error { - return unmarshal(yamlBytes, obj, yaml.UnmarshalStrict, append(opts, DisallowUnknownFields)...) +// - If obj, or any of its recursive children, is a struct, presence of fields in the serialized data unknown to the struct will yield a strict error. +func UnmarshalStrict(yamlBytes []byte, obj interface{}) (strictErrors []error, err error) { + return unmarshal(yamlBytes, obj, disallowUnknownFields) } // unmarshal unmarshals the given YAML byte stream into the given interface, // optionally performing the unmarshalling strictly -func unmarshal(yamlBytes []byte, obj interface{}, unmarshalFn func([]byte, interface{}) error, opts ...JSONOpt) error { +func unmarshal(yamlBytes []byte, obj interface{}, options yamlToTargetOption) (strictErrors []error, err error) { jsonTarget := reflect.ValueOf(obj) + if jsonTarget.Kind() != reflect.Ptr || jsonTarget.IsNil() { + return nil, fmt.Errorf("provided object is not a valid pointer") + } - jsonBytes, err := yamlToJSONTarget(yamlBytes, &jsonTarget, unmarshalFn) + jsonBytes, strictErrors, err := yamlToJSONTarget(yamlBytes, &jsonTarget, options) if err != nil { - return fmt.Errorf("error converting YAML to JSON: %w", err) + return strictErrors, err } - err = jsonUnmarshal(bytes.NewReader(jsonBytes), obj, opts...) + // Decode jsonBytes into obj. + strictOptions := []kubejson.StrictOption{} + if options&disallowUnknownFields != 0 { + strictOptions = append(strictOptions, kubejson.DisallowUnknownFields) + } + strictErrors2, err := kubejson.UnmarshalStrict(jsonBytes, &obj, strictOptions...) + strictErrors = append(strictErrors, strictErrors2...) if err != nil { - return fmt.Errorf("error unmarshaling JSON: %w", err) + return strictErrors, fmt.Errorf("error unmarshaling JSON: %w", err) } - return nil + return strictErrors, nil } -// jsonUnmarshal unmarshals the JSON byte stream from the given reader into the -// object, optionally applying decoder options prior to decoding. We are not -// using json.Unmarshal directly as we want the chance to pass in non-default -// options. -func jsonUnmarshal(reader io.Reader, obj interface{}, opts ...JSONOpt) error { - d := json.NewDecoder(reader) - for _, opt := range opts { - d = opt(d) +func splitYAMLError(yamlError error) (strictErrors []error, err error) { + if e, ok := yamlError.(*yaml.TypeError); ok { + strictErrors = make([]error, 0, len(e.StrictErrors)) + for _, strictError := range e.StrictErrors { + strictErrors = append(strictErrors, errors.New(strictError)) + } + if len(e.Errors) == 0 { + return strictErrors, nil + } + + return strictErrors, &yaml.TypeError{Errors: e.Errors} } - return d.Decode(obj) + return nil, yamlError +} + +func yamlUnmarshal(yamlBytes []byte, v interface{}, options yamlToTargetOption) (strictErrors []error, err error) { + dec := yaml.NewDecoder(bytes.NewReader(yamlBytes)) + dec.KnownFields(options&disallowUnknownFields != 0) + return splitYAMLError(dec.Decode(v)) } // JSONToYAML converts JSON to YAML. Notable implementation details: // -// - Duplicate fields, are case-sensitively ignored in an undefined order. -// - The sequence indentation style is compact, which means that the "- " marker for a YAML sequence will be on the same indentation level as the sequence field name. -// - Unlike Unmarshal, all integers, up to 64 bits, are preserved during this round-trip. -func JSONToYAML(j []byte) ([]byte, error) { +// - Duplicate fields (only case-sensitive matches), result in a fatal error +func JSONToYAML(jsonBytes []byte) ([]byte, error) { // Convert the JSON to an object. var jsonObj interface{} - // We are using yaml.Unmarshal here (instead of json.Unmarshal) because the - // Go JSON library doesn't try to pick the right number type (int, float, - // etc.) when unmarshalling to interface{}, it just picks float64 - // universally. go-yaml does go through the effort of picking the right - // number type, so we can preserve number type throughout this process. - err := yaml.Unmarshal(j, &jsonObj) - if err != nil { + if err := kubejson.UnmarshalCaseSensitivePreserveInts(jsonBytes, &jsonObj); err != nil { return nil, fmt.Errorf("error converting JSON to YAML: %w", err) } @@ -133,29 +150,17 @@ func JSONToYAML(j []byte) ([]byte, error) { // not use the !!binary tag in your YAML. This will ensure the original base64 // encoded data makes it all the way through to the JSON. // * And more... read the YAML specification for more details. -// -// Notable about the implementation: -// -// - Duplicate fields are case-sensitively ignored in an undefined order. Note that the YAML specification forbids duplicate fields, so this logic is more permissive than it needs to. See YAMLToJSONStrict for an alternative. -// - As per the YAML 1.1 specification, which yaml.v2 used underneath implements, literal 'yes' and 'no' strings without quotation marks will be converted to true/false implicitly. -// - Unlike Unmarshal, all integers, up to 64 bits, are preserved during this round-trip. -// - There are no compatibility guarantees for returned error values. -func YAMLToJSON(y []byte) ([]byte, error) { - return yamlToJSONTarget(y, nil, yaml.Unmarshal) +func YAMLToJSON(yamlBytes []byte) ([]byte, error) { + json, _, err := yamlToJSONTarget(yamlBytes, nil, none) + return json, err } -// YAMLToJSONStrict is like YAMLToJSON but enables strict YAML decoding, -// returning an error on any duplicate field names. -func YAMLToJSONStrict(y []byte) ([]byte, error) { - return yamlToJSONTarget(y, nil, yaml.UnmarshalStrict) -} - -func yamlToJSONTarget(yamlBytes []byte, jsonTarget *reflect.Value, unmarshalFn func([]byte, interface{}) error) ([]byte, error) { +func yamlToJSONTarget(yamlBytes []byte, jsonTarget *reflect.Value, options yamlToTargetOption) (jsonBytes []byte, strictErrors []error, err error) { // Convert the YAML to an object. var yamlObj interface{} - err := unmarshalFn(yamlBytes, &yamlObj) + strictErrors, err = yamlUnmarshal(yamlBytes, &yamlObj, options) if err != nil { - return nil, fmt.Errorf("error converting YAML to JSON: %w", err) + return nil, strictErrors, fmt.Errorf("error converting YAML to JSON: %w", err) } // YAML objects are not completely compatible with JSON objects (e.g. you @@ -164,15 +169,15 @@ func yamlToJSONTarget(yamlBytes []byte, jsonTarget *reflect.Value, unmarshalFn f // incompatibilties happen along the way. jsonObj, err := convertToJSONableObject(yamlObj, jsonTarget) if err != nil { - return nil, fmt.Errorf("error converting YAML to JSON: %w", err) + return nil, strictErrors, fmt.Errorf("error converting YAML to JSON: %w", err) } // Convert this object to JSON and return the data. - jsonBytes, err := json.Marshal(jsonObj) + jsonBytes, err = json.Marshal(jsonObj) if err != nil { - return nil, fmt.Errorf("error converting YAML to JSON: %w", err) + return nil, strictErrors, fmt.Errorf("error converting YAML to JSON: %w", err) } - return jsonBytes, nil + return jsonBytes, strictErrors, nil } func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (interface{}, error) { @@ -198,7 +203,15 @@ func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (in // If yamlObj is a map or array, find the field that each key is // unmarshaling to, and when you recurse pass the reflect.Value for that // field back into this function. +cast_type: switch typedYAMLObj := yamlObj.(type) { + case map[string]interface{}: + temp := make(map[interface{}]interface{}) + for k, v := range typedYAMLObj { + temp[k] = v + } + yamlObj = temp + goto cast_type case map[interface{}]interface{}: // JSON does not support arbitrary keys in a map, so we must convert // these keys to strings. @@ -271,7 +284,17 @@ func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (in if f != nil { // Find the reflect.Value of the most preferential // struct field. - jtf := t.Field(f.index[0]) + jtf := t + for _, i := range f.index { + if jtf.Kind() == reflect.Ptr { + if jtf.IsNil() { + jtf = reflect.New(jtf.Type().Elem()) + } + jtf = jtf.Elem() + } + jtf = jtf.Field(i) + } + strMap[keyString], err = convertToJSONableObject(v, &jtf) if err != nil { return nil, err @@ -336,7 +359,7 @@ func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (in case int64: s = strconv.FormatInt(typedVal, 10) case float64: - s = strconv.FormatFloat(typedVal, 'g', -1, 32) + s = strconv.FormatFloat(typedVal, 'g', -1, 64) case uint64: s = strconv.FormatUint(typedVal, 10) case bool: @@ -350,67 +373,7 @@ func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (in yamlObj = interface{}(s) } } - return yamlObj, nil - } -} - -// JSONObjectToYAMLObject converts an in-memory JSON object into a YAML in-memory MapSlice, -// without going through a byte representation. A nil or empty map[string]interface{} input is -// converted to an empty map, i.e. yaml.MapSlice(nil). -// -// interface{} slices stay interface{} slices. map[string]interface{} becomes yaml.MapSlice. -// -// int64 and float64 are down casted following the logic of github.com/go-yaml/yaml: -// - float64s are down-casted as far as possible without data-loss to int, int64, uint64. -// - int64s are down-casted to int if possible without data-loss. -// -// Big int/int64/uint64 do not lose precision as in the json-yaml roundtripping case. -// -// string, bool and any other types are unchanged. -func JSONObjectToYAMLObject(j map[string]interface{}) yaml.MapSlice { - if len(j) == 0 { - return nil - } - ret := make(yaml.MapSlice, 0, len(j)) - for k, v := range j { - ret = append(ret, yaml.MapItem{Key: k, Value: jsonToYAMLValue(v)}) - } - return ret -} -func jsonToYAMLValue(j interface{}) interface{} { - switch j := j.(type) { - case map[string]interface{}: - if j == nil { - return interface{}(nil) - } - return JSONObjectToYAMLObject(j) - case []interface{}: - if j == nil { - return interface{}(nil) - } - ret := make([]interface{}, len(j)) - for i := range j { - ret[i] = jsonToYAMLValue(j[i]) - } - return ret - case float64: - // replicate the logic in https://github.com/go-yaml/yaml/blob/51d6538a90f86fe93ac480b35f37b2be17fef232/resolve.go#L151 - if i64 := int64(j); j == float64(i64) { - if i := int(i64); i64 == int64(i) { - return i - } - return i64 - } - if ui64 := uint64(j); j == float64(ui64) { - return ui64 - } - return j - case int64: - if i := int(j); j == int64(i) { - return i - } - return j + return yamlObj, nil } - return j } diff --git a/yaml_go110.go b/yaml_go110.go deleted file mode 100644 index 94abc17..0000000 --- a/yaml_go110.go +++ /dev/null @@ -1,31 +0,0 @@ -// This file contains changes that are only compatible with go 1.10 and onwards. - -//go:build go1.10 -// +build go1.10 - -/* -Copyright 2021 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package yaml - -import "encoding/json" - -// DisallowUnknownFields configures the JSON decoder to error out if unknown -// fields come along, instead of dropping them by default. -func DisallowUnknownFields(d *json.Decoder) *json.Decoder { - d.DisallowUnknownFields() - return d -} diff --git a/yaml_go110_test.go b/yaml_go110_test.go deleted file mode 100644 index 629fff4..0000000 --- a/yaml_go110_test.go +++ /dev/null @@ -1,63 +0,0 @@ -//go:build go1.10 -// +build go1.10 - -/* -Copyright 2021 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package yaml - -import ( - "fmt" - "testing" -) - -func TestUnmarshalWithTags(t *testing.T) { - type WithTaggedField struct { - Field string `json:"field"` - } - - t.Run("Known tagged field", func(t *testing.T) { - y := []byte(`field: "hello"`) - v := WithTaggedField{} - if err := Unmarshal(y, &v, DisallowUnknownFields); err != nil { - t.Errorf("unexpected error: %v", err) - } - if v.Field != "hello" { - t.Errorf("v.Field=%v, want 'hello'", v.Field) - } - - }) - t.Run("With unknown tagged field", func(t *testing.T) { - y := []byte(`unknown: "hello"`) - v := WithTaggedField{} - err := Unmarshal(y, &v, DisallowUnknownFields) - if err == nil { - t.Errorf("want error because of unknown field, got : v=%#v", v) - } - }) - -} - -func exampleUnknown() { - type WithTaggedField struct { - Field string `json:"field"` - } - y := []byte(`unknown: "hello"`) - v := WithTaggedField{} - fmt.Printf("%v\n", Unmarshal(y, &v, DisallowUnknownFields)) - // Ouptut: - // unmarshaling JSON: while decoding JSON: json: unknown field "unknown" -} diff --git a/yaml_test.go b/yaml_test.go index 785162a..410d5ba 100644 --- a/yaml_test.go +++ b/yaml_test.go @@ -17,16 +17,11 @@ limitations under the License. package yaml import ( - "encoding/json" "fmt" "math" "reflect" - "sort" "strconv" "testing" - - "github.com/davecgh/go-spew/spew" - yaml "gopkg.in/yaml.v2" ) /* Test helper functions */ @@ -38,8 +33,10 @@ func strPtr(str string) *string { type errorType int const ( - noErrorsType errorType = 0 - fatalErrorsType errorType = 1 << iota + noErrorsType errorType = 0 + strictErrorsType errorType = 1 << iota + fatalErrorsType + strictAndFatalErrorsType errorType = strictErrorsType | fatalErrorsType ) type unmarshalTestCase struct { @@ -49,14 +46,15 @@ type unmarshalTestCase struct { err errorType } -type testUnmarshalFunc = func(yamlBytes []byte, obj interface{}) error +type testUnmarshalFunc = func(yamlBytes []byte, obj interface{}) (strictErrors []error, err error) var ( - funcUnmarshal testUnmarshalFunc = func(yamlBytes []byte, obj interface{}) error { - return Unmarshal(yamlBytes, obj) + funcUnmarshal testUnmarshalFunc = func(yamlBytes []byte, obj interface{}) (strictErrors []error, err error) { + err = Unmarshal(yamlBytes, obj) + return []error{}, err } - funcUnmarshalStrict testUnmarshalFunc = func(yamlBytes []byte, obj interface{}) error { + funcUnmarshalStrict testUnmarshalFunc = func(yamlBytes []byte, obj interface{}) (strictErrors []error, err error) { return UnmarshalStrict(yamlBytes, obj) } ) @@ -81,9 +79,12 @@ func testUnmarshal(t *testing.T, f testUnmarshalFunc, tests map[string]unmarshal t.Errorf("unmarshalTest.ptr %#v is not a pointer to a zero value", test.decodeInto) } - err := f(test.encoded, value.Interface()) - if err != nil && test.err == noErrorsType { - t.Errorf("error unmarshaling YAML: %v", err) + strictErrors, err := f(test.encoded, value.Interface()) + if (err != nil || len(strictErrors) > 0) && test.err == noErrorsType { + t.Errorf("error unmarshaling YAML: %v %v", err, strictErrors) + } + if len(strictErrors) == 0 && test.err&strictErrorsType != 0 { + t.Errorf("expected strict errors, but no strict error was returned, yaml: `%s`", test.encoded) } if err == nil && test.err&fatalErrorsType != 0 { t.Errorf("expected a fatal error, but no fatal error was returned, yaml: `%s`", test.encoded) @@ -106,19 +107,16 @@ type yamlToJSONTestcase struct { json string // By default we test that reversing the output == input. But if there is a // difference in the reversed output, you can optionally specify it here. - yaml_reverse_overwrite *string - err errorType + yamlReverseOverwrite *string + err errorType } -type testYAMLToJSONFunc = func(yamlBytes []byte) ([]byte, error) +type testYAMLToJSONFunc = func(yamlBytes []byte) (json []byte, strictErrors []error, err error) var ( - funcYAMLToJSON testYAMLToJSONFunc = func(yamlBytes []byte) ([]byte, error) { - return YAMLToJSON(yamlBytes) - } - - funcYAMLToJSONStrict testYAMLToJSONFunc = func(yamlBytes []byte) ([]byte, error) { - return YAMLToJSONStrict(yamlBytes) + funcYAMLToJSON testYAMLToJSONFunc = func(yamlBytes []byte) (json []byte, strictErrors []error, err error) { + json, err = YAMLToJSON(yamlBytes) + return json, []error{}, err } ) @@ -126,9 +124,12 @@ func testYAMLToJSON(t *testing.T, f testYAMLToJSONFunc, tests map[string]yamlToJ for testName, test := range tests { t.Run(fmt.Sprintf("%s_YAMLToJSON", testName), func(t *testing.T) { // Convert Yaml to Json - jsonBytes, err := f([]byte(test.yaml)) - if err != nil && test.err == noErrorsType { - t.Errorf("Failed to convert YAML to JSON, yaml: `%s`, err: %v", test.yaml, err) + jsonBytes, strictErrors, err := f([]byte(test.yaml)) + if (err != nil || len(strictErrors) > 0) && test.err == noErrorsType { + t.Errorf("Failed to convert YAML to JSON, yaml: `%s`, err: %v %v", test.yaml, err, strictErrors) + } + if len(strictErrors) == 0 && test.err&strictErrorsType != 0 { + t.Errorf("expected strict errors, but no strict error was returned, yaml: `%s`", test.yaml) } if err == nil && test.err&fatalErrorsType != 0 { t.Errorf("expected a fatal error, but no fatal error was returned, yaml: `%s`", test.yaml) @@ -156,8 +157,8 @@ func testYAMLToJSON(t *testing.T, f testYAMLToJSONFunc, tests map[string]yamlToJ correctYamlString := test.yaml // If a special reverse string was specified, use that instead. - if test.yaml_reverse_overwrite != nil { - correctYamlString = *test.yaml_reverse_overwrite + if test.yamlReverseOverwrite != nil { + correctYamlString = *test.yamlReverseOverwrite } // Check it against the expected output. @@ -248,27 +249,12 @@ type UnmarshalEmbedRecursiveStruct struct { func TestUnmarshal(t *testing.T) { tests := map[string]unmarshalTestCase{ - // casematched / non-casematched untagged keys + // casematched untagged keys "untagged casematched string key": { encoded: []byte("A: test"), decodeInto: new(UnmarshalUntaggedStruct), decoded: UnmarshalUntaggedStruct{A: "test"}, }, - "untagged non-casematched string key": { - encoded: []byte("a: test"), - decodeInto: new(UnmarshalUntaggedStruct), - decoded: UnmarshalUntaggedStruct{A: "test"}, - }, - "untagged casematched boolean key": { - encoded: []byte("True: test"), - decodeInto: new(UnmarshalUntaggedStruct), - decoded: UnmarshalUntaggedStruct{True: "test"}, - }, - "untagged non-casematched boolean key": { - encoded: []byte("true: test"), - decodeInto: new(UnmarshalUntaggedStruct), - decoded: UnmarshalUntaggedStruct{True: "test"}, - }, // casematched / non-casematched tagged keys "tagged casematched string key": { @@ -294,12 +280,12 @@ func TestUnmarshal(t *testing.T) { "tagged casematched boolean key (yes)": { encoded: []byte("Yes: test"), decodeInto: new(UnmarshalTaggedStruct), - decoded: UnmarshalTaggedStruct{TrueLower: "test"}, + decoded: UnmarshalTaggedStruct{YesUpper: "test"}, }, "tagged non-casematched boolean key (yes)": { encoded: []byte("yes: test"), decodeInto: new(UnmarshalTaggedStruct), - decoded: UnmarshalTaggedStruct{TrueLower: "test"}, + decoded: UnmarshalTaggedStruct{YesLower: "test"}, }, "tagged integer key": { encoded: []byte("3: test"), @@ -341,7 +327,7 @@ func TestUnmarshal(t *testing.T) { "boolean value (no) into string field": { encoded: []byte("a: no"), decodeInto: new(UnmarshalStruct), - decoded: UnmarshalStruct{A: "false"}, + decoded: UnmarshalStruct{A: "no"}, }, // decode into complex fields @@ -388,7 +374,7 @@ func TestUnmarshal(t *testing.T) { encoded: []byte("Yes:"), decodeInto: new(map[string]struct{}), decoded: map[string]struct{}{ - "true": {}, + "Yes": {}, }, }, "string map: decode integer key": { @@ -415,19 +401,19 @@ func TestUnmarshal(t *testing.T) { "decode 2^53 + 1 into interface": { encoded: []byte("9007199254740993"), decodeInto: new(interface{}), - decoded: 9.007199254740992e+15, + decoded: int64(9007199254740993), }, // decode into interface "float into interface": { encoded: []byte("3.0"), decodeInto: new(interface{}), - decoded: float64(3), + decoded: int64(3), }, "integer into interface": { encoded: []byte("3"), decodeInto: new(interface{}), - decoded: float64(3), + decoded: int64(3), }, "empty vs empty string into interface": { encoded: []byte("a: \"\"\nb: \n"), @@ -438,47 +424,6 @@ func TestUnmarshal(t *testing.T) { }, }, - // duplicate (non-casematched) keys (NOTE: this is very non-ideal behaviour!) - "decode duplicate (non-casematched) into nested struct 1": { - encoded: []byte("a:\n a: 1\n b: 1\n c: test\n\nA:\n a: 2"), - decodeInto: new(UnmarshalNestedStruct), - decoded: UnmarshalNestedStruct{A: UnmarshalStruct{A: "1", B: strPtr("1"), C: "test"}}, - }, - "decode duplicate (non-casematched) into nested struct 2": { - encoded: []byte("A:\n a: 1\n b: 1\n c: test\na:\n a: 2"), - decodeInto: new(UnmarshalNestedStruct), - decoded: UnmarshalNestedStruct{A: UnmarshalStruct{A: "2", B: strPtr("1"), C: "test"}}, - }, - "decode duplicate (non-casematched) into nested slice 1": { - encoded: []byte("a:\n - a: abc\n b: def\nA:\n - a: 123"), - decodeInto: new(UnmarshalSlice), - decoded: UnmarshalSlice{[]UnmarshalStruct{{A: "abc", B: strPtr("def")}}}, - }, - "decode duplicate (non-casematched) into nested slice 2": { - encoded: []byte("A:\n - a: abc\n b: def\na:\n - a: 123"), - decodeInto: new(UnmarshalSlice), - decoded: UnmarshalSlice{[]UnmarshalStruct{{A: "123", B: strPtr("def")}}}, - }, - "decode duplicate (non-casematched) into nested string map 1": { - encoded: []byte("a:\n b: 1\nA:\n c: 1"), - decodeInto: new(UnmarshalStringMap), - decoded: UnmarshalStringMap{map[string]string{"b": "1", "c": "1"}}, - }, - "decode duplicate (non-casematched) into nested string map 2": { - encoded: []byte("A:\n b: 1\na:\n c: 1"), - decodeInto: new(UnmarshalStringMap), - decoded: UnmarshalStringMap{map[string]string{"b": "1", "c": "1"}}, - }, - "decode duplicate (non-casematched) into string map": { - encoded: []byte("a: test\nb: test\nA: test2"), - decodeInto: new(map[string]string), - decoded: map[string]string{ - "a": "test", - "A": "test2", - "b": "test", - }, - }, - // decoding embeded structs "decode embeded struct": { encoded: []byte("a: testA\nb: testB"), @@ -510,8 +455,6 @@ func TestUnmarshal(t *testing.T) { B: "testB", }, }, - - // BUG: type info gets lost (#58) "decode embeded struct and cast integer to string": { encoded: []byte("a: 11\nb: testB"), decodeInto: new(UnmarshalEmbedStruct), @@ -521,7 +464,6 @@ func TestUnmarshal(t *testing.T) { }, B: "testB", }, - err: fatalErrorsType, }, "decode embeded structpointer and cast integer to string": { encoded: []byte("a: 11\nb: testB"), @@ -532,7 +474,6 @@ func TestUnmarshal(t *testing.T) { }, B: "testB", }, - err: fatalErrorsType, }, // decoding into incompatible type @@ -541,19 +482,7 @@ func TestUnmarshal(t *testing.T) { decodeInto: new(UnmarshalStringMap), err: fatalErrorsType, }, - } - t.Run("Unmarshal", func(t *testing.T) { - testUnmarshal(t, funcUnmarshal, tests) - }) - - t.Run("UnmarshalStrict", func(t *testing.T) { - testUnmarshal(t, funcUnmarshalStrict, tests) - }) -} - -func TestUnmarshalStrictFails(t *testing.T) { - tests := map[string]unmarshalTestCase{ // decoding with duplicate values "decode into struct pointer map with duplicate string value": { encoded: []byte("a:\n a: TestA\n b: ID-A\n b: ID-1"), @@ -561,23 +490,19 @@ func TestUnmarshalStrictFails(t *testing.T) { decoded: map[string]*UnmarshalStruct{ "a": {A: "TestA", B: strPtr("ID-1")}, }, + err: fatalErrorsType, }, "decode into string field with duplicate boolean value": { encoded: []byte("a: true\na: false"), decodeInto: new(UnmarshalStruct), decoded: UnmarshalStruct{A: "false"}, + err: fatalErrorsType, }, "decode into slice with duplicate string-boolean value": { encoded: []byte("a:\n- b: abc\n a: 32\n b: 123"), decodeInto: new(UnmarshalSlice), decoded: UnmarshalSlice{[]UnmarshalStruct{{A: "32", B: strPtr("123")}}}, - }, - - // decoding with unknown fields - "decode into struct with unknown field": { - encoded: []byte("a: TestB\nb: ID-B\nunknown: Some-Value"), - decodeInto: new(UnmarshalStruct), - decoded: UnmarshalStruct{A: "TestB", B: strPtr("ID-B")}, + err: fatalErrorsType, }, // decoding with duplicate complex values @@ -585,16 +510,19 @@ func TestUnmarshalStrictFails(t *testing.T) { encoded: []byte("a:\n a: 1\na:\n a: 2"), decodeInto: new(UnmarshalNestedStruct), decoded: UnmarshalNestedStruct{A: UnmarshalStruct{A: "2"}}, + err: fatalErrorsType, }, "decode duplicate into nested slice": { encoded: []byte("a:\n - a: abc\n b: def\na:\n - a: 123"), decodeInto: new(UnmarshalSlice), decoded: UnmarshalSlice{[]UnmarshalStruct{{A: "123"}}}, + err: fatalErrorsType, }, "decode duplicate into nested string map": { encoded: []byte("a:\n b: 1\na:\n c: 1"), decodeInto: new(UnmarshalStringMap), decoded: UnmarshalStringMap{map[string]string{"c": "1"}}, + err: fatalErrorsType, }, "decode duplicate into string map": { encoded: []byte("a: test\nb: test\na: test2"), @@ -603,6 +531,86 @@ func TestUnmarshalStrictFails(t *testing.T) { "a": "test2", "b": "test", }, + err: fatalErrorsType, + }, + + // duplicate (non-casematched) keys + "decode duplicate (non-casematched) into string map": { + encoded: []byte("a: test\nb: test\nA: test2"), + decodeInto: new(map[string]string), + decoded: map[string]string{ + "a": "test", + "A": "test2", + "b": "test", + }, + }, + } + + t.Run("Unmarshal", func(t *testing.T) { + testUnmarshal(t, funcUnmarshal, tests) + }) + + t.Run("UnmarshalStrict", func(t *testing.T) { + testUnmarshal(t, funcUnmarshalStrict, tests) + }) +} + +func TestUnmarshalStrictFails(t *testing.T) { + tests := map[string]unmarshalTestCase{ + // non-casematched untagged keys + "untagged non-casematched string key": { + encoded: []byte("a: test"), + decodeInto: new(UnmarshalUntaggedStruct), + decoded: UnmarshalUntaggedStruct{}, + }, + "untagged casematched boolean key": { + encoded: []byte("True: test"), + decodeInto: new(UnmarshalUntaggedStruct), + decoded: UnmarshalUntaggedStruct{}, // BUG: because True is a boolean, it is converted to the string "true" which does not match the fieldname + }, + "untagged non-casematched boolean key": { + encoded: []byte("true: test"), + decodeInto: new(UnmarshalUntaggedStruct), + decoded: UnmarshalUntaggedStruct{}, + }, + + // duplicate (non-casematched) keys + "decode duplicate (non-casematched) into nested struct 1": { + encoded: []byte("a:\n a: 1\n b: 1\n c: test\n\nA:\n a: 2"), + decodeInto: new(UnmarshalNestedStruct), + decoded: UnmarshalNestedStruct{A: UnmarshalStruct{A: "1", B: strPtr("1"), C: "test"}}, + }, + "decode duplicate (non-casematched) into nested struct 2": { + encoded: []byte("A:\n a: 1\n b: 1\n c: test\na:\n a: 2"), + decodeInto: new(UnmarshalNestedStruct), + decoded: UnmarshalNestedStruct{A: UnmarshalStruct{A: "2"}}, + }, + "decode duplicate (non-casematched) into nested slice 1": { + encoded: []byte("a:\n - a: abc\n b: def\nA:\n - a: 123"), + decodeInto: new(UnmarshalSlice), + decoded: UnmarshalSlice{[]UnmarshalStruct{{A: "abc", B: strPtr("def")}}}, + }, + "decode duplicate (non-casematched) into nested slice 2": { + encoded: []byte("A:\n - a: abc\n b: def\na:\n - a: 123"), + decodeInto: new(UnmarshalSlice), + decoded: UnmarshalSlice{[]UnmarshalStruct{{A: "123"}}}, + }, + "decode duplicate (non-casematched) into nested string map 1": { + encoded: []byte("a:\n b: 1\nA:\n c: 1"), + decodeInto: new(UnmarshalStringMap), + decoded: UnmarshalStringMap{map[string]string{"b": "1"}}, + }, + "decode duplicate (non-casematched) into nested string map 2": { + encoded: []byte("A:\n b: 1\na:\n c: 1"), + decodeInto: new(UnmarshalStringMap), + decoded: UnmarshalStringMap{map[string]string{"c": "1"}}, + }, + + // decoding with unknown fields + "decode into struct with unknown field": { + encoded: []byte("a: TestB\nb: ID-B\nunknown: Some-Value"), + decodeInto: new(UnmarshalStruct), + decoded: UnmarshalStruct{A: "TestB", B: strPtr("ID-B")}, }, } @@ -613,7 +621,7 @@ func TestUnmarshalStrictFails(t *testing.T) { t.Run("UnmarshalStrict", func(t *testing.T) { failTests := map[string]unmarshalTestCase{} for name, test := range tests { - test.err = fatalErrorsType + test.err |= strictErrorsType failTests[name] = test } testUnmarshal(t, funcUnmarshalStrict, failTests) @@ -631,63 +639,64 @@ func TestYAMLToJSON(t *testing.T) { json: `{"t":null}`, }, "boolean value": { - yaml: "t: True\n", - json: `{"t":true}`, - yaml_reverse_overwrite: strPtr("t: true\n"), + yaml: "t: True\n", + json: `{"t":true}`, + yamlReverseOverwrite: strPtr("t: true\n"), }, "boolean value (no)": { - yaml: "t: no\n", - json: `{"t":false}`, - yaml_reverse_overwrite: strPtr("t: false\n"), + yaml: "t: no\n", + json: `{"t":"no"}`, + yamlReverseOverwrite: strPtr("t: \"no\"\n"), }, "integer value (2^53 + 1)": { - yaml: "t: 9007199254740993\n", - json: `{"t":9007199254740993}`, - yaml_reverse_overwrite: strPtr("t: 9007199254740993\n"), + yaml: "t: 9007199254740993\n", + json: `{"t":9007199254740993}`, + yamlReverseOverwrite: strPtr("t: 9007199254740993\n"), }, "integer value (1000000000000000000000000000000000000)": { - yaml: "t: 1000000000000000000000000000000000000\n", - json: `{"t":1e+36}`, - yaml_reverse_overwrite: strPtr("t: 1e+36\n"), + yaml: "t: 1000000000000000000000000000000000000\n", + json: `{"t":1e+36}`, + yamlReverseOverwrite: strPtr("t: 1e+36\n"), }, "line-wrapped string value": { - yaml: "t: this is very long line with spaces and it must be longer than 80 so we will repeat\n that it must be longer that 80\n", - json: `{"t":"this is very long line with spaces and it must be longer than 80 so we will repeat that it must be longer that 80"}`, + yaml: "t: this is very long line with spaces and it must be longer than 80 so we will repeat\n that it must be longer that 80\n", + json: `{"t":"this is very long line with spaces and it must be longer than 80 so we will repeat that it must be longer that 80"}`, + yamlReverseOverwrite: strPtr("t: this is very long line with spaces and it must be longer than 80 so we will repeat that it must be longer that 80\n"), }, "empty yaml value": { - yaml: "t: ", - json: `{"t":null}`, - yaml_reverse_overwrite: strPtr("t: null\n"), + yaml: "t: ", + json: `{"t":null}`, + yamlReverseOverwrite: strPtr("t: null\n"), }, "boolean key": { - yaml: "True: a", - json: `{"true":"a"}`, - yaml_reverse_overwrite: strPtr("\"true\": a\n"), + yaml: "True: a", + json: `{"true":"a"}`, + yamlReverseOverwrite: strPtr("\"true\": a\n"), }, "boolean key (no)": { - yaml: "no: a", - json: `{"false":"a"}`, - yaml_reverse_overwrite: strPtr("\"false\": a\n"), + yaml: "no: a", + json: `{"no":"a"}`, + yamlReverseOverwrite: strPtr("\"no\": a\n"), }, "integer key": { - yaml: "1: a", - json: `{"1":"a"}`, - yaml_reverse_overwrite: strPtr("\"1\": a\n"), + yaml: "1: a", + json: `{"1":"a"}`, + yamlReverseOverwrite: strPtr("\"1\": a\n"), }, "float key": { - yaml: "1.2: a", - json: `{"1.2":"a"}`, - yaml_reverse_overwrite: strPtr("\"1.2\": a\n"), + yaml: "1.2: a", + json: `{"1.2":"a"}`, + yamlReverseOverwrite: strPtr("\"1.2\": a\n"), }, "large integer key": { - yaml: "1000000000000000000000000000000000000: a", - json: `{"1e+36":"a"}`, - yaml_reverse_overwrite: strPtr("\"1e+36\": a\n"), + yaml: "1000000000000000000000000000000000000: a", + json: `{"1e+36":"a"}`, + yamlReverseOverwrite: strPtr("\"1e+36\": a\n"), }, "large integer key (scientific notation)": { - yaml: "1e+36: a", - json: `{"1e+36":"a"}`, - yaml_reverse_overwrite: strPtr("\"1e+36\": a\n"), + yaml: "1e+36: a", + json: `{"1e+36":"a"}`, + yamlReverseOverwrite: strPtr("\"1e+36\": a\n"), }, "string key (large integer as string)": { yaml: "\"1e+36\": a\n", @@ -706,187 +715,49 @@ func TestYAMLToJSON(t *testing.T) { json: `[{"t":"a"},{"t":{"b":1,"c":2}}]`, }, "nested struct array (json notation)": { - yaml: `[{t: a}, {t: {b: 1, c: 2}}]`, - json: `[{"t":"a"},{"t":{"b":1,"c":2}}]`, - yaml_reverse_overwrite: strPtr("- t: a\n- t:\n b: 1\n c: 2\n"), + yaml: `[{t: a}, {t: {b: 1, c: 2}}]`, + json: `[{"t":"a"},{"t":{"b":1,"c":2}}]`, + yamlReverseOverwrite: strPtr("- t: a\n- t:\n b: 1\n c: 2\n"), }, "empty struct value": { - yaml: "- t: ", - json: `[{"t":null}]`, - yaml_reverse_overwrite: strPtr("- t: null\n"), + yaml: "- t: ", + json: `[{"t":null}]`, + yamlReverseOverwrite: strPtr("- t: null\n"), }, "null struct value": { yaml: "- t: null\n", json: `[{"t":null}]`, }, "binary data": { - yaml: "a: !!binary gIGC", - json: `{"a":"\ufffd\ufffd\ufffd"}`, - yaml_reverse_overwrite: strPtr("a: \ufffd\ufffd\ufffd\n"), + yaml: "a: !!binary gIGC", + json: `{"a":"\ufffd\ufffd\ufffd"}`, + yamlReverseOverwrite: strPtr("a: \ufffd\ufffd\ufffd\n"), }, // Cases that should produce errors. "~ key": { - yaml: "~: a", - json: `{"null":"a"}`, - yaml_reverse_overwrite: strPtr("\"null\": a\n"), - err: fatalErrorsType, + yaml: "~: a", + json: `{"null":"a"}`, + yamlReverseOverwrite: strPtr("\"null\": a\n"), + err: fatalErrorsType, }, "null key": { - yaml: "null: a", - json: `{"null":"a"}`, - yaml_reverse_overwrite: strPtr("\"null\": a\n"), - err: fatalErrorsType, + yaml: "null: a", + json: `{"null":"a"}`, + yamlReverseOverwrite: strPtr("\"null\": a\n"), + err: fatalErrorsType, }, - } - t.Run("YAMLToJSON", func(t *testing.T) { - testYAMLToJSON(t, funcYAMLToJSON, tests) - }) - - t.Run("YAMLToJSONStrict", func(t *testing.T) { - testYAMLToJSON(t, funcYAMLToJSONStrict, tests) - }) -} - -func TestYAMLToJSONStrictFails(t *testing.T) { - tests := map[string]yamlToJSONTestcase{ - // expect YAMLtoJSON to pass on duplicate field names + // expect YAMLtoJSON to fail on duplicate field names "duplicate struct value": { - yaml: "foo: bar\nfoo: baz\n", - json: `{"foo":"baz"}`, - yaml_reverse_overwrite: strPtr("foo: baz\n"), + yaml: "foo: bar\nfoo: baz\n", + json: `{"foo":"baz"}`, + yamlReverseOverwrite: strPtr("foo: baz\n"), + err: fatalErrorsType, }, } t.Run("YAMLToJSON", func(t *testing.T) { testYAMLToJSON(t, funcYAMLToJSON, tests) }) - - t.Run("YAMLToJSONStrict", func(t *testing.T) { - failTests := map[string]yamlToJSONTestcase{} - for name, test := range tests { - test.err = fatalErrorsType - failTests[name] = test - } - testYAMLToJSON(t, funcYAMLToJSONStrict, failTests) - }) -} - -func TestJSONObjectToYAMLObject(t *testing.T) { - const bigUint64 = ((uint64(1) << 63) + 500) / 1000 * 1000 - intOrInt64 := func(i64 int64) interface{} { - if i := int(i64); i64 == int64(i) { - return i - } - return i64 - } - - tests := []struct { - name string - input map[string]interface{} - expected yaml.MapSlice - }{ - {name: "nil", expected: yaml.MapSlice(nil)}, - {name: "empty", input: map[string]interface{}{}, expected: yaml.MapSlice(nil)}, - { - name: "values", - input: map[string]interface{}{ - "nil slice": []interface{}(nil), - "nil map": map[string]interface{}(nil), - "empty slice": []interface{}{}, - "empty map": map[string]interface{}{}, - "bool": true, - "float64": float64(42.1), - "fractionless": float64(42), - "int": int(42), - "int64": int64(42), - "int64 big": float64(math.Pow(2, 62)), - "negative int64 big": -float64(math.Pow(2, 62)), - "map": map[string]interface{}{"foo": "bar"}, - "slice": []interface{}{"foo", "bar"}, - "string": string("foo"), - "uint64 big": bigUint64, - }, - expected: yaml.MapSlice{ - {Key: "nil slice"}, - {Key: "nil map"}, - {Key: "empty slice", Value: []interface{}{}}, - {Key: "empty map", Value: yaml.MapSlice(nil)}, - {Key: "bool", Value: true}, - {Key: "float64", Value: float64(42.1)}, - {Key: "fractionless", Value: int(42)}, - {Key: "int", Value: int(42)}, - {Key: "int64", Value: int(42)}, - {Key: "int64 big", Value: intOrInt64(int64(1) << 62)}, - {Key: "negative int64 big", Value: intOrInt64(-(1 << 62))}, - {Key: "map", Value: yaml.MapSlice{{Key: "foo", Value: "bar"}}}, - {Key: "slice", Value: []interface{}{"foo", "bar"}}, - {Key: "string", Value: string("foo")}, - {Key: "uint64 big", Value: bigUint64}, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := JSONObjectToYAMLObject(tt.input) - sortMapSlicesInPlace(tt.expected) - sortMapSlicesInPlace(got) - if !reflect.DeepEqual(got, tt.expected) { - t.Errorf("jsonToYAML() = %v, want %v", spew.Sdump(got), spew.Sdump(tt.expected)) - } - - jsonBytes, err := json.Marshal(tt.input) - if err != nil { - t.Fatalf("unexpected json.Marshal error: %v", err) - } - var gotByRoundtrip yaml.MapSlice - if err := yaml.Unmarshal(jsonBytes, &gotByRoundtrip); err != nil { - t.Fatalf("unexpected yaml.Unmarshal error: %v", err) - } - - // yaml.Unmarshal loses precision, it's rounding to the 4th last digit. - // Replicate this here in the test, but don't change the type. - for i := range got { - switch got[i].Key { - case "int64 big", "uint64 big", "negative int64 big": - switch v := got[i].Value.(type) { - case int64: - d := int64(500) - if v < 0 { - d = -500 - } - got[i].Value = int64((v+d)/1000) * 1000 - case uint64: - got[i].Value = uint64((v+500)/1000) * 1000 - case int: - d := int(500) - if v < 0 { - d = -500 - } - got[i].Value = int((v+d)/1000) * 1000 - default: - t.Fatalf("unexpected type for key %s: %v:%T", got[i].Key, v, v) - } - } - } - - if !reflect.DeepEqual(got, gotByRoundtrip) { - t.Errorf("yaml.Unmarshal(json.Marshal(tt.input)) = %v, want %v\njson: %s", spew.Sdump(gotByRoundtrip), spew.Sdump(got), string(jsonBytes)) - } - }) - } -} - -func sortMapSlicesInPlace(x interface{}) { - switch x := x.(type) { - case []interface{}: - for i := range x { - sortMapSlicesInPlace(x[i]) - } - case yaml.MapSlice: - sort.Slice(x, func(a, b int) bool { - return x[a].Key.(string) < x[b].Key.(string) - }) - } }