Skip to content

Commit

Permalink
Allow case insensitive prop typecheck on traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
aq17 committed Nov 8, 2022
1 parent 72f007f commit 0e43360
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
@@ -0,0 +1,4 @@
changes:
- type: feat
scope: pkg
description: Allow case insensitive prop typecheck on traversal
18 changes: 18 additions & 0 deletions pkg/codegen/hcl2/model/type_object.go
Expand Up @@ -72,9 +72,27 @@ func (t *ObjectType) Traverse(traverser hcl.Traverser) (Traversable, hcl.Diagnos
keyString, err := convert.Convert(key, cty.String)
contract.Assert(err == nil)

propertiesLower := make(map[string]string)
for p := range t.Properties {
propertiesLower[strings.ToLower(p)] = p
}

propertyName := keyString.AsString()
propertyType, hasProperty := t.Properties[propertyName]
if !hasProperty {
propertyNameLower := strings.ToLower(propertyName)
if propertyNameOrig, ok := propertiesLower[propertyNameLower]; ok {
propertyType = t.Properties[propertyNameOrig]
rng := traverser.SourceRange()
return propertyType, hcl.Diagnostics{
{
Severity: hcl.DiagWarning,
Subject: &rng,
Summary: "Found matching case-insensitive property",
Detail: fmt.Sprintf("Matched %s with %s", propertyName, propertyNameOrig),
},
}
}
props := make([]string, 0, len(t.Properties))
for k := range t.Properties {
props = append(props, k)
Expand Down
2 changes: 2 additions & 0 deletions pkg/codegen/hcl2/model/type_test.go
Expand Up @@ -508,6 +508,8 @@ func TestObjectType(t *testing.T) {
testTraverse(t, typ, hcl.TraverseAttr{Name: "bar"}, IntType, false)
testTraverse(t, typ, hcl.TraverseAttr{Name: "baz"}, NumberType, false)
testTraverse(t, typ, hcl.TraverseAttr{Name: "qux"}, NewOptionalType(BoolType), false)
// test case-insensitive attribute
testTraverse(t, typ, hcl.TraverseAttr{Name: "Qux"}, NewOptionalType(BoolType), true)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("foo")}, BoolType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("bar")}, IntType, false)
testTraverse(t, typ, hcl.TraverseIndex{Key: cty.StringVal("baz")}, NumberType, false)
Expand Down

0 comments on commit 0e43360

Please sign in to comment.