mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Merge branch 'bugfix/nonstring-keys' of git://github.com/kenjones-cisco/yaml into kenjones-cisco-bugfix/nonstring-keys
This commit is contained in:
commit
79baa49eaa
@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strconv"
|
||||||
yaml "gopkg.in/yaml.v2"
|
yaml "gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -27,7 +27,13 @@ func toJSON(context interface{}) interface{} {
|
|||||||
oldMap := context.(yaml.MapSlice)
|
oldMap := context.(yaml.MapSlice)
|
||||||
newMap := make(map[string]interface{})
|
newMap := make(map[string]interface{})
|
||||||
for _, entry := range oldMap {
|
for _, entry := range oldMap {
|
||||||
newMap[entry.Key.(string)] = toJSON(entry.Value)
|
if str, ok := entry.Key.(string); ok {
|
||||||
|
newMap[str] = toJSON(entry.Value)
|
||||||
|
} else if i, ok := entry.Key.(int); ok {
|
||||||
|
newMap[strconv.Itoa(i)] = toJSON(entry.Value)
|
||||||
|
} else if b, ok := entry.Key.(bool); ok {
|
||||||
|
newMap[strconv.FormatBool(b)] = toJSON(entry.Value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return newMap
|
return newMap
|
||||||
default:
|
default:
|
||||||
|
@ -13,6 +13,24 @@ b:
|
|||||||
assertResult(t, "{\"b\":{\"c\":2}}", jsonToString(data))
|
assertResult(t, "{\"b\":{\"c\":2}}", jsonToString(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestJsonToString_withIntKey(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
|
---
|
||||||
|
b:
|
||||||
|
2: c
|
||||||
|
`)
|
||||||
|
assertResult(t, `{"b":{"2":"c"}}`, jsonToString(data))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJsonToString_withBoolKey(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
|
---
|
||||||
|
b:
|
||||||
|
false: c
|
||||||
|
`)
|
||||||
|
assertResult(t, `{"b":{"false":"c"}}`, jsonToString(data))
|
||||||
|
}
|
||||||
|
|
||||||
func TestJsonToString_withArray(t *testing.T) {
|
func TestJsonToString_withArray(t *testing.T) {
|
||||||
var data = parseData(`
|
var data = parseData(`
|
||||||
---
|
---
|
||||||
|
Loading…
Reference in New Issue
Block a user