Skip to content

Commit

Permalink
decoder: Support AnyAttribute in Validate (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Aug 8, 2023
1 parent 42d4eba commit 01ef96c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
21 changes: 12 additions & 9 deletions decoder/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ func (d *PathDecoder) validateBody(ctx context.Context, body *hclsyntax.Body, bo
for name, attribute := range body.Attributes {
attributeSchema, ok := bodySchema.Attributes[name]
if !ok {
// ---------- diag ERR unknown attribute
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unexpected attribute",
Detail: fmt.Sprintf("An attribute named %q is not expected here", name),
Subject: attribute.SrcRange.Ptr(),
})
// don't check futher because this isn't a valid attribute
continue
if bodySchema.AnyAttribute == nil {
// ---------- diag ERR unknown attribute
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unexpected attribute",
Detail: fmt.Sprintf("An attribute named %q is not expected here", name),
Subject: attribute.SrcRange.Ptr(),
})
// don't check futher because this isn't a valid attribute
continue
}
attributeSchema = bodySchema.AnyAttribute
}

// ---------- diag WARN deprecated attribute
Expand Down
54 changes: 54 additions & 0 deletions decoder/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,60 @@ wakka = 2
"test.tf": {},
},
},
{
"any attribute",
&schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"foo": {
Body: &schema.BodySchema{
AnyAttribute: &schema.AttributeSchema{
Constraint: schema.LiteralType{Type: cty.Number},
IsOptional: true,
},
},
},
},
},
`foo {
test = 1
}`,
map[string]hcl.Diagnostics{
"test.tf": {},
},
},
{
"deprecated any attribute",
&schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"foo": {
Body: &schema.BodySchema{
AnyAttribute: &schema.AttributeSchema{
Constraint: schema.LiteralType{Type: cty.Number},
IsOptional: true,
IsDeprecated: true,
},
},
},
},
},
`foo {
test = 1
}`,
map[string]hcl.Diagnostics{
"test.tf": {
{
Severity: hcl.DiagWarning,
Summary: `"test" is deprecated`,
Detail: `Reason: ""`,
Subject: &hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 2, Column: 5, Byte: 10},
End: hcl.Pos{Line: 2, Column: 13, Byte: 18},
},
},
},
},
},
}

for i, tc := range testCases {
Expand Down

0 comments on commit 01ef96c

Please sign in to comment.