Add RNode fromMap method.

This commit is contained in:
jregan
2020-11-14 08:35:18 -08:00
parent 22f41c789a
commit a4f22cb84f
31 changed files with 179 additions and 37 deletions

View File

@@ -318,8 +318,7 @@ func (rn *RNode) PipeE(functions ...Filter) error {
return errors.Wrap(err)
}
// Document returns the Node RNode for the value. Does not unwrap the node if it is a
// DocumentNodes
// Document returns the Node for the value.
func (rn *RNode) Document() *yaml.Node {
return rn.value
}
@@ -565,6 +564,7 @@ func (rn *RNode) GetAssociativeKey() string {
return ""
}
// MarshalJSON creates a byte slice from the RNode.
func (rn *RNode) MarshalJSON() ([]byte, error) {
s, err := rn.String()
if err != nil {
@@ -586,18 +586,13 @@ func (rn *RNode) MarshalJSON() ([]byte, error) {
return json.Marshal(m)
}
// UnmarshalJSON overwrites this RNode with data from []byte.
func (rn *RNode) UnmarshalJSON(b []byte) error {
m := map[string]interface{}{}
if err := json.Unmarshal(b, &m); err != nil {
return err
}
c, err := Marshal(m)
if err != nil {
return err
}
r, err := Parse(string(c))
r, err := FromMap(m)
if err != nil {
return err
}
@@ -605,6 +600,14 @@ func (rn *RNode) UnmarshalJSON(b []byte) error {
return nil
}
func FromMap(m map[string]interface{}) (*RNode, error) {
c, err := Marshal(m)
if err != nil {
return nil, err
}
return Parse(string(c))
}
// ConvertJSONToYamlNode parses input json string and returns equivalent yaml node
func ConvertJSONToYamlNode(jsonStr string) (*RNode, error) {
var body map[string]interface{}

View File

@@ -11,6 +11,137 @@ import (
"github.com/stretchr/testify/assert"
)
func TestRNodeFromMap(t *testing.T) {
testConfigMap := map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "winnie",
},
}
type thingExpected struct {
out string
err error
}
testCases := map[string]struct {
theMap map[string]interface{}
expected thingExpected
}{
"actuallyNil": {
theMap: nil,
expected: thingExpected{
out: `{}`,
err: nil,
},
},
"empty": {
theMap: map[string]interface{}{},
expected: thingExpected{
out: `{}`,
err: nil,
},
},
"mostlyEmpty": {
theMap: map[string]interface{}{
"hey": "there",
},
expected: thingExpected{
out: `hey: there`,
err: nil,
},
},
"configmap": {
theMap: testConfigMap,
expected: thingExpected{
out: `
apiVersion: v1
kind: ConfigMap
metadata:
name: winnie
`,
err: nil,
},
},
"list": {
theMap: map[string]interface{}{
"apiVersion": "v1",
"kind": "List",
"items": []interface{}{
testConfigMap,
testConfigMap,
},
},
expected: thingExpected{
out: `
apiVersion: v1
items:
- apiVersion: v1
kind: ConfigMap
metadata:
name: winnie
- apiVersion: v1
kind: ConfigMap
metadata:
name: winnie
kind: List
`,
err: nil,
},
},
"configmaplist": {
theMap: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMapList",
"items": []interface{}{
testConfigMap,
testConfigMap,
},
},
expected: thingExpected{
out: `
apiVersion: v1
items:
- apiVersion: v1
kind: ConfigMap
metadata:
name: winnie
- apiVersion: v1
kind: ConfigMap
metadata:
name: winnie
kind: ConfigMapList
`,
err: nil,
},
},
}
for n := range testCases {
tc := testCases[n]
t.Run(n, func(t *testing.T) {
rn, err := FromMap(tc.theMap)
if tc.expected.err == nil {
if !assert.NoError(t, err) {
t.FailNow()
}
if !assert.Equal(t,
strings.TrimSpace(tc.expected.out),
strings.TrimSpace(rn.MustString())) {
t.FailNow()
}
} else {
if !assert.Error(t, err) {
t.FailNow()
}
if !assert.Equal(t, tc.expected.err, err) {
t.FailNow()
}
}
})
}
}
// Test that non-UTF8 characters in comments don't cause failures
func TestRNode_GetMeta_UTF16(t *testing.T) {
sr, err := Parse(`apiVersion: rbac.istio.io/v1alpha1