From d41432d951f2813d594a5a09394d7a65333a319b Mon Sep 17 00:00:00 2001 From: James Nugent Date: Mon, 21 Mar 2016 12:14:31 +0000 Subject: [PATCH] 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. --- decoder.go | 8 ++++++++ decoder_test.go | 8 ++++++++ test-fixtures/nested_provider_bad.hcl | 5 +++++ 3 files changed, 21 insertions(+) create mode 100644 test-fixtures/nested_provider_bad.hcl diff --git a/decoder.go b/decoder.go index 6b01859..02888d2 100644 --- a/decoder.go +++ b/decoder.go @@ -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) diff --git a/decoder_test.go b/decoder_test.go index 3f07200..7ef6963 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -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, diff --git a/test-fixtures/nested_provider_bad.hcl b/test-fixtures/nested_provider_bad.hcl new file mode 100644 index 0000000..94a753a --- /dev/null +++ b/test-fixtures/nested_provider_bad.hcl @@ -0,0 +1,5 @@ +resource "aws" "web" { + provider = "aws" { + region = "us-west-2" + } +}