Skip to content

Commit

Permalink
Merge pull request #7138 from hashicorp/b-better-dot-index-error
Browse files Browse the repository at this point in the history
core: Better error for dot indexing on user vars
  • Loading branch information
phinze authored Jun 12, 2016
2 parents 5289124 + ffa2909 commit 2127466
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
4 changes: 4 additions & 0 deletions config/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ func NewUserVariable(key string) (*UserVariable, error) {
name = name[:idx]
}

if len(elem) > 0 {
return nil, fmt.Errorf("Invalid dot index found: 'var.%s.%s'. Values in maps and lists can be referenced using square bracket indexing, like: 'var.mymap[\"key\"]' or 'var.mylist[1]'.", name, elem)
}

return &UserVariable{
key: key,

Expand Down
19 changes: 5 additions & 14 deletions config/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"reflect"
"strings"
"testing"

"github.com/hashicorp/hil"
Expand Down Expand Up @@ -143,20 +144,10 @@ func TestNewUserVariable(t *testing.T) {
}
}

func TestNewUserVariable_map(t *testing.T) {
v, err := NewUserVariable("var.bar.baz")
if err != nil {
t.Fatalf("err: %s", err)
}

if v.Name != "bar" {
t.Fatalf("bad: %#v", v.Name)
}
if v.Elem != "baz" {
t.Fatalf("bad: %#v", v.Elem)
}
if v.FullKey() != "var.bar.baz" {
t.Fatalf("bad: %#v", v)
func TestNewUserVariable_oldMapDotIndexErr(t *testing.T) {
_, err := NewUserVariable("var.bar.baz")
if err == nil || !strings.Contains(err.Error(), "Invalid dot index") {
t.Fatalf("Expected dot index err, got: %#v", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion config/test-fixtures/validate-good/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ resource "aws_security_group" "firewall" {
}

resource aws_instance "web" {
ami = "${var.amis.east}"
ami = "${var.amis["east"]}"
security_groups = [
"foo",
"${aws_security_group.firewall.foo}"
Expand Down

0 comments on commit 2127466

Please sign in to comment.