Skip to content

Commit

Permalink
always quote hcl map keys
Browse files Browse the repository at this point in the history
HCL identifiers may need to be quoted, so always quote them to be safe.
  • Loading branch information
jbardin committed Feb 24, 2017
1 parent 38748f0 commit 43f62d2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion command/hcl_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (e *encodeState) encodeMap(m map[string]interface{}) error {
for i, k := range sortedKeys(m) {
v := m[k]

e.WriteString(k + " = ")
e.WriteString(fmt.Sprintf("%q = ", k))
err := e.encode(v)
if err != nil {
return err
Expand Down
52 changes: 52 additions & 0 deletions command/hcl_printer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package command

import "testing"

// The command package has it's own HCL encoder to encode variables to push.
// Make sure the variable we encode parse correctly
func TestHCLEncoder_parse(t *testing.T) {
cases := []struct {
Name string
Val interface{}
Error bool
}{
{
Name: "int",
Val: 12345,
},
{
Name: "float",
Val: 1.2345,
},
{
Name: "string",
Val: "terraform",
},
{
Name: "list",
Val: []interface{}{"a", "b", "c"},
},
{
Name: "map",
Val: map[string]interface{}{
"a": 1,
},
},
// a numeric looking identifier requires quotes
{
Name: "map_with_quoted_key",
Val: map[string]interface{}{
"0.0.0.0/24": "mask",
},
},
}

for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
_, err := encodeHCL(c.Val)
if err != nil {
t.Fatal(err)
}
})
}
}

0 comments on commit 43f62d2

Please sign in to comment.