Skip to content

Commit

Permalink
return error if field is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsayde committed Oct 10, 2022
1 parent 095ade8 commit f6582a0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 88 deletions.
4 changes: 4 additions & 0 deletions internal/operator/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ func GetOperator(name string) Operator {
}

func convertToNumber(value interface{}) (float64, error) {
if value == nil {
return 0, nil
}

val := reflect.ValueOf(value)
if val.Type().ConvertibleTo(floatType) {
return val.Convert(floatType).Float(), nil
Expand Down
4 changes: 2 additions & 2 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (n *Node) find(path []string) ([]*Node, error) {
if n.Type == Object {
item := n.mapItems[path[0]]
if item == nil {
return []*Node{{}}, nil
return nil, nil
}
return item.find(path[1:])
} else if n.Type == Array {
Expand All @@ -94,7 +94,7 @@ func (n *Node) find(path []string) ([]*Node, error) {
return n.listItems[int(index)].find(path[1:])
}
}
return []*Node{{}}, nil
return nil, nil
}

func lookup(in interface{}, parent *Node, path string, index int) *Node {
Expand Down
10 changes: 7 additions & 3 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestParserFind(t *testing.T) {
name: "scalar-not-found",
input: map[string]interface{}{"my-key": true},
key: "my-key-2",
expected: nil,
expected: (*Node)(nil),
},
{
name: "map-scalar-string",
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestParserFind(t *testing.T) {
},
},
key: "parent.child.not-found",
expected: nil,
expected: (*Node)(nil),
},
}

Expand All @@ -126,7 +126,11 @@ func TestParserFind(t *testing.T) {
if err != nil {
t.Error(err)
}
assert.Equal(t, test.expected, field.Value, "testcase #%d failed", i)
if field != nil {
assert.Equal(t, test.expected, field.Value, "testcase #%d failed", i)
} else {
assert.Equal(t, test.expected, field, "testcase #%d failed", i)
}
}
}

Expand Down
83 changes: 0 additions & 83 deletions tests/testcases/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,86 +21,3 @@ tests:
failed: true
errors:
- namespace must be default
- id: field-failing-check-2
input:
metadata:
result:
failed: true
errors:
- namespace must be default

---

id: check-statment-2
policy:
rules:
- when:
field: metadata.name
operator: equal
value: app
condition:
field: metadata.namespace
operator: equal
value: default
result: namespace must be default
tests:
- id: field-passing-check
input:
metadata:
name: app
namespace: default
result:
passed: true
- id: field-passing-check
input:
metadata:
name: test
namespace: default
result:
passed: true
- id: field-passing-check
input:
metadata:
name: app
namespace: test
result:
failed: true
errors:
- namespace must be default


# id: check-statment-2
# policy:
# rules:
# - condition:
# expr: default
# operator: equal
# value: default
# result: namespace must be default
# tests:
# - id: field-passing-check
# input:
# metadata:
# namespace: default
# result:
# passed: true

# ---

# id: check-statment-2
# policy:
# rules:
# - condition:
# expr: ${ .Params.test | upper }
# operator: equal
# value: DEFAULT
# result: namespace must be default
# tests:
# - id: field-passing-check
# input:
# metadata:
# namespace: default
# params:
# test: default
# result:
# passed: true
4 changes: 4 additions & 0 deletions yapl/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func (c *Condition) Eval(ctx *Context, input *parser.Node) ([]ConditionResult, e
return nil, err
}

if fields == nil {
return nil, fmt.Errorf("field %s not found", condition.Field)
}

for i := range fields {
ctx.Cond.Field = fields[i]
value := fields[i].Value
Expand Down

0 comments on commit f6582a0

Please sign in to comment.