mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Write json files in sink
This commit is contained in:
@@ -735,7 +735,7 @@ func ConvertJSONToYamlNode(jsonStr string) (*RNode, error) {
|
||||
return node, nil
|
||||
}
|
||||
|
||||
// ConvertJSONToYamlNode parses input json string and returns equivalent yaml string
|
||||
// ConvertJSONToYamlString parses input json string and returns equivalent yaml string
|
||||
func ConvertJSONToYamlString(jsonStr string) (string, error) {
|
||||
var body map[string]interface{}
|
||||
err := json.Unmarshal([]byte(jsonStr), &body)
|
||||
@@ -749,6 +749,20 @@ func ConvertJSONToYamlString(jsonStr string) (string, error) {
|
||||
return string(yml), nil
|
||||
}
|
||||
|
||||
// ConvertYamlNodeToJSONString returns json string from input yaml RNode
|
||||
func ConvertYamlNodeToJSONString(node *RNode) (string, error) {
|
||||
nodeStr, err := node.String()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var body interface{}
|
||||
if err := yaml.Unmarshal([]byte(nodeStr), &body); err != nil {
|
||||
return "", err
|
||||
}
|
||||
res, err := json.Marshal(body)
|
||||
return string(res), err
|
||||
}
|
||||
|
||||
// checkKey returns true if all elems have the key
|
||||
func checkKey(key string, elems []*Node) bool {
|
||||
count := 0
|
||||
|
||||
@@ -182,3 +182,21 @@ type: string
|
||||
}
|
||||
assert.Equal(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestConvertYamlNodeToJSONStr(t *testing.T) {
|
||||
yl := `enum:
|
||||
- allowedValue1
|
||||
- allowedValue2
|
||||
maxLength: 15
|
||||
type: string
|
||||
`
|
||||
node, err := Parse(yl)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
res, err := ConvertYamlNodeToJSONString(node)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
assert.Equal(t, `{"enum":["allowedValue1","allowedValue2"],"maxLength":15,"type":"string"}`, res)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user