Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parser): Fixed MarshalJSON Error on YAML Extend #3414 #3423

Merged
merged 2 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion pkg/parser/yaml/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,42 @@ func (p *Parser) Parse(filePath string, fileContent []byte) ([]model.Document, e
}
}

return documents, nil
return convertKeysToString(documents), nil
}

// convertKeysToString goes through every document to convert map[interface{}]interface{}
// to map[string]interface{}
func convertKeysToString(docs []model.Document) []model.Document {
documents := make([]model.Document, 0, len(docs))
for _, doc := range docs {
for key, value := range doc {
doc[key] = convert(value)
}
documents = append(documents, doc)
}
return documents
}

// convert goes recursively through the keys in the given value and converts nested maps type of map[interface{}]interface{}
// to map[string]interface{}
func convert(value interface{}) interface{} {
switch t := value.(type) {
case map[interface{}]interface{}:
mapStr := map[string]interface{}{}
for key, val := range t {
mapStr[key.(string)] = convert(val)
}
return mapStr
case []interface{}:
for key, val := range t {
t[key] = convert(val)
}
case model.Document:
for key, val := range t {
t[key] = convert(val)
}
}
return value
}

// SupportedExtensions returns extensions supported by this parser, which are yaml and yml extension
Expand Down
16 changes: 14 additions & 2 deletions pkg/parser/yaml/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ martin2:
mode: create
permission: authenticated-read
`, `
-name: test
-name: test2
test:
- &test_anchor
group:
name: "cx"
test_2:
perm:
- <<: *test_anchor
`,
}

Expand All @@ -57,6 +62,13 @@ martin2:
require.NoError(t, err)
require.Len(t, playbook, 1)
require.Contains(t, playbook[0]["playbooks"].([]interface{})[0].(map[string]interface{})["name"], "bucket2")

nestedMap, err := p.Parse("test.yaml", []byte(have[2]))
require.NoError(t, err)
require.Len(t, nestedMap, 1)
require.Contains(t, nestedMap[0], "test_2")
require.Contains(t,
nestedMap[0]["test_2"].(model.Document)["perm"].([]interface{})[0].(map[string]interface{})["group"].(model.Document)["name"], "cx")
}

// Test_Resolve tests the functions [Resolve()] and all the methods called by them
Expand Down