mirror of https://github.com/helm/helm
This code removes dependency on sigs.k8s.io/yaml which itself is a mirror of github.com/ghodss/yaml. All but one usages of ghodss/yaml were no-ops. That is they passed directly to go-yaml v2 (gopkg.in/yaml.v2). Upgradring them to use gopkg.in/yaml.v3 was trivial. The one non-trivial use of ghodss/yaml was of the YAMLToJSON function. I have copied a minimal tree of dependency functions to chartutil/yaml to suppose just that function hosted with helm. Upgrading to yaml.v3 supports retaining comments on round-trip serialisation which is blocking the ability for helm package to support the --set flag see https://github.com/helm/helm/issues/3141. This commit pulls in the code and is pure refactoring (the next commit fixes some minor differences in the test cases between go-yaml v2 and v3), so the functionality from ghodss/yaml should be unchanged Signed-off-by: Silas Davis <silas@monax.io>pull/7963/head
parent
d0726e07ab
commit
f610f74218
@ -0,0 +1,21 @@
|
|||||||
|
package yaml
|
||||||
|
|
||||||
|
import "gopkg.in/yaml.v2"
|
||||||
|
|
||||||
|
// Convert JSON to YAML.
|
||||||
|
func FromJSON(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, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marshal this object into YAML.
|
||||||
|
return yaml.Marshal(jsonObj)
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package yaml
|
||||||
|
|
||||||
|
import "gopkg.in/yaml.v3"
|
||||||
|
|
||||||
|
var Marshal = yaml.Marshal
|
@ -0,0 +1,213 @@
|
|||||||
|
package yaml
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ToJSON(y []byte) ([]byte, error) {
|
||||||
|
// Convert the YAML to an object.
|
||||||
|
var yamlObj interface{}
|
||||||
|
err := yaml.Unmarshal(y, &yamlObj)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 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
|
||||||
|
// incompatibilities happen along the way.
|
||||||
|
jsonObj, err := convertToJSONableObject(yamlObj, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert this object to JSON and return the data.
|
||||||
|
return json.Marshal(jsonObj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (interface{}, error) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// Resolve jsonTarget to a concrete value (i.e. not a pointer or an
|
||||||
|
// interface). We pass decodingNull as false because we're not actually
|
||||||
|
// decoding into the value, we're just checking if the ultimate target is a
|
||||||
|
// string.
|
||||||
|
if jsonTarget != nil {
|
||||||
|
ju, tu, pv := 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 {
|
||||||
|
jsonTarget = nil
|
||||||
|
} else {
|
||||||
|
jsonTarget = &pv
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If yamlObj is a number or a boolean, check if jsonTarget is a string -
|
||||||
|
// if so, coerce. Else return normal.
|
||||||
|
// 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.
|
||||||
|
switch typedYAMLObj := yamlObj.(type) {
|
||||||
|
case map[interface{}]interface{}:
|
||||||
|
// JSON does not support arbitrary keys in a map, so we must convert
|
||||||
|
// these keys to strings.
|
||||||
|
//
|
||||||
|
// From my reading of go-yaml v2 (specifically the resolve function),
|
||||||
|
// keys can only have the types string, int, int64, float64, binary
|
||||||
|
// (unsupported), or null (unsupported).
|
||||||
|
strMap := make(map[string]interface{})
|
||||||
|
for k, v := range typedYAMLObj {
|
||||||
|
// Resolve the key to a string first.
|
||||||
|
var keyString string
|
||||||
|
switch typedKey := k.(type) {
|
||||||
|
case string:
|
||||||
|
keyString = typedKey
|
||||||
|
case int:
|
||||||
|
keyString = strconv.Itoa(typedKey)
|
||||||
|
case int64:
|
||||||
|
// go-yaml will only return an int64 as a key if the system
|
||||||
|
// architecture is 32-bit and the key's value is between 32-bit
|
||||||
|
// and 64-bit. Otherwise the key type will simply be int.
|
||||||
|
keyString = strconv.FormatInt(typedKey, 10)
|
||||||
|
case float64:
|
||||||
|
// Stolen from go-yaml to use the same conversion to string as
|
||||||
|
// the go-yaml library uses to convert float to string when
|
||||||
|
// Marshaling.
|
||||||
|
s := strconv.FormatFloat(typedKey, 'g', -1, 32)
|
||||||
|
switch s {
|
||||||
|
case "+Inf":
|
||||||
|
s = ".inf"
|
||||||
|
case "-Inf":
|
||||||
|
s = "-.inf"
|
||||||
|
case "NaN":
|
||||||
|
s = ".nan"
|
||||||
|
}
|
||||||
|
keyString = s
|
||||||
|
case bool:
|
||||||
|
if typedKey {
|
||||||
|
keyString = "true"
|
||||||
|
} else {
|
||||||
|
keyString = "false"
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("Unsupported map key of type: %s, key: %+#v, value: %+#v",
|
||||||
|
reflect.TypeOf(k), k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// jsonTarget should be a struct or a map. If it's a struct, find
|
||||||
|
// the field it's going to map to and pass its reflect.Value. If
|
||||||
|
// it's a map, find the element type of the map and pass the
|
||||||
|
// reflect.Value created from that type. If it's neither, just pass
|
||||||
|
// nil - JSON conversion will error for us if it's a real issue.
|
||||||
|
if jsonTarget != nil {
|
||||||
|
t := *jsonTarget
|
||||||
|
if t.Kind() == reflect.Struct {
|
||||||
|
keyBytes := []byte(keyString)
|
||||||
|
// Find the field that the JSON library would use.
|
||||||
|
var f *field
|
||||||
|
fields := cachedTypeFields(t.Type())
|
||||||
|
for i := range fields {
|
||||||
|
ff := &fields[i]
|
||||||
|
if bytes.Equal(ff.nameBytes, keyBytes) {
|
||||||
|
f = ff
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// Do case-insensitive comparison.
|
||||||
|
if f == nil && ff.equalFold(ff.nameBytes, keyBytes) {
|
||||||
|
f = ff
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if f != nil {
|
||||||
|
// Find the reflect.Value of the most preferential
|
||||||
|
// struct field.
|
||||||
|
jtf := t.Field(f.index[0])
|
||||||
|
strMap[keyString], err = convertToJSONableObject(v, &jtf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
} else if t.Kind() == reflect.Map {
|
||||||
|
// Create a zero value of the map's element type to use as
|
||||||
|
// the JSON target.
|
||||||
|
jtv := reflect.Zero(t.Type().Elem())
|
||||||
|
strMap[keyString], err = convertToJSONableObject(v, &jtv)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
strMap[keyString], err = convertToJSONableObject(v, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return strMap, nil
|
||||||
|
case []interface{}:
|
||||||
|
// We need to recurse into arrays in case there are any
|
||||||
|
// map[interface{}]interface{}'s inside and to convert any
|
||||||
|
// numbers to strings.
|
||||||
|
|
||||||
|
// If jsonTarget is a slice (which it really should be), find the
|
||||||
|
// thing it's going to map to. If it's not a slice, just pass nil
|
||||||
|
// - JSON conversion will error for us if it's a real issue.
|
||||||
|
var jsonSliceElemValue *reflect.Value
|
||||||
|
if jsonTarget != nil {
|
||||||
|
t := *jsonTarget
|
||||||
|
if t.Kind() == reflect.Slice {
|
||||||
|
// By default slices point to nil, but we need a reflect.Value
|
||||||
|
// pointing to a value of the slice type, so we create one here.
|
||||||
|
ev := reflect.Indirect(reflect.New(t.Type().Elem()))
|
||||||
|
jsonSliceElemValue = &ev
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make and use a new array.
|
||||||
|
arr := make([]interface{}, len(typedYAMLObj))
|
||||||
|
for i, v := range typedYAMLObj {
|
||||||
|
arr[i], err = convertToJSONableObject(v, jsonSliceElemValue)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return arr, nil
|
||||||
|
default:
|
||||||
|
// If the target type is a string and the YAML type is a number,
|
||||||
|
// convert the YAML type to a string.
|
||||||
|
if jsonTarget != nil && (*jsonTarget).Kind() == reflect.String {
|
||||||
|
// Based on my reading of go-yaml, it may return int, int64,
|
||||||
|
// float64, or uint64.
|
||||||
|
var s string
|
||||||
|
switch typedVal := typedYAMLObj.(type) {
|
||||||
|
case int:
|
||||||
|
s = strconv.FormatInt(int64(typedVal), 10)
|
||||||
|
case int64:
|
||||||
|
s = strconv.FormatInt(typedVal, 10)
|
||||||
|
case float64:
|
||||||
|
s = strconv.FormatFloat(typedVal, 'g', -1, 32)
|
||||||
|
case uint64:
|
||||||
|
s = strconv.FormatUint(typedVal, 10)
|
||||||
|
case bool:
|
||||||
|
if typedVal {
|
||||||
|
s = "true"
|
||||||
|
} else {
|
||||||
|
s = "false"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(s) > 0 {
|
||||||
|
yamlObj = interface{}(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return yamlObj, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package yaml
|
||||||
|
|
||||||
|
// This code was extracted from https://github.com/ghodss/yaml to provide the ToJSON (nee YAMLToJSON) functionality
|
||||||
|
// (being the only functionality needed above gopkg.in/yaml.v3. The reason for extraction was to upgrade to yaml.v3
|
||||||
|
// but also to minimise dependency footprint.
|
@ -0,0 +1,172 @@
|
|||||||
|
package yaml
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Case struct {
|
||||||
|
input string
|
||||||
|
output 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.
|
||||||
|
reverse *string
|
||||||
|
}
|
||||||
|
|
||||||
|
type RunType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
RunTypeJSONToYAML RunType = iota
|
||||||
|
RunTypeYAMLToJSON
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestJSONToYAML(t *testing.T) {
|
||||||
|
cases := []Case{
|
||||||
|
{
|
||||||
|
`{"t":"a"}`,
|
||||||
|
"t: a\n",
|
||||||
|
nil,
|
||||||
|
}, {
|
||||||
|
`{"t":null}`,
|
||||||
|
"t: null\n",
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
runCases(t, RunTypeJSONToYAML, cases)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestYAMLToJSON(t *testing.T) {
|
||||||
|
cases := []Case{
|
||||||
|
{
|
||||||
|
"t: a\n",
|
||||||
|
`{"t":"a"}`,
|
||||||
|
nil,
|
||||||
|
}, {
|
||||||
|
"t: \n",
|
||||||
|
`{"t":null}`,
|
||||||
|
strPtr("t: null\n"),
|
||||||
|
}, {
|
||||||
|
"t: null\n",
|
||||||
|
`{"t":null}`,
|
||||||
|
nil,
|
||||||
|
}, {
|
||||||
|
"1: a\n",
|
||||||
|
`{"1":"a"}`,
|
||||||
|
strPtr("\"1\": a\n"),
|
||||||
|
}, {
|
||||||
|
"1000000000000000000000000000000000000: a\n",
|
||||||
|
`{"1e+36":"a"}`,
|
||||||
|
strPtr("\"1e+36\": a\n"),
|
||||||
|
}, {
|
||||||
|
"1e+36: a\n",
|
||||||
|
`{"1e+36":"a"}`,
|
||||||
|
strPtr("\"1e+36\": a\n"),
|
||||||
|
}, {
|
||||||
|
"\"1e+36\": a\n",
|
||||||
|
`{"1e+36":"a"}`,
|
||||||
|
nil,
|
||||||
|
}, {
|
||||||
|
"\"1.2\": a\n",
|
||||||
|
`{"1.2":"a"}`,
|
||||||
|
nil,
|
||||||
|
}, {
|
||||||
|
"- t: a\n",
|
||||||
|
`[{"t":"a"}]`,
|
||||||
|
nil,
|
||||||
|
}, {
|
||||||
|
"- t: a\n" +
|
||||||
|
"- t:\n" +
|
||||||
|
" b: 1\n" +
|
||||||
|
" c: 2\n",
|
||||||
|
`[{"t":"a"},{"t":{"b":1,"c":2}}]`,
|
||||||
|
nil,
|
||||||
|
}, {
|
||||||
|
`[{t: a}, {t: {b: 1, c: 2}}]`,
|
||||||
|
`[{"t":"a"},{"t":{"b":1,"c":2}}]`,
|
||||||
|
strPtr("- t: a\n" +
|
||||||
|
"- t:\n" +
|
||||||
|
" b: 1\n" +
|
||||||
|
" c: 2\n"),
|
||||||
|
}, {
|
||||||
|
"- t: \n",
|
||||||
|
`[{"t":null}]`,
|
||||||
|
strPtr("- t: null\n"),
|
||||||
|
}, {
|
||||||
|
"- t: null\n",
|
||||||
|
`[{"t":null}]`,
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cases that should produce errors.
|
||||||
|
_ = []Case{
|
||||||
|
{
|
||||||
|
"~: a",
|
||||||
|
`{"null":"a"}`,
|
||||||
|
nil,
|
||||||
|
}, {
|
||||||
|
"a: !!binary gIGC\n",
|
||||||
|
"{\"a\":\"\x80\x81\x82\"}",
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
runCases(t, RunTypeYAMLToJSON, cases)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runCases(t *testing.T, runType RunType, cases []Case) {
|
||||||
|
var f func([]byte) ([]byte, error)
|
||||||
|
var invF func([]byte) ([]byte, error)
|
||||||
|
var msg string
|
||||||
|
var invMsg string
|
||||||
|
if runType == RunTypeJSONToYAML {
|
||||||
|
f = FromJSON
|
||||||
|
invF = ToJSON
|
||||||
|
msg = "JSON to YAML"
|
||||||
|
invMsg = "YAML back to JSON"
|
||||||
|
} else {
|
||||||
|
f = ToJSON
|
||||||
|
invF = FromJSON
|
||||||
|
msg = "YAML to JSON"
|
||||||
|
invMsg = "JSON back to YAML"
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
// Convert the string.
|
||||||
|
t.Logf("converting %s\n", c.input)
|
||||||
|
output, err := f([]byte(c.input))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Failed to convert %s, input: `%s`, err: %v", msg, c.input, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check it against the expected output.
|
||||||
|
if string(output) != c.output {
|
||||||
|
t.Errorf("Failed to convert %s, input: `%s`, expected `%s`, got `%s`",
|
||||||
|
msg, c.input, c.output, string(output))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the string that we will compare the reversed output to.
|
||||||
|
reverse := c.input
|
||||||
|
// If a special reverse string was specified, use that instead.
|
||||||
|
if c.reverse != nil {
|
||||||
|
reverse = *c.reverse
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reverse the output.
|
||||||
|
input, err := invF(output)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Failed to convert %s, input: `%s`, err: %v", invMsg, string(output), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the reverse is equal to the input (or to *c.reverse).
|
||||||
|
if string(input) != reverse {
|
||||||
|
t.Errorf("Failed to convert %s, input: `%s`, expected `%s`, got `%s`",
|
||||||
|
invMsg, string(output), reverse, string(input))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// To be able to easily fill in the *Case.reverse string above.
|
||||||
|
func strPtr(s string) *string {
|
||||||
|
return &s
|
||||||
|
}
|
Loading…
Reference in new issue