Skip to content

Commit

Permalink
Merge pull request #25 from KeisukeYamashita/change-map-key-to-remove…
Browse files Browse the repository at this point in the history
…-dot

Remove dot in map attribute
  • Loading branch information
KeisukeYamashita authored Jan 3, 2020
2 parents 595eca5 + fd36e9b commit 7a7312b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 11 additions & 1 deletion internal/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ func decodeBlockToMap(block *schema.Block, val reflect.Value) {
mv := reflect.MakeMap(val.Type())

for k, attr := range content.Attributes {
mv.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(attr.Value))
key := removeAttrDot(k)
mv.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(attr.Value))
}

blocksByType := content.Blocks.ByType()
Expand Down Expand Up @@ -477,3 +478,12 @@ func getFieldTags(ty reflect.Type) *fieldTags {

return ret
}

func removeAttrDot(v interface{}) interface{} {
str, ok := v.(string)
if !ok {
return v
}

return strings.Trim(str, ".")
}
5 changes: 4 additions & 1 deletion internal/decoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,10 @@ acl bye {
"with flat block": {`acl hello {
"localhost";
"local";
}`, map[string]interface{}{}, map[string]interface{}{"acl": map[string]interface{}{"hello": []interface{}{"localhost", "local"}}}}}
}`, map[string]interface{}{}, map[string]interface{}{"acl": map[string]interface{}{"hello": []interface{}{"localhost", "local"}}}},
"with dot attribute block": {`backend default {
.port = "8080";
}`, map[string]interface{}{}, map[string]interface{}{"backend": map[string]interface{}{"default": map[string]interface{}{"port": "8080"}}}}}

for n, tc := range testCases {
t.Run(n, func(t *testing.T) {
Expand Down

0 comments on commit 7a7312b

Please sign in to comment.