From 5aeb3943c7ac6a4fdac0db825b16e872b6d20f48 Mon Sep 17 00:00:00 2001 From: Gordon Date: Thu, 29 Feb 2024 11:40:00 -0500 Subject: [PATCH 1/2] Convert non-string primitive values for string property --- pkg/engine/operational_eval/vertex_path_expand.go | 4 ++++ pkg/knowledgebase/properties/string_property.go | 13 ++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pkg/engine/operational_eval/vertex_path_expand.go b/pkg/engine/operational_eval/vertex_path_expand.go index 5255e2f9c..6408df764 100644 --- a/pkg/engine/operational_eval/vertex_path_expand.go +++ b/pkg/engine/operational_eval/vertex_path_expand.go @@ -135,6 +135,10 @@ func (v *pathExpandVertex) addDepsFromProps( } var errs error for k, prop := range tmpl.Properties { + // Only consider properties whose type can even accommodate a resource + if prop.Type() != "resource" { + continue + } details := prop.Details() if details.OperationalRule == nil { // If the property can't create resources, skip it. diff --git a/pkg/knowledgebase/properties/string_property.go b/pkg/knowledgebase/properties/string_property.go index b8bfcac19..249168f73 100644 --- a/pkg/knowledgebase/properties/string_property.go +++ b/pkg/knowledgebase/properties/string_property.go @@ -64,12 +64,15 @@ func (str *StringProperty) Parse(value any, ctx knowledgebase.DynamicContext, da if err == nil { return val, nil } - if val, ok := value.(string); ok { - var result string - err := ctx.ExecuteDecode(val, data, &result) - return result, err + switch val := value.(type) { + case string: + err := ctx.ExecuteDecode(val, data, &val) + return val, err + + case int, int32, int64, float32, float64, bool: + return fmt.Sprintf("%v", val), nil } - return nil, fmt.Errorf("could not parse string property: invalid string value %v", value) + return nil, fmt.Errorf("could not parse string property: invalid string value %v (%[1]T)", value) } func (s *StringProperty) ZeroValue() any { From 2e5bffe3c482b9ff568ee0ff50789e80d7ac44eb Mon Sep 17 00:00:00 2001 From: Gordon Date: Thu, 29 Feb 2024 11:52:48 -0500 Subject: [PATCH 2/2] Use prefix instead of equal to handle 'resource(matcher)' types --- pkg/engine/operational_eval/vertex_path_expand.go | 2 +- .../operational_eval/vertex_path_expand_test.go | 1 + .../properties/string_property_test.go | 14 ++++++++++++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/engine/operational_eval/vertex_path_expand.go b/pkg/engine/operational_eval/vertex_path_expand.go index 6408df764..451251d6f 100644 --- a/pkg/engine/operational_eval/vertex_path_expand.go +++ b/pkg/engine/operational_eval/vertex_path_expand.go @@ -136,7 +136,7 @@ func (v *pathExpandVertex) addDepsFromProps( var errs error for k, prop := range tmpl.Properties { // Only consider properties whose type can even accommodate a resource - if prop.Type() != "resource" { + if !strings.HasPrefix(prop.Type(), "resource") { continue } details := prop.Details() diff --git a/pkg/engine/operational_eval/vertex_path_expand_test.go b/pkg/engine/operational_eval/vertex_path_expand_test.go index 4a0a8ed2c..11f794618 100644 --- a/pkg/engine/operational_eval/vertex_path_expand_test.go +++ b/pkg/engine/operational_eval/vertex_path_expand_test.go @@ -154,6 +154,7 @@ func Test_pathExpandVertex_addDepsFromProps(t *testing.T) { mockSol.On("RawView").Return(resultGraph).Once() mockProperty.EXPECT().Validate(resource, construct.ResourceId{Name: "u"}, gomock.Any()).Return(nil).Times(1) + mockProperty.EXPECT().Type().Return("resource").Times(1) return nil }, want: graphChanges{ diff --git a/pkg/knowledgebase/properties/string_property_test.go b/pkg/knowledgebase/properties/string_property_test.go index 68f3af66e..dec95514c 100644 --- a/pkg/knowledgebase/properties/string_property_test.go +++ b/pkg/knowledgebase/properties/string_property_test.go @@ -235,13 +235,23 @@ func Test_StringParse(t *testing.T) { expected: "test", }, { - name: "non string throws error", + name: "non string converts int", + property: &StringProperty{ + PropertyDetails: knowledgebase.PropertyDetails{ + Path: "test", + }, + }, + value: 1, + expected: "1", + }, + { + name: "non string fails non-primitive", property: &StringProperty{ PropertyDetails: knowledgebase.PropertyDetails{ Path: "test", }, }, - value: 1, + value: []string{"hi"}, wantErr: true, }, }