Add RNode.Map method and test to help decoding.

This commit is contained in:
monopole
2021-01-09 06:57:01 -08:00
parent 6f2f401f6b
commit 1a002005c1
3 changed files with 47 additions and 0 deletions

View File

@@ -785,6 +785,19 @@ func FromMap(m map[string]interface{}) (*RNode, error) {
return Parse(string(c))
}
func (rn *RNode) Map() map[string]interface{} {
if rn == nil || rn.value == nil {
return make(map[string]interface{})
}
var result map[string]interface{}
if err := rn.value.Decode(&result); err != nil {
// Should not be able to create an RNode that cannot be decoded;
// this is an unrecoverable error.
log.Fatalf("failed to decode ynode: %v", err)
}
return result
}
// ConvertJSONToYamlNode parses input json string and returns equivalent yaml node
func ConvertJSONToYamlNode(jsonStr string) (*RNode, error) {
var body map[string]interface{}