mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-13 22:38:04 +00:00
86639acf70
The base directory has all shell scripts in scripts/ and all example/test files in examples/. A Makefile provides all the commands with helpful information. If a developer simply types `make` then vendor is properly updated, the code is formatted, linted, tested, built, acceptance test run, and installed. Linting errors resolved. Ignored test case (`TestParsePath`) updated to work as expected.
37 lines
758 B
Go
37 lines
758 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
)
|
|
|
|
func jsonToString(context interface{}) string {
|
|
out, err := json.Marshal(toJSON(context))
|
|
if err != nil {
|
|
die("error printing yaml as json: ", err)
|
|
}
|
|
return string(out)
|
|
}
|
|
|
|
func toJSON(context interface{}) interface{} {
|
|
switch context.(type) {
|
|
case []interface{}:
|
|
oldArray := context.([]interface{})
|
|
newArray := make([]interface{}, len(oldArray))
|
|
for index, value := range oldArray {
|
|
newArray[index] = toJSON(value)
|
|
}
|
|
return newArray
|
|
case yaml.MapSlice:
|
|
oldMap := context.(yaml.MapSlice)
|
|
newMap := make(map[string]interface{})
|
|
for _, entry := range oldMap {
|
|
newMap[entry.Key.(string)] = toJSON(entry.Value)
|
|
}
|
|
return newMap
|
|
default:
|
|
return context
|
|
}
|
|
}
|