Blame vendor/github.com/ghodss/yaml/yaml.go

Packit Service 3a6627
package yaml
Packit Service 3a6627
Packit Service 3a6627
import (
Packit Service 3a6627
	"bytes"
Packit Service 3a6627
	"encoding/json"
Packit Service 3a6627
	"fmt"
Packit Service 3a6627
	"reflect"
Packit Service 3a6627
	"strconv"
Packit Service 3a6627
Packit Service 3a6627
	"gopkg.in/yaml.v2"
Packit Service 3a6627
)
Packit Service 3a6627
Packit Service 3a6627
// Marshals the object into JSON then converts JSON to YAML and returns the
Packit Service 3a6627
// YAML.
Packit Service 3a6627
func Marshal(o interface{}) ([]byte, error) {
Packit Service 3a6627
	j, err := json.Marshal(o)
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		return nil, fmt.Errorf("error marshaling into JSON: %v", err)
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	y, err := JSONToYAML(j)
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		return nil, fmt.Errorf("error converting JSON to YAML: %v", err)
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	return y, nil
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
// Converts YAML to JSON then uses JSON to unmarshal into an object.
Packit Service 3a6627
func Unmarshal(y []byte, o interface{}) error {
Packit Service 3a6627
	vo := reflect.ValueOf(o)
Packit Service 3a6627
	j, err := yamlToJSON(y, &vo)
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		return fmt.Errorf("error converting YAML to JSON: %v", err)
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	err = json.Unmarshal(j, o)
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		return fmt.Errorf("error unmarshaling JSON: %v", err)
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	return nil
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
// Convert JSON to YAML.
Packit Service 3a6627
func JSONToYAML(j []byte) ([]byte, error) {
Packit Service 3a6627
	// Convert the JSON to an object.
Packit Service 3a6627
	var jsonObj interface{}
Packit Service 3a6627
	// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
Packit Service 3a6627
	// Go JSON library doesn't try to pick the right number type (int, float,
Packit Service 3a6627
	// etc.) when unmarshalling to interface{}, it just picks float64
Packit Service 3a6627
	// universally. go-yaml does go through the effort of picking the right
Packit Service 3a6627
	// number type, so we can preserve number type throughout this process.
Packit Service 3a6627
	err := yaml.Unmarshal(j, &jsonObj)
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		return nil, err
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	// Marshal this object into YAML.
Packit Service 3a6627
	return yaml.Marshal(jsonObj)
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
// Convert YAML to JSON. Since JSON is a subset of YAML, passing JSON through
Packit Service 3a6627
// this method should be a no-op.
Packit Service 3a6627
//
Packit Service 3a6627
// Things YAML can do that are not supported by JSON:
Packit Service 3a6627
// * In YAML you can have binary and null keys in your maps. These are invalid
Packit Service 3a6627
//   in JSON. (int and float keys are converted to strings.)
Packit Service 3a6627
// * Binary data in YAML with the !!binary tag is not supported. If you want to
Packit Service 3a6627
//   use binary data with this library, encode the data as base64 as usual but do
Packit Service 3a6627
//   not use the !!binary tag in your YAML. This will ensure the original base64
Packit Service 3a6627
//   encoded data makes it all the way through to the JSON.
Packit Service 3a6627
func YAMLToJSON(y []byte) ([]byte, error) {
Packit Service 3a6627
	return yamlToJSON(y, nil)
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
func yamlToJSON(y []byte, jsonTarget *reflect.Value) ([]byte, error) {
Packit Service 3a6627
	// Convert the YAML to an object.
Packit Service 3a6627
	var yamlObj interface{}
Packit Service 3a6627
	err := yaml.Unmarshal(y, &yamlObj)
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		return nil, err
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	// YAML objects are not completely compatible with JSON objects (e.g. you
Packit Service 3a6627
	// can have non-string keys in YAML). So, convert the YAML-compatible object
Packit Service 3a6627
	// to a JSON-compatible object, failing with an error if irrecoverable
Packit Service 3a6627
	// incompatibilties happen along the way.
Packit Service 3a6627
	jsonObj, err := convertToJSONableObject(yamlObj, jsonTarget)
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		return nil, err
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	// Convert this object to JSON and return the data.
Packit Service 3a6627
	return json.Marshal(jsonObj)
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (interface{}, error) {
Packit Service 3a6627
	var err error
Packit Service 3a6627
Packit Service 3a6627
	// Resolve jsonTarget to a concrete value (i.e. not a pointer or an
Packit Service 3a6627
	// interface). We pass decodingNull as false because we're not actually
Packit Service 3a6627
	// decoding into the value, we're just checking if the ultimate target is a
Packit Service 3a6627
	// string.
Packit Service 3a6627
	if jsonTarget != nil {
Packit Service 3a6627
		ju, tu, pv := indirect(*jsonTarget, false)
Packit Service 3a6627
		// We have a JSON or Text Umarshaler at this level, so we can't be trying
Packit Service 3a6627
		// to decode into a string.
Packit Service 3a6627
		if ju != nil || tu != nil {
Packit Service 3a6627
			jsonTarget = nil
Packit Service 3a6627
		} else {
Packit Service 3a6627
			jsonTarget = &pv
Packit Service 3a6627
		}
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	// If yamlObj is a number or a boolean, check if jsonTarget is a string -
Packit Service 3a6627
	// if so, coerce.  Else return normal.
Packit Service 3a6627
	// If yamlObj is a map or array, find the field that each key is
Packit Service 3a6627
	// unmarshaling to, and when you recurse pass the reflect.Value for that
Packit Service 3a6627
	// field back into this function.
Packit Service 3a6627
	switch typedYAMLObj := yamlObj.(type) {
Packit Service 3a6627
	case map[interface{}]interface{}:
Packit Service 3a6627
		// JSON does not support arbitrary keys in a map, so we must convert
Packit Service 3a6627
		// these keys to strings.
Packit Service 3a6627
		//
Packit Service 3a6627
		// From my reading of go-yaml v2 (specifically the resolve function),
Packit Service 3a6627
		// keys can only have the types string, int, int64, float64, binary
Packit Service 3a6627
		// (unsupported), or null (unsupported).
Packit Service 3a6627
		strMap := make(map[string]interface{})
Packit Service 3a6627
		for k, v := range typedYAMLObj {
Packit Service 3a6627
			// Resolve the key to a string first.
Packit Service 3a6627
			var keyString string
Packit Service 3a6627
			switch typedKey := k.(type) {
Packit Service 3a6627
			case string:
Packit Service 3a6627
				keyString = typedKey
Packit Service 3a6627
			case int:
Packit Service 3a6627
				keyString = strconv.Itoa(typedKey)
Packit Service 3a6627
			case int64:
Packit Service 3a6627
				// go-yaml will only return an int64 as a key if the system
Packit Service 3a6627
				// architecture is 32-bit and the key's value is between 32-bit
Packit Service 3a6627
				// and 64-bit. Otherwise the key type will simply be int.
Packit Service 3a6627
				keyString = strconv.FormatInt(typedKey, 10)
Packit Service 3a6627
			case float64:
Packit Service 3a6627
				// Stolen from go-yaml to use the same conversion to string as
Packit Service 3a6627
				// the go-yaml library uses to convert float to string when
Packit Service 3a6627
				// Marshaling.
Packit Service 3a6627
				s := strconv.FormatFloat(typedKey, 'g', -1, 32)
Packit Service 3a6627
				switch s {
Packit Service 3a6627
				case "+Inf":
Packit Service 3a6627
					s = ".inf"
Packit Service 3a6627
				case "-Inf":
Packit Service 3a6627
					s = "-.inf"
Packit Service 3a6627
				case "NaN":
Packit Service 3a6627
					s = ".nan"
Packit Service 3a6627
				}
Packit Service 3a6627
				keyString = s
Packit Service 3a6627
			case bool:
Packit Service 3a6627
				if typedKey {
Packit Service 3a6627
					keyString = "true"
Packit Service 3a6627
				} else {
Packit Service 3a6627
					keyString = "false"
Packit Service 3a6627
				}
Packit Service 3a6627
			default:
Packit Service 3a6627
				return nil, fmt.Errorf("Unsupported map key of type: %s, key: %+#v, value: %+#v",
Packit Service 3a6627
					reflect.TypeOf(k), k, v)
Packit Service 3a6627
			}
Packit Service 3a6627
Packit Service 3a6627
			// jsonTarget should be a struct or a map. If it's a struct, find
Packit Service 3a6627
			// the field it's going to map to and pass its reflect.Value. If
Packit Service 3a6627
			// it's a map, find the element type of the map and pass the
Packit Service 3a6627
			// reflect.Value created from that type. If it's neither, just pass
Packit Service 3a6627
			// nil - JSON conversion will error for us if it's a real issue.
Packit Service 3a6627
			if jsonTarget != nil {
Packit Service 3a6627
				t := *jsonTarget
Packit Service 3a6627
				if t.Kind() == reflect.Struct {
Packit Service 3a6627
					keyBytes := []byte(keyString)
Packit Service 3a6627
					// Find the field that the JSON library would use.
Packit Service 3a6627
					var f *field
Packit Service 3a6627
					fields := cachedTypeFields(t.Type())
Packit Service 3a6627
					for i := range fields {
Packit Service 3a6627
						ff := &fields[i]
Packit Service 3a6627
						if bytes.Equal(ff.nameBytes, keyBytes) {
Packit Service 3a6627
							f = ff
Packit Service 3a6627
							break
Packit Service 3a6627
						}
Packit Service 3a6627
						// Do case-insensitive comparison.
Packit Service 3a6627
						if f == nil && ff.equalFold(ff.nameBytes, keyBytes) {
Packit Service 3a6627
							f = ff
Packit Service 3a6627
						}
Packit Service 3a6627
					}
Packit Service 3a6627
					if f != nil {
Packit Service 3a6627
						// Find the reflect.Value of the most preferential
Packit Service 3a6627
						// struct field.
Packit Service 3a6627
						jtf := t.Field(f.index[0])
Packit Service 3a6627
						strMap[keyString], err = convertToJSONableObject(v, &jtf)
Packit Service 3a6627
						if err != nil {
Packit Service 3a6627
							return nil, err
Packit Service 3a6627
						}
Packit Service 3a6627
						continue
Packit Service 3a6627
					}
Packit Service 3a6627
				} else if t.Kind() == reflect.Map {
Packit Service 3a6627
					// Create a zero value of the map's element type to use as
Packit Service 3a6627
					// the JSON target.
Packit Service 3a6627
					jtv := reflect.Zero(t.Type().Elem())
Packit Service 3a6627
					strMap[keyString], err = convertToJSONableObject(v, &jtv)
Packit Service 3a6627
					if err != nil {
Packit Service 3a6627
						return nil, err
Packit Service 3a6627
					}
Packit Service 3a6627
					continue
Packit Service 3a6627
				}
Packit Service 3a6627
			}
Packit Service 3a6627
			strMap[keyString], err = convertToJSONableObject(v, nil)
Packit Service 3a6627
			if err != nil {
Packit Service 3a6627
				return nil, err
Packit Service 3a6627
			}
Packit Service 3a6627
		}
Packit Service 3a6627
		return strMap, nil
Packit Service 3a6627
	case []interface{}:
Packit Service 3a6627
		// We need to recurse into arrays in case there are any
Packit Service 3a6627
		// map[interface{}]interface{}'s inside and to convert any
Packit Service 3a6627
		// numbers to strings.
Packit Service 3a6627
Packit Service 3a6627
		// If jsonTarget is a slice (which it really should be), find the
Packit Service 3a6627
		// thing it's going to map to. If it's not a slice, just pass nil
Packit Service 3a6627
		// - JSON conversion will error for us if it's a real issue.
Packit Service 3a6627
		var jsonSliceElemValue *reflect.Value
Packit Service 3a6627
		if jsonTarget != nil {
Packit Service 3a6627
			t := *jsonTarget
Packit Service 3a6627
			if t.Kind() == reflect.Slice {
Packit Service 3a6627
				// By default slices point to nil, but we need a reflect.Value
Packit Service 3a6627
				// pointing to a value of the slice type, so we create one here.
Packit Service 3a6627
				ev := reflect.Indirect(reflect.New(t.Type().Elem()))
Packit Service 3a6627
				jsonSliceElemValue = &ev
Packit Service 3a6627
			}
Packit Service 3a6627
		}
Packit Service 3a6627
Packit Service 3a6627
		// Make and use a new array.
Packit Service 3a6627
		arr := make([]interface{}, len(typedYAMLObj))
Packit Service 3a6627
		for i, v := range typedYAMLObj {
Packit Service 3a6627
			arr[i], err = convertToJSONableObject(v, jsonSliceElemValue)
Packit Service 3a6627
			if err != nil {
Packit Service 3a6627
				return nil, err
Packit Service 3a6627
			}
Packit Service 3a6627
		}
Packit Service 3a6627
		return arr, nil
Packit Service 3a6627
	default:
Packit Service 3a6627
		// If the target type is a string and the YAML type is a number,
Packit Service 3a6627
		// convert the YAML type to a string.
Packit Service 3a6627
		if jsonTarget != nil && (*jsonTarget).Kind() == reflect.String {
Packit Service 3a6627
			// Based on my reading of go-yaml, it may return int, int64,
Packit Service 3a6627
			// float64, or uint64.
Packit Service 3a6627
			var s string
Packit Service 3a6627
			switch typedVal := typedYAMLObj.(type) {
Packit Service 3a6627
			case int:
Packit Service 3a6627
				s = strconv.FormatInt(int64(typedVal), 10)
Packit Service 3a6627
			case int64:
Packit Service 3a6627
				s = strconv.FormatInt(typedVal, 10)
Packit Service 3a6627
			case float64:
Packit Service 3a6627
				s = strconv.FormatFloat(typedVal, 'g', -1, 32)
Packit Service 3a6627
			case uint64:
Packit Service 3a6627
				s = strconv.FormatUint(typedVal, 10)
Packit Service 3a6627
			case bool:
Packit Service 3a6627
				if typedVal {
Packit Service 3a6627
					s = "true"
Packit Service 3a6627
				} else {
Packit Service 3a6627
					s = "false"
Packit Service 3a6627
				}
Packit Service 3a6627
			}
Packit Service 3a6627
			if len(s) > 0 {
Packit Service 3a6627
				yamlObj = interface{}(s)
Packit Service 3a6627
			}
Packit Service 3a6627
		}
Packit Service 3a6627
		return yamlObj, nil
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	return nil, nil
Packit Service 3a6627
}