Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lenient string prop #951

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/engine/operational_eval/vertex_path_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 !strings.HasPrefix(prop.Type(), "resource") {
continue
}
details := prop.Details()
if details.OperationalRule == nil {
// If the property can't create resources, skip it.
Expand Down
1 change: 1 addition & 0 deletions pkg/engine/operational_eval/vertex_path_expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
13 changes: 8 additions & 5 deletions pkg/knowledgebase/properties/string_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 12 additions & 2 deletions pkg/knowledgebase/properties/string_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down
Loading