Skip to content

Commit

Permalink
refactor tests & update yaml lib
Browse files Browse the repository at this point in the history
Signed-off-by: Inteon <42113979+inteon@users.noreply.github.com>
  • Loading branch information
inteon committed Oct 23, 2021
1 parent 6ef1584 commit a4ea27a
Show file tree
Hide file tree
Showing 5 changed files with 634 additions and 315 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ require (
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6
)

replace gopkg.in/yaml.v3 => github.com/amurant/go-yaml v0.0.0-20211021125301-a69ce44590ee
replace gopkg.in/yaml.v3 => github.com/amurant/go-yaml v0.0.0-20211023093135-e364698f17de
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/amurant/go-yaml v0.0.0-20211021125301-a69ce44590ee h1:hh4GlzhK0sFfqNroFo5dgDa4NylwldjzlweQzm85sdI=
github.com/amurant/go-yaml v0.0.0-20211021125301-a69ce44590ee/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
github.com/amurant/go-yaml v0.0.0-20211023093135-e364698f17de h1:9CsNtWchSqdRcj0ehywK8mKnZS2Z5A9U0rL0/tdgtX8=
github.com/amurant/go-yaml v0.0.0-20211023093135-e364698f17de/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
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=
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 h1:fD1pz4yfdADVNfFmcP2aBEtudwUQ1AlLnRBALr33v3s=
Expand Down
10 changes: 3 additions & 7 deletions marshal.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package yaml

import (
"encoding/json"
"fmt"

"encoding/json"
kubejson "sigs.k8s.io/json"

"gopkg.in/yaml.v3"
)
Expand All @@ -13,12 +14,7 @@ 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.
if err := yaml.Unmarshal(jsonBytes, &jsonObj); err != nil {
if err := kubejson.UnmarshalCaseSensitivePreserveInts(jsonBytes, &jsonObj); err != nil {
return nil, fmt.Errorf("error converting JSON to YAML: %v", err)
}

Expand Down
12 changes: 8 additions & 4 deletions unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ const (
func yamlUnmarshal(yamlBytes []byte, v interface{}, options yamlToTargetOption) (strictErrors []error, err error) {
dec := yaml.NewDecoder(bytes.NewReader(yamlBytes))
dec.KnownFields(options&disallowUnknownFields != 0)
dec.DisableUniqueKeys(options&disallowDuplicateFields != 0)
dec.DisableUniqueKeys(options&disallowDuplicateFields == 0)
err = dec.Decode(v)
if e, ok := err.(*yaml.TypeError); ok {
errs := make([]error, 0, len(e.StrictErrors))
for _, err := range e.StrictErrors {
errs = append(errs, errors.New(err))
}
return errs, &yaml.TypeError{e.Errors, nil}
if len(e.Errors) == 0 {
return errs, nil
} else {
return errs, &yaml.TypeError{e.Errors, nil}
}
}
return nil, err
}
Expand Down Expand Up @@ -108,13 +112,13 @@ func YAMLToJSONStrict(yamlBytes []byte, strictOptions ...StrictOption) (json []b
case DisallowDuplicateFields:
options = options | disallowDuplicateFields
case DisallowUnknownFields:
options = options | disallowUnknownFields
return nil, nil, fmt.Errorf("strict option `DisallowUnknownFields` is not supported for YAMLToJSONStrict")
default:
return nil, nil, fmt.Errorf("unknown strict option %d", strictOpt)
}
}
} else {
options = none | disallowDuplicateFields | disallowUnknownFields
options = none | disallowDuplicateFields
}
return yamlToJSONTarget(yamlBytes, nil, options)
}
Expand Down

0 comments on commit a4ea27a

Please sign in to comment.