Skip to content

Commit

Permalink
move type mismatch check and short-circuit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Oct 18, 2024
1 parent 629a5ff commit 49ec2be
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions pkg/tfbridge/detailed_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,38 @@ func (differ detailedDiffer) makeShortCircuitDiff(
return map[detailedDiffKey]*pulumirpc.PropertyDiff{path.Key(): propDiff}
}

func isTypeMismatched(val resource.PropertyValue, propType shim.ValueType) bool {
if !isPresent(val) {
return false
}
switch propType {
case shim.TypeList:
return !val.IsArray()
case shim.TypeSet:
return !val.IsArray()
case shim.TypeMap:
return !val.IsObject()
default:
return false
}
}

func (differ detailedDiffer) makePropDiff(
path propertyPath, old, new resource.PropertyValue,
) map[detailedDiffKey]*pulumirpc.PropertyDiff {
if path.IsReservedKey() {
return nil
}
propType := differ.getEffectiveType(differ.propertyPathToSchemaPath(path))
if !isPresent(old) || isTypeMismatched(old, propType) {
old = resource.NewNullProperty()
}
if !isPresent(new) || isTypeMismatched(new, propType) && !new.IsComputed() {
new = resource.NewNullProperty()
}
if old.IsNull() || new.IsNull() || new.IsComputed() {
return differ.makeShortCircuitDiff(path, old, new)
}

switch propType {
case shim.TypeList:
Expand All @@ -329,16 +354,6 @@ func (differ detailedDiffer) makePropDiff(
func (differ detailedDiffer) makeListDiff(
path propertyPath, old, new resource.PropertyValue,
) map[detailedDiffKey]*pulumirpc.PropertyDiff {
if !isPresent(old) || !old.IsArray() {
old = resource.NewNullProperty()
}
if (!isPresent(new) || !new.IsArray()) && !new.IsComputed() {
new = resource.NewNullProperty()
}
if old.IsNull() || new.IsNull() || new.IsComputed() {
return differ.makeShortCircuitDiff(path, old, new)
}

diff := make(map[detailedDiffKey]*pulumirpc.PropertyDiff)
oldList := old.ArrayValue()
newList := new.ArrayValue()
Expand Down Expand Up @@ -368,16 +383,6 @@ func (differ detailedDiffer) makeListDiff(
func (differ detailedDiffer) makeMapDiff(
path propertyPath, old, new resource.PropertyValue,
) map[detailedDiffKey]*pulumirpc.PropertyDiff {
if !isPresent(old) || !old.IsObject() {
old = resource.NewNullProperty()
}
if !isPresent(new) || !new.IsObject() && !new.IsComputed() {
new = resource.NewNullProperty()
}
if old.IsNull() || new.IsNull() || new.IsComputed() {
return differ.makeShortCircuitDiff(path, old, new)
}

oldMap := old.ObjectValue()
newMap := new.ObjectValue()
diff := make(map[detailedDiffKey]*pulumirpc.PropertyDiff)
Expand Down

0 comments on commit 49ec2be

Please sign in to comment.