Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for duplicate keys in objects when building types from expressions #624

Merged
merged 1 commit into from
Aug 30, 2023
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
Check for duplicate keys in objects when building types from expressions
  • Loading branch information
liamcervante committed Aug 28, 2023
commit 4259e4ba13b9bc9807f95b101c770bfa8b3490e9
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