mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Suggested changes
This commit is contained in:
@@ -722,9 +722,9 @@ func (rn *RNode) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConvertJSONToYamlNode parses input json string and returns equivalent yaml node
|
||||
func ConvertJSONToYamlNode(jsonStr string) (*RNode, error) {
|
||||
yml, err := ConvertJSONToYamlString(jsonStr)
|
||||
// ConvertJSONToYAMLNode parses input json string and returns equivalent yaml node
|
||||
func ConvertJSONToYAMLNode(jsonStr string) (*RNode, error) {
|
||||
yml, err := ConvertJSONToYAMLString(jsonStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -735,8 +735,8 @@ func ConvertJSONToYamlNode(jsonStr string) (*RNode, error) {
|
||||
return node, nil
|
||||
}
|
||||
|
||||
// ConvertJSONToYamlString parses input json string and returns equivalent yaml string
|
||||
func ConvertJSONToYamlString(jsonStr string) (string, error) {
|
||||
// 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)
|
||||
if err != nil {
|
||||
@@ -749,20 +749,6 @@ 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
|
||||
|
||||
@@ -147,7 +147,7 @@ hello: world
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertJSONToYamlNode(t *testing.T) {
|
||||
func TestConvertJSONToYAMLNode(t *testing.T) {
|
||||
inputJSON := `{"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}`
|
||||
expected := `enum:
|
||||
- allowedValue1
|
||||
@@ -156,7 +156,7 @@ maxLength: 15
|
||||
type: string
|
||||
`
|
||||
|
||||
node, err := ConvertJSONToYamlNode(inputJSON)
|
||||
node, err := ConvertJSONToYAMLNode(inputJSON)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -167,7 +167,7 @@ type: string
|
||||
assert.Equal(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestConvertJSONToYamlString(t *testing.T) {
|
||||
func TestConvertJSONToYAMLString(t *testing.T) {
|
||||
inputJSON := `{"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}`
|
||||
expected := `enum:
|
||||
- allowedValue1
|
||||
@@ -176,27 +176,22 @@ maxLength: 15
|
||||
type: string
|
||||
`
|
||||
|
||||
actual, err := ConvertJSONToYamlString(inputJSON)
|
||||
actual, err := ConvertJSONToYAMLString(inputJSON)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
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) {
|
||||
// error if there are multiple json blobs in input string
|
||||
func TestConvertJSONToYAMLStringError(t *testing.T) {
|
||||
inputJSON := `{"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}
|
||||
{"type": "string", "maxLength": 5, "enum": ["allowedValue2", "allowedValue3"]}`
|
||||
expected := `invalid character '{' after top-level value`
|
||||
|
||||
_, err := ConvertJSONToYAMLString(inputJSON)
|
||||
if !assert.Error(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)
|
||||
assert.Equal(t, expected, err.Error())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user