Skip to content

Commit

Permalink
Implement operation on duration type and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sonda2208 committed Jun 13, 2019
1 parent 35a3569 commit aa219d1
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 0 deletions.
60 changes: 60 additions & 0 deletions evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ func computeEQ(lhs, rhs Expr) (*BoolLiteral, error) {

return &BoolLiteral{Val: (l.Val == dt)}, nil
}
case *DurationLiteral:
rv, ok := rhs.(*StringLiteral)
if ok {
v, err := time.ParseDuration(rv.Val)
if err != nil {
return nil, err
}

return &BoolLiteral{Val: l.Val == v}, nil
}
}

return nil, fmt.Errorf(`cannot convert "%s" to %s`, rhs.String(), lhs.Type())
Expand Down Expand Up @@ -175,6 +185,16 @@ func computeNEQ(lhs, rhs Expr) (*BoolLiteral, error) {

return &BoolLiteral{Val: (l.Val != dt)}, nil
}
case *DurationLiteral:
rv, ok := rhs.(*StringLiteral)
if ok {
v, err := time.ParseDuration(rv.Val)
if err != nil {
return nil, err
}

return &BoolLiteral{Val: l.Val != v}, nil
}
}

return nil, fmt.Errorf(`cannot convert "%s" to %s`, rhs.String(), lhs.Type())
Expand Down Expand Up @@ -207,6 +227,16 @@ func computeLT(lhs, rhs Expr) (*BoolLiteral, error) {

return &BoolLiteral{Val: l.Val.Before(dt)}, nil
}
case *DurationLiteral:
rv, ok := rhs.(*StringLiteral)
if ok {
v, err := time.ParseDuration(rv.Val)
if err != nil {
return nil, err
}

return &BoolLiteral{Val: l.Val < v}, nil
}
}

return nil, fmt.Errorf(`cannot convert "%s" to %s`, rhs.String(), lhs.Type())
Expand Down Expand Up @@ -239,6 +269,16 @@ func computeLTE(lhs, rhs Expr) (*BoolLiteral, error) {

return &BoolLiteral{Val: l.Val.Before(dt)}, nil
}
case *DurationLiteral:
rv, ok := rhs.(*StringLiteral)
if ok {
v, err := time.ParseDuration(rv.Val)
if err != nil {
return nil, err
}

return &BoolLiteral{Val: l.Val <= v}, nil
}
}

return nil, fmt.Errorf(`cannot convert "%s" to %s`, rhs.String(), lhs.Type())
Expand Down Expand Up @@ -271,6 +311,16 @@ func computeGT(lhs, rhs Expr) (*BoolLiteral, error) {

return &BoolLiteral{Val: l.Val.After(dt)}, nil
}
case *DurationLiteral:
rv, ok := rhs.(*StringLiteral)
if ok {
v, err := time.ParseDuration(rv.Val)
if err != nil {
return nil, err
}

return &BoolLiteral{Val: l.Val > v}, nil
}
}

return nil, fmt.Errorf(`cannot convert "%s" to %s`, rhs.String(), lhs.Type())
Expand Down Expand Up @@ -303,6 +353,16 @@ func computeGTE(lhs, rhs Expr) (*BoolLiteral, error) {

return &BoolLiteral{Val: l.Val.After(dt)}, nil
}
case *DurationLiteral:
rv, ok := rhs.(*StringLiteral)
if ok {
v, err := time.ParseDuration(rv.Val)
if err != nil {
return nil, err
}

return &BoolLiteral{Val: l.Val >= v}, nil
}
}

return nil, fmt.Errorf(`cannot convert "%s" to %s`, rhs.String(), lhs.Type())
Expand Down
130 changes: 130 additions & 0 deletions evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ func TestEvaluator(t *testing.T) {
dt, err := time.Parse(time.RFC3339, "2019-03-28T11:39:43+07:00")
require.NoError(t, err)

dur2m, err := time.ParseDuration("2m")
require.NoError(t, err)

dur1m30s, err := time.ParseDuration("1m30s")
require.NoError(t, err)

dur45s, err := time.ParseDuration("45s")
require.NoError(t, err)

tests := []TestCase{
{
`{ "comparator": "||", "rules": [ { "comparator": "&&", "rules": [ { "var": "a", "op": "==", "val": 1 }, { "var": "b", "op": "==", "val": 2 } ] }, { "comparator": "&&", "rules": [ { "var": "c", "op": "==", "val": 3 }, { "var": "d", "op": "==", "val": 4 } ] } ] }`,
Expand Down Expand Up @@ -342,6 +351,127 @@ func TestEvaluator(t *testing.T) {
},
},
},
{
`{ "var": "a", "op": "==", "val": "1m30s" }`,
[]Evaluation{
{
map[string]interface{}{
"a": dur2m,
},
false,
false,
},
{
map[string]interface{}{
"a": dur1m30s,
},
true,
false,
},
{
map[string]interface{}{
"a": 1,
},
false,
true,
},
},
},
{
`{ "var": "a", "op": "!=", "val": "1m30s" }`,
[]Evaluation{
{
map[string]interface{}{
"a": dur2m,
},
true,
false,
},
{
map[string]interface{}{
"a": dur1m30s,
},
false,
false,
},
},
},
{
`{ "var": "a", "op": ">", "val": "1m30s" }`,
[]Evaluation{
{
map[string]interface{}{
"a": dur2m,
},
true,
false,
},
{
map[string]interface{}{
"a": dur1m30s,
},
false,
false,
},
},
},
{
`{ "var": "a", "op": ">=", "val": "1m30s" }`,
[]Evaluation{
{
map[string]interface{}{
"a": dur2m,
},
true,
false,
},
{
map[string]interface{}{
"a": dur1m30s,
},
true,
false,
},
},
},
{
`{ "var": "a", "op": "<", "val": "1m30s" }`,
[]Evaluation{
{
map[string]interface{}{
"a": dur45s,
},
true,
false,
},
{
map[string]interface{}{
"a": dur2m,
},
false,
false,
},
},
},
{
`{ "var": "a", "op": "<=", "val": "1m30s" }`,
[]Evaluation{
{
map[string]interface{}{
"a": dur1m30s,
},
true,
false,
},
{
map[string]interface{}{
"a": dur2m,
},
false,
false,
},
},
},
}

for _, test := range tests {
Expand Down

0 comments on commit aa219d1

Please sign in to comment.