Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion ext/typeexpr/get_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ package typeexpr
import (
"fmt"

"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"

"github.com/hashicorp/hcl/v2"
)

const invalidTypeSummary = "Invalid type specification"
Expand Down Expand Up @@ -179,6 +180,18 @@ func getType(expr hcl.Expression, constraint, withDefaults bool) (cty.Type, *Def
})
continue
}

if _, exists := atys[attrName]; exists {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: invalidTypeSummary,
Detail: "Object constructor map keys must be unique.",
Subject: attrDef.Key.Range().Ptr(),
Context: expr.Range().Ptr(),
})
continue
}

atyExpr := attrDef.Value

// the attribute type expression might be wrapped in the special
Expand Down
27 changes: 26 additions & 1 deletion ext/typeexpr/get_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"github.com/hashicorp/hcl/v2/gohcl"

"github.com/google/go-cmp/cmp"
"github.com/zclconf/go-cty/cty"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/json"
"github.com/zclconf/go-cty/cty"
)

var (
Expand Down Expand Up @@ -700,6 +701,30 @@ func TestGetTypeDefaults(t *testing.T) {
nil,
`Optional attribute modifier expects at most two arguments: the attribute type, and a default value.`,
},

// Duplicate arguments.
{
`map(object({operations=optional(list(string), []),type=optional(string, "ABC"),type=optional(number)}))`,
&Defaults{
Type: cty.Map(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"operations": cty.List(cty.String),
"type": cty.String,
}, []string{"operations", "type"})),
Children: map[string]*Defaults{
"": {
Type: cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"operations": cty.List(cty.String),
"type": cty.String,
}, []string{"operations", "type"}),
DefaultValues: map[string]cty.Value{
"operations": cty.ListValEmpty(cty.String),
"type": cty.StringVal("ABC"),
},
},
},
},
"Object constructor map keys must be unique.",
},
}

for _, test := range tests {
Expand Down