Return an error if object keys are not strings

This now gives an error instead of a panic when encountering
configuration such as described in hashicorp/terraform#5740:

```
resource "aws" "web" {
  provider = "aws" {
    region = "us-west-2"
  }
}
```

We now return an error message - "hcl object keys must be a string"
instead of crashing.

Fixes hashicorp/terraform#5740.
This commit is contained in:
James Nugent 2016-03-21 12:14:31 +00:00
parent 32f2911ca2
commit d41432d951
3 changed files with 21 additions and 0 deletions

View File

@ -337,6 +337,14 @@ func (d *decoder) decodeMap(name string, node ast.Node, result reflect.Value) er
continue
}
// github.com/hashicorp/terraform/issue/5740
if len(item.Keys) == 0 {
return &parser.PosError{
Pos: node.Pos(),
Err: fmt.Errorf("%s: map must have string keys", name),
}
}
// Get the key we're dealing with, which is the first item
keyStr := item.Keys[0].Token.Value().(string)

View File

@ -245,6 +245,14 @@ func TestDecode_interface(t *testing.T) {
nil,
},
{
"nested_provider_bad.hcl",
true,
// This is not ideal but without significant rework of the decoder
// we get a partial result back as well as an error.
map[string]interface{}{},
},
{
"object_list.json",
false,

View File

@ -0,0 +1,5 @@
resource "aws" "web" {
provider = "aws" {
region = "us-west-2"
}
}