Skip to content

Commit

Permalink
Flowpipe pipeline param default value compatibility test with the dec…
Browse files Browse the repository at this point in the history
…lared type may fail for complex types. Fixes #441.
  • Loading branch information
vhadianto committed Aug 14, 2024
1 parent ce23c59 commit 037fd85
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Shared Pipes Component

## v1.5.2 [2024-08-14]

* Flowpipe pipeline param default value compatibility test with the declared type may fail for complex types. ([#441](https://github.com/turbot/pipe-fittings/issues/441))

## v1.5.1 [2024-08-13]

_Bug fixes_
Expand Down
33 changes: 24 additions & 9 deletions hclhelpers/compatible.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,36 @@ func IsValueCompatibleWithType(ctyType cty.Type, value cty.Value) bool {

valueType := value.Type()

if ctyType.IsMapType() {
if ctyType.IsMapType() || ctyType.IsObjectType() {
if valueType.IsMapType() || valueType.IsObjectType() {
mapElementType := ctyType.ElementType()
if ctyType.IsCollectionType() {
mapElementType := ctyType.ElementType()

// Ensure the value is known before iterating over it to avoid panic
if value.IsKnown() {
for it := value.ElementIterator(); it.Next(); {
_, mapValue := it.Element()
if !IsValueCompatibleWithType(mapElementType, mapValue) {
// Ensure the value is known before iterating over it to avoid panic
if value.IsKnown() {
for it := value.ElementIterator(); it.Next(); {
_, mapValue := it.Element()
if !IsValueCompatibleWithType(mapElementType, mapValue) {
return false
}
}
return true
} else {
return false
}
} else if ctyType.IsObjectType() {
typeMapTypes := ctyType.AttributeTypes()
for name, typeValue := range typeMapTypes {
if valueType.HasAttribute(name) {
innerValue := value.GetAttr(name)
if !IsValueCompatibleWithType(typeValue, innerValue) {
return false
}
} else {
return false
}
}
return true
} else {
return false
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions hclhelpers/compatible_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ var compatibleTests = map[string]compatibleTest{
value: cty.ListVal([]cty.Value{cty.ListVal([]cty.Value{cty.ListVal([]cty.Value{cty.ListVal([]cty.Value{cty.NumberIntVal(23), cty.NumberIntVal(42)})})})}),
expected: false,
},
"map of an object": {
ctyType: cty.Map(cty.Object(map[string]cty.Type{"foo": cty.String, "bar": cty.String})),
value: cty.MapVal(map[string]cty.Value{"foo": cty.ObjectVal(map[string]cty.Value{"foo": cty.StringVal("foo"), "bar": cty.StringVal("bar")})}),
expected: true,
},
"map of an object 2": {
ctyType: cty.Map(cty.Object(map[string]cty.Type{"foo": cty.String, "bar": cty.Number})),
value: cty.ObjectVal(map[string]cty.Value{"foo": cty.ObjectVal(map[string]cty.Value{"foo": cty.StringVal("foo"), "bar": cty.NumberIntVal(42)})}),
expected: true,
},
}

func TestCompatible(t *testing.T) {
Expand Down

0 comments on commit 037fd85

Please sign in to comment.