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

Added support for dereferenced map properties #2635

Merged
merged 5 commits into from
Jan 31, 2025
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
27 changes: 27 additions & 0 deletions pkg/exprparser/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func TestFunctionContains(t *testing.T) {
{`contains(fromJSON('[3.14,"second"]'), 3.14) }}`, true, "contains-item-number-number"},
{`contains(fromJSON('["","second"]'), fromJSON('[]')) }}`, false, "contains-item-str-arr"},
{`contains(fromJSON('["","second"]'), fromJSON('{}')) }}`, false, "contains-item-str-obj"},
{`contains(fromJSON('[{ "first": { "result": "success" }},{ "second": { "result": "success" }}]').first.result, 'success') }}`, true, "multiple-contains-item"},
{`contains(fromJSON('[{ "result": "success" },{ "result": "failure" }]').*.result, 'failure') }}`, true, "multiple-contains-dereferenced-failure-item"},
{`contains(fromJSON('[{ "result": "failure" },{ "result": "success" }]').*.result, 'success') }}`, true, "multiple-contains-dereferenced-success-item"},
{`contains(fromJSON('[{ "result": "failure" },{ "result": "success" }]').*.result, 'notthere') }}`, false, "multiple-contains-dereferenced-missing-item"},
{`contains(fromJSON('[{ "result": "failure", "outputs": { "key": "val1" } },{ "result": "success", "outputs": { "key": "val2" } }]').*.outputs.key, 'val1') }}`, true, "multiple-contains-dereferenced-output-item"},
{`contains(fromJSON('[{ "result": "failure", "outputs": { "key": "val1" } },{ "result": "success", "outputs": { "key": "val2" } }]').*.outputs.key, 'val2') }}`, true, "multiple-contains-dereferenced-output-item-2"},
{`contains(fromJSON('[{ "result": "failure", "outputs": { "key": "val1" } },{ "result": "success", "outputs": { "key": "val2" } }]').*.outputs.key, 'missing') }}`, false, "multiple-contains-dereferenced-output-misssing-item"},
}
m1r4c marked this conversation as resolved.
Show resolved Hide resolved

env := &EvaluationEnvironment{}
Expand Down Expand Up @@ -249,3 +256,23 @@ func TestFunctionFormat(t *testing.T) {
})
}
}

func TestMapContains(t *testing.T) {
env := &EvaluationEnvironment{
Needs: map[string]Needs{
"first-job": {
Outputs: map[string]string{},
Result: "success",
},
"second-job": {
Outputs: map[string]string{},
Result: "failure",
},
},
}

output, err := NewInterpeter(env, Config{}).Evaluate("contains(needs.*.result, 'failure')", DefaultStatusCheckNone)
assert.Nil(t, err)

assert.Equal(t, true, output)
}
32 changes: 32 additions & 0 deletions pkg/exprparser/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@
return nil, err
}

_, receiverIsDeref := objectDerefNode.Receiver.(*actionlint.ArrayDerefNode)
if receiverIsDeref {
return impl.getPropertyValueDereferenced(reflect.ValueOf(left), objectDerefNode.Property)
}
return impl.getPropertyValue(reflect.ValueOf(left), objectDerefNode.Property)
}

Expand Down Expand Up @@ -312,6 +316,34 @@
return nil, nil
}

func (impl *interperterImpl) getPropertyValueDereferenced(left reflect.Value, property string) (value interface{}, err error) {
switch left.Kind() {
case reflect.Ptr:
return impl.getPropertyValue(left, property)

Check warning on line 322 in pkg/exprparser/interpreter.go

View check run for this annotation

Codecov / codecov/patch

pkg/exprparser/interpreter.go#L321-L322

Added lines #L321 - L322 were not covered by tests

case reflect.Struct:
return impl.getPropertyValue(left, property)

Check warning on line 325 in pkg/exprparser/interpreter.go

View check run for this annotation

Codecov / codecov/patch

pkg/exprparser/interpreter.go#L324-L325

Added lines #L324 - L325 were not covered by tests
case reflect.Map:
iter := left.MapRange()

var values []interface{}
for iter.Next() {
value, err := impl.getPropertyValue(iter.Value(), property)
if err != nil {
return nil, err
}

Check warning on line 334 in pkg/exprparser/interpreter.go

View check run for this annotation

Codecov / codecov/patch

pkg/exprparser/interpreter.go#L333-L334

Added lines #L333 - L334 were not covered by tests

values = append(values, value)
}

return values, nil
case reflect.Slice:
return impl.getPropertyValue(left, property)
}

return nil, nil
}

func (impl *interperterImpl) getMapValue(value reflect.Value) (interface{}, error) {
if value.Kind() == reflect.Ptr {
return impl.getMapValue(value.Elem())
Expand Down
8 changes: 8 additions & 0 deletions pkg/exprparser/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,8 @@ func TestContexts(t *testing.T) {
{"matrix.os", "Linux", "matrix-context"},
{"needs.job-id.outputs.output-name", "value", "needs-context"},
{"needs.job-id.result", "success", "needs-context"},
{"contains(needs.*.result, 'success')", true, "needs-wildcard-context-contains-success"},
{"contains(needs.*.result, 'failure')", false, "needs-wildcard-context-contains-failure"},
{"inputs.name", "value", "inputs-context"},
}

Expand Down Expand Up @@ -610,6 +612,12 @@ func TestContexts(t *testing.T) {
},
Result: "success",
},
"another-job-id": {
Outputs: map[string]string{
"output-name": "value",
},
Result: "success",
},
},
Inputs: map[string]interface{}{
"name": "value",
Expand Down
Loading