Skip to content

Commit

Permalink
address review
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Dec 23, 2024
1 parent 9ecaf15 commit d4a1312
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 40 deletions.
54 changes: 16 additions & 38 deletions pkg/tfbridge/property_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,23 @@ func (SkipChildrenError) Error() string {
return "skip children"
}

type TypeMismatchError struct{}

func (TypeMismatchError) Error() string {
return "type mismatch"
}

type twoPropertyValueVisitor func(path propertyPath, val1, val2 resource.PropertyValue) error

// walkTwoPropertyValues walks the two property values and calls the visitor for each step.
// It returns an error if the visitor returns an error other than SkipChildrenError.
//
// The visitor can return SkipChildrenError to skip the children of the current step.
// In case the two values have different types, we walk both values, starting with val1.
//
// Note that elements inside a Secret or Output value will not get visited.
//
// Can return a TypeMismatchError in case the two values' types do not match.
func walkTwoPropertyValues(
path propertyPath,
val1, val2 resource.PropertyValue,
Expand All @@ -103,6 +113,10 @@ func walkTwoPropertyValues(
return err
}

if val1.IsNull() || val2.IsNull() {
return nil
}

if val1.IsArray() && val2.IsArray() {
arr1 := val1.ArrayValue()
arr2 := val2.ArrayValue()
Expand Down Expand Up @@ -134,44 +148,8 @@ func walkTwoPropertyValues(
return err
}
}
} else {
if val1.IsArray() {
arr1 := val1.ArrayValue()
for i, v := range arr1 {
childPath := path.Index(i)
err := walkTwoPropertyValues(childPath, v, resource.NewNullProperty(), visitor)
if err != nil {
return err
}
}
} else if val1.IsObject() {
obj1 := val1.ObjectValue()
for k, v := range obj1 {
err := walkTwoPropertyValues(path.Subkey(k), v, resource.NewNullProperty(), visitor)
if err != nil {
return err
}
}
}

if val2.IsArray() {
arr2 := val2.ArrayValue()
for i, v := range arr2 {
childPath := path.Index(i)
err := walkTwoPropertyValues(childPath, resource.NewNullProperty(), v, visitor)
if err != nil {
return err
}
}
} else if val2.IsObject() {
obj2 := val2.ObjectValue()
for k, v := range obj2 {
err := walkTwoPropertyValues(path.Subkey(k), resource.NewNullProperty(), v, visitor)
if err != nil {
return err
}
}
}
} else if val1.IsArray() || val2.IsArray() || val1.IsObject() || val2.IsObject() {
return TypeMismatchError{}
}
return nil
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/tfbridge/property_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ func TestWalkTwoPropertyValues(t *testing.T) {
return nil
})

require.NoError(t, err)
require.Equal(t, []string{"", "a", "a[0]", "a.b"}, visited)
require.Error(t, err)
require.IsType(t, TypeMismatchError{}, err)
})

t.Run("both arrays error", func(t *testing.T) {
Expand Down Expand Up @@ -228,6 +228,7 @@ func TestWalkTwoPropertyValues(t *testing.T) {
})

require.Error(t, err)
require.IsType(t, TypeMismatchError{}, err)
})

t.Run("mismatched types first object error", func(t *testing.T) {
Expand All @@ -251,6 +252,7 @@ func TestWalkTwoPropertyValues(t *testing.T) {
})

require.Error(t, err)
require.IsType(t, TypeMismatchError{}, err)
})

t.Run("mismatched types second array error", func(t *testing.T) {
Expand All @@ -274,6 +276,7 @@ func TestWalkTwoPropertyValues(t *testing.T) {
})

require.Error(t, err)
require.IsType(t, TypeMismatchError{}, err)
})

t.Run("mismatched types second object error", func(t *testing.T) {
Expand All @@ -297,5 +300,6 @@ func TestWalkTwoPropertyValues(t *testing.T) {
})

require.Error(t, err)
require.IsType(t, TypeMismatchError{}, err)
})
}

0 comments on commit d4a1312

Please sign in to comment.