diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2020-05-08 12:59:42 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-08 12:59:42 +0200 |
commit | 4b300a12cedabff3c57ee657afac0696cefaae0a (patch) | |
tree | 61c5fceec50e88338038415fa1375371bef2dca4 /vendor/sigs.k8s.io/yaml/yaml.go | |
parent | 70e7fc670be13d3877e65e160456c312e23a5806 (diff) | |
parent | 2f0bc5ff1cde9afb595868b92dd123478af9ef74 (diff) | |
download | podman-4b300a12cedabff3c57ee657afac0696cefaae0a.tar.gz podman-4b300a12cedabff3c57ee657afac0696cefaae0a.tar.bz2 podman-4b300a12cedabff3c57ee657afac0696cefaae0a.zip |
Merge pull request #6086 from containers/dependabot/go_modules/k8s.io/api-0.18.2
Bump k8s.io/api from 0.17.4 to 0.18.2
Diffstat (limited to 'vendor/sigs.k8s.io/yaml/yaml.go')
-rw-r--r-- | vendor/sigs.k8s.io/yaml/yaml.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/vendor/sigs.k8s.io/yaml/yaml.go b/vendor/sigs.k8s.io/yaml/yaml.go index 024596112..efbc535d4 100644 --- a/vendor/sigs.k8s.io/yaml/yaml.go +++ b/vendor/sigs.k8s.io/yaml/yaml.go @@ -317,3 +317,64 @@ func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (in 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 j +} |