summaryrefslogtreecommitdiff
path: root/vendor/sigs.k8s.io/yaml/yaml.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/sigs.k8s.io/yaml/yaml.go')
-rw-r--r--vendor/sigs.k8s.io/yaml/yaml.go61
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
+}