Include json files for fn source

This commit is contained in:
Phani Teja Marupaka
2020-05-27 17:45:15 -07:00
parent fb6830c98a
commit 4cd3944860
6 changed files with 93 additions and 33 deletions

View File

@@ -724,22 +724,31 @@ func (rn *RNode) UnmarshalJSON(b []byte) error {
// ConvertJSONToYamlNode parses input json string and returns equivalent yaml node
func ConvertJSONToYamlNode(jsonStr string) (*RNode, error) {
var body map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &body)
yml, err := ConvertJSONToYamlString(jsonStr)
if err != nil {
return nil, err
}
yml, err := yaml.Marshal(body)
if err != nil {
return nil, err
}
node, err := Parse(string(yml))
node, err := Parse(yml)
if err != nil {
return nil, err
}
return node, nil
}
// ConvertJSONToYamlNode 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 {
return "", err
}
yml, err := yaml.Marshal(body)
if err != nil {
return "", err
}
return string(yml), nil
}
// checkKey returns true if all elems have the key
func checkKey(key string, elems []*Node) bool {
count := 0

View File

@@ -166,3 +166,19 @@ type: string
}
assert.Equal(t, expected, actual)
}
func TestConvertJSONToYamlString(t *testing.T) {
inputJSON := `{"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}`
expected := `enum:
- allowedValue1
- allowedValue2
maxLength: 15
type: string
`
actual, err := ConvertJSONToYamlString(inputJSON)
if !assert.NoError(t, err) {
t.FailNow()
}
assert.Equal(t, expected, actual)
}