Skip to content

Commit

Permalink
split yaml.go in marshal.go & unmarshal.go, rename functions and vari…
Browse files Browse the repository at this point in the history
…ables, improve error messages & wrapping

Signed-off-by: Inteon <42113979+inteon@users.noreply.github.com>
  • Loading branch information
inteon committed Oct 26, 2021
1 parent 4924d91 commit 23f10d0
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 94 deletions.
43 changes: 43 additions & 0 deletions marshal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package yaml

import (
"encoding/json"
"fmt"

"gopkg.in/yaml.v2"
)

// JSONToYAML Converts JSON to YAML.
func JSONToYAML(j []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 {
return nil, fmt.Errorf("error converting JSON to YAML: %w", err)
}

// Marshal this object into YAML.
yamlBytes, err := yaml.Marshal(jsonObj)
if err != nil {
return nil, fmt.Errorf("error converting JSON to YAML: %w", err)
}

return yamlBytes, nil
}

// Marshal marshals the object into JSON then converts JSON to YAML and returns the
// YAML.
func Marshal(obj interface{}) ([]byte, error) {
jsonBytes, err := json.Marshal(obj)
if err != nil {
return nil, fmt.Errorf("error marshaling into JSON: %w", err)
}

return JSONToYAML(jsonBytes)
}
158 changes: 64 additions & 94 deletions yaml.go → unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,89 +11,29 @@ import (
"gopkg.in/yaml.v2"
)

// Marshal marshals the object into JSON then converts JSON to YAML and returns the
// YAML.
func Marshal(o interface{}) ([]byte, error) {
j, err := json.Marshal(o)
if err != nil {
return nil, fmt.Errorf("error marshaling into JSON: %v", err)
}

y, err := JSONToYAML(j)
if err != nil {
return nil, fmt.Errorf("error converting JSON to YAML: %v", err)
}

return y, nil
}

// JSONOpt is a decoding option for decoding from JSON format.
type JSONOpt func(*json.Decoder) *json.Decoder

// Unmarshal converts YAML to JSON then uses JSON to unmarshal into an object,
// optionally configuring the behavior of the JSON unmarshal.
func Unmarshal(y []byte, o interface{}, opts ...JSONOpt) error {
return yamlUnmarshal(y, o, false, opts...)
}

// UnmarshalStrict strictly converts YAML to JSON then uses JSON to unmarshal
// into an object, optionally configuring the behavior of the JSON unmarshal.
func UnmarshalStrict(y []byte, o interface{}, opts ...JSONOpt) error {
return yamlUnmarshal(y, o, true, append(opts, DisallowUnknownFields)...)
}

// yamlUnmarshal unmarshals the given YAML byte stream into the given interface,
// optionally performing the unmarshalling strictly
func yamlUnmarshal(y []byte, o interface{}, strict bool, opts ...JSONOpt) error {
vo := reflect.ValueOf(o)
unmarshalFn := yaml.Unmarshal
if strict {
unmarshalFn = yaml.UnmarshalStrict
}
j, err := yamlToJSON(y, &vo, unmarshalFn)
func yamlToJSONTarget(yamlBytes []byte, jsonTarget *reflect.Value, unmarshalFn func([]byte, interface{}) error) ([]byte, error) {
// Convert the YAML to an object.
var yamlObj interface{}
err := unmarshalFn(yamlBytes, &yamlObj)
if err != nil {
return fmt.Errorf("error converting YAML to JSON: %v", err)
return nil, fmt.Errorf("error converting YAML to JSON: %w", err)
}

err = jsonUnmarshal(bytes.NewReader(j), o, opts...)
// YAML objects are not completely compatible with JSON objects (e.g. you
// can have non-string keys in YAML). So, convert the YAML-compatible object
// to a JSON-compatible object, failing with an error if irrecoverable
// incompatibilties happen along the way.
jsonObj, err := convertToJSONableObject(yamlObj, jsonTarget)
if err != nil {
return fmt.Errorf("error unmarshaling JSON: %v", err)
}

return 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(r io.Reader, o interface{}, opts ...JSONOpt) error {
d := json.NewDecoder(r)
for _, opt := range opts {
d = opt(d)
return nil, fmt.Errorf("error converting YAML to JSON: %w", err)
}
if err := d.Decode(&o); err != nil {
return fmt.Errorf("while decoding JSON: %v", err)
}
return nil
}

// JSONToYAML Converts JSON to YAML.
func JSONToYAML(j []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)
// Convert this object to JSON and return the data.
jsonBytes, err := json.Marshal(jsonObj)
if err != nil {
return nil, err
return nil, fmt.Errorf("error converting YAML to JSON: %w", err)
}

// Marshal this object into YAML.
return yaml.Marshal(jsonObj)
return jsonBytes, nil
}

// YAMLToJSON converts YAML to JSON. Since JSON is a subset of YAML,
Expand All @@ -109,34 +49,64 @@ func JSONToYAML(j []byte) ([]byte, error) {
//
// For strict decoding of YAML, use YAMLToJSONStrict.
func YAMLToJSON(y []byte) ([]byte, error) {
return yamlToJSON(y, nil, yaml.Unmarshal)
return yamlToJSONTarget(y, nil, yaml.Unmarshal)
}

// YAMLToJSONStrict is like YAMLToJSON but enables strict YAML decoding,
// returning an error on any duplicate field names.
func YAMLToJSONStrict(y []byte) ([]byte, error) {
return yamlToJSON(y, nil, yaml.UnmarshalStrict)
return yamlToJSONTarget(y, nil, yaml.UnmarshalStrict)
}

func yamlToJSON(y []byte, jsonTarget *reflect.Value, yamlUnmarshal func([]byte, interface{}) error) ([]byte, error) {
// Convert the YAML to an object.
var yamlObj interface{}
err := yamlUnmarshal(y, &yamlObj)
// JSONOpt is a decoding option for decoding from JSON format.
type JSONOpt func(*json.Decoder) *json.Decoder

// 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)
}
if err := d.Decode(&obj); err != nil {
return err
}
return nil
}

// 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 {
jsonTarget := reflect.ValueOf(obj)
if jsonTarget.Kind() != reflect.Ptr || jsonTarget.IsNil() {
return fmt.Errorf("provided object is not a valid pointer")
}

jsonBytes, err := yamlToJSONTarget(yamlBytes, &jsonTarget, unmarshalFn)
if err != nil {
return nil, err
return fmt.Errorf("error converting YAML to JSON: %w", err)
}

// YAML objects are not completely compatible with JSON objects (e.g. you
// can have non-string keys in YAML). So, convert the YAML-compatible object
// to a JSON-compatible object, failing with an error if irrecoverable
// incompatibilties happen along the way.
jsonObj, err := convertToJSONableObject(yamlObj, jsonTarget)
err = jsonUnmarshal(bytes.NewReader(jsonBytes), obj, opts...)
if err != nil {
return nil, err
return fmt.Errorf("error unmarshaling JSON: %w", err)
}

// Convert this object to JSON and return the data.
return json.Marshal(jsonObj)
return nil
}

// Unmarshal converts YAML to JSON then uses JSON to unmarshal into an object,
// optionally configuring the behavior of the JSON unmarshal.
func Unmarshal(yamlBytes []byte, obj interface{}, opts ...JSONOpt) error {
return unmarshal(yamlBytes, obj, yaml.Unmarshal, opts...)
}

// UnmarshalStrict strictly converts YAML to JSON then uses JSON to unmarshal
// into an object, optionally configuring the behavior of the JSON unmarshal.
func UnmarshalStrict(yamlBytes []byte, obj interface{}, opts ...JSONOpt) error {
return unmarshal(yamlBytes, obj, yaml.UnmarshalStrict, append(opts, DisallowUnknownFields)...)
}

func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (interface{}, error) {
Expand All @@ -147,13 +117,13 @@ func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (in
// decoding into the value, we're just checking if the ultimate target is a
// string.
if jsonTarget != nil {
ju, tu, pv := indirect(*jsonTarget, false)
jsonUnmarshaler, textUnmarshaler, pointerValue := indirect(*jsonTarget, false)
// We have a JSON or Text Umarshaler at this level, so we can't be trying
// to decode into a string.
if ju != nil || tu != nil {
if jsonUnmarshaler != nil || textUnmarshaler != nil {
jsonTarget = nil
} else {
jsonTarget = &pv
jsonTarget = &pointerValue
}
}

Expand Down Expand Up @@ -205,7 +175,7 @@ func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (in
keyString = "false"
}
default:
return nil, fmt.Errorf("Unsupported map key of type: %s, key: %+#v, value: %+#v",
return nil, fmt.Errorf("unsupported map key of type: %s, key: %+#v, value: %+#v",
reflect.TypeOf(k), k, v)
}

Expand Down

0 comments on commit 23f10d0

Please sign in to comment.