Skip to content

Commit

Permalink
Cleans up some PropertyPath.Get() sites
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSeptimus-Klotho committed Aug 20, 2024
1 parent 7949de9 commit 79d3ab6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
36 changes: 24 additions & 12 deletions pkg/construct/properties_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package construct

import (
"github.com/klothoplatform/klotho/pkg/reflectutil"
"strings"
"testing"

"github.com/klothoplatform/klotho/pkg/reflectutil"

"github.com/klothoplatform/klotho/pkg/set"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -150,7 +151,8 @@ func TestResource_PropertyPath(t *testing.T) {
if !assert.NoError(err) {
return
}
v, _ := path.Get()
v, ok := path.Get()
assert.True(ok)
assert.Equal(tt.want, v)

// Test the last item's itemToPath instead of the path's Parts
Expand Down Expand Up @@ -187,28 +189,33 @@ func TestResource_PropertyPath_ops(t *testing.T) {
}

foo := path("A.foo")
v, _ := foo.Get()
v, ok := foo.Get()
assert.True(ok)
assert.Equal("bar", v)
if assert.NoError(foo.Set("baz")) {
v, _ := foo.Get()
v, ok := foo.Get()
assert.True(ok)
assert.Equal("baz", v)
}
assert.Error(foo.Append("value"))

if assert.NoError(foo.Remove(nil)) {
assert.Nil(foo.Get())
v, _ := path("A").Get()
v, ok := path("A").Get()
assert.True(ok)
m := v.(map[string]any)
assert.NotContains(m, "foo")
}

arr := path("A.array")
if assert.NoError(arr.Append("cat")) {
v, _ := arr.Get()
v, ok := arr.Get()
assert.True(ok)
assert.Equal([]any{"fox", "bat", "dog", "cat"}, v)
}
if assert.NoError(arr.Remove("bat")) {
v, _ := arr.Get()
v, ok := arr.Get()
assert.True(ok)
assert.Equal([]any{"fox", "dog", "cat"}, v)
}

Expand Down Expand Up @@ -238,7 +245,8 @@ func TestResource_PropertyPath_ops(t *testing.T) {

c := path("C")
if assert.NoError(c.Append(map[string]string{"hello": "world"})) {
v, _ := c.Get()
v, ok := c.Get()
assert.True(ok)
assert.Equal(map[string]any{
"x": "y",
"hello": "world",
Expand All @@ -254,20 +262,23 @@ func TestResource_PropertyPath_ops(t *testing.T) {
e := path("E")
if assert.NoError(e.Set([]string{"one", "two"})) {
assert.NoError(e.Append([]string{"three", "four"}))
v, _ := e.Get()
v, ok := e.Get()
assert.True(ok)
assert.Equal([]string{"one", "two", "three", "four"}, v)
}

tmp := path("temp")
if assert.NoError(tmp.Append("test")) {
v, _ := tmp.Get()
v, ok := tmp.Get()
assert.True(ok)
assert.Equal([]string{"test"}, v)
assert.NoError(tmp.Remove(nil))
}
assert.Nil(tmp.Get())

if assert.NoError(tmp.Append(map[string]string{"hello": "world"})) {
v, _ := tmp.Get()
v, ok := tmp.Get()
assert.True(ok)
assert.Equal(map[string]string{"hello": "world"}, v)
assert.NoError(tmp.Remove(nil))
}
Expand All @@ -276,7 +287,8 @@ func TestResource_PropertyPath_ops(t *testing.T) {
assert.Nil(nested.Get())
assert.Nil(path("deeply").Get())
if assert.NoError(nested.Set("test")) {
v, _ := path("deeply").Get()
v, ok := path("deeply").Get()
assert.True(ok)
assert.Equal(map[string]interface{}{"nested": map[string]interface{}{"value": "test"}}, v)
}
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/engine/operational_eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,10 @@ func (eval *Evaluator) cleanupPropertiesSubVertices(ref construct.PropertyRef, r
if err == nil {
// if the paths parent still exists then we know we will end up evaluating the vertex and should not remove it
parentIndex := len(path) - 2
parent, _ := path[parentIndex].Get()
if parentIndex < 0 || parent != nil {
if parentIndex < 0 {
continue
}
if parent, ok := path[parentIndex].Get(); ok && parent != nil {
continue
}
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/k2/constructs/import_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ func filterImportProperties(resources map[construct.ResourceId]*construct.Resour
}
for id, r := range resources {
_ = r.WalkProperties(func(path construct.PropertyPath, _ error) error {
v, _ := path.Get()
v, ok := path.Get()
if !ok {
return nil
}
switch v := v.(type) {
case construct.ResourceId:
if _, ok := resources[v]; !ok {
Expand Down

0 comments on commit 79d3ab6

Please sign in to comment.