Skip to content
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
13 changes: 8 additions & 5 deletions pkg/JsonResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ func buildCompoundOperator(o interface{}, depth int, operator string) (string, b
}
}
if depth > 0 {

return "(" + strings.Join(ands, operator) + ")", false, nil
}

Expand Down Expand Up @@ -467,7 +466,7 @@ func joinOperator(v interface{}, operator string) (string, error) {
}
ops := make([]string, len(arr))
for i := 0; i < len(arr); i++ {
ope, err := parseOperand(arr[i], false)
ope, err := parseOperand(arr[i], false, operator == " != ")
if err != nil {

return "", err
Expand All @@ -487,12 +486,12 @@ func joinSet(v interface{}, operator string) (string, error) {

return "", fmt.Errorf("set operand count must be 2")
}
leftOpe, err := parseOperand(arr[0], true)
leftOpe, err := parseOperand(arr[0], true, false)
if err != nil {

return "", err
}
rightOpe, err := parseOperand(arr[1], true)
rightOpe, err := parseOperand(arr[1], true, false)
if err != nil {

return "", err
Expand All @@ -504,7 +503,7 @@ func joinSet(v interface{}, operator string) (string, error) {
return "", fmt.Errorf("operator has an unexpected type")
}

func parseOperand(o interface{}, noWrap bool) (string, error) {
func parseOperand(o interface{}, noWrap bool, negation bool) (string, error) {
switch operandType := o.(type) {
case string:

Expand Down Expand Up @@ -532,6 +531,10 @@ func parseOperand(o interface{}, noWrap bool) (string, error) {
return expr, nil
}

if negation {
return "!(" + expr + ")", nil
}

return "(" + expr + ")", nil
default:

Expand Down
36 changes: 35 additions & 1 deletion pkg/JsonResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
package pkg

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

const jsonData = `{
Expand Down Expand Up @@ -209,6 +210,33 @@ const expectedBigIntConversion = `rule SpeedUp "When testcar is speeding up we k
}
`

const jsonNegation = `[{
"name": "SpeedConstant",
"desc": "When testcar is not speeding up we keep constant \"speed\".",
"salience": 10,
"when": {
"not": [
{
"and": [
{"eq": ["TestCar.SpeedUp", true]},
{"not": ["TestCar.Speed", 10]}
]
}
]
},
"then": [
{"call": ["Log", {"const": "\"Speed\" constant\n"}]}
]
}]`

const expecteJsonNegation = `rule SpeedConstant "When testcar is not speeding up we keep constant \"speed\"." salience 10 {
when
!(TestCar.SpeedUp == true && TestCar.Speed != 10)
then
Log("\"Speed\" constant\n");
}
`

func TestParseJSONRuleset(t *testing.T) {
rs, err := ParseJSONRule([]byte(jsonData))
assert.NoError(t, err)
Expand Down Expand Up @@ -262,3 +290,9 @@ func TestJSONBigIntConversion(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expectedBigIntConversion, rs)
}

func TestJsonNegation(t *testing.T) {
rs, err := ParseJSONRuleset([]byte(jsonNegation))
assert.NoError(t, err)
assert.Equal(t, expecteJsonNegation, rs)
}
Loading