Skip to content

Commit

Permalink
Merge #11266
Browse files Browse the repository at this point in the history
11266: Allow case insensitive prop typecheck on traversal r=aq17 a=aq17

<!--- 
Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation.
-->

# Description

<!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. -->

Addresses some errors in #11070 

## Checklist

<!--- Please provide details if the checkbox below is to be left unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the Pulumi Service,
then the service should honor older versions of the CLI where this change would not exist.
You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Service API version
  <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. -->


Co-authored-by: aq17 <aqiu@pulumi.com>
  • Loading branch information
bors[bot] and aq17 committed Nov 8, 2022
2 parents 83c9dfc + 13bb77a commit 4910d9c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
@@ -0,0 +1,4 @@
changes:
- type: feat
scope: programgen
description: More programs can be converted to Pulumi when using `pulumi convert`, provider bridging, and conversion tools by allowing property accesses and field names to fall back to a case insensitive lookup.
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 4910d9c

Please sign in to comment.