Skip to content

Commit 57b74a2

Browse files
committed
add date in greater operator
1 parent 80f3745 commit 57b74a2

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

querybuilder/operator/greater.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package operator
22

3+
import "time"
4+
35
func init() {
46
AddOperator(Greater)
57
}
@@ -19,6 +21,10 @@ var Greater = &Operator{
1921
return input.(int) > value.(int)
2022
case string:
2123
return len(input.(string)) > len(value.(string))
24+
case time.Time:
25+
i := input.(time.Time)
26+
v := value.(time.Time)
27+
return i.After(v)
2228
default:
2329
return false
2430
}

querybuilder/operator/greater_test.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package operator
22

3-
import "testing"
3+
import (
4+
"testing"
5+
"time"
6+
)
47

58
func TestGreater(t *testing.T) {
69
var inputs = []struct {
@@ -17,12 +20,18 @@ func TestGreater(t *testing.T) {
1720
{input: float64(2.4), value: float64(2.5), want: false, kind: "float64"},
1821
{input: "mystring", value: "string", want: true, kind: "string"},
1922
{input: "mystring", value: "myUpstring", want: false, kind: "string"},
23+
{input: time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), value: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), want: true, kind: "date"},
24+
{input: time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), value: time.Date(2000, 1, 3, 0, 0, 0, 0, time.UTC), want: false, kind: "date"},
25+
{input: time.Date(0001, 1, 1, 15, 10, 7, 0, time.UTC), value: time.Date(0001, 1, 1, 15, 10, 6, 0, time.UTC), want: true, kind: "time"},
26+
{input: time.Date(0001, 1, 1, 15, 10, 7, 0, time.UTC), value: time.Date(0001, 1, 1, 15, 10, 8, 0, time.UTC), want: false, kind: "time"},
2027
}
2128

2229
for _, input := range inputs {
23-
got := Greater.Evaluate(input.input, input.value)
24-
if got != input.want {
25-
t.Errorf("%s - %v greater %v got: %t, want: %t", input.kind, input.input, input.value, got, input.want)
26-
}
30+
t.Run(input.kind, func(t *testing.T) {
31+
got := Greater.Evaluate(input.input, input.value)
32+
if got != input.want {
33+
t.Errorf("%s - %v greater %v got: %t, want: %t", input.kind, input.input, input.value, got, input.want)
34+
}
35+
})
2736
}
2837
}

querybuilder/type_convert.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,34 +77,34 @@ func toBoolean(v interface{}) bool {
7777
}
7878

7979
// Date
80-
func toDate(v interface{}) *time.Time {
80+
func toDate(v interface{}) time.Time {
8181
switch v.(type) {
8282
case string:
8383
t, _ := time.Parse(DATE_ISO_8601, v.(string))
84-
return &t
84+
return t
8585
default:
86-
return &time.Time{}
86+
return time.Time{}
8787
}
8888
}
8989

9090
// Time
91-
func toTime(v interface{}) *time.Time {
91+
func toTime(v interface{}) time.Time {
9292
switch v.(type) {
9393
case string:
9494
t, _ := time.Parse(TIME_ISO_8601, v.(string))
95-
return &t
95+
return t
9696
default:
97-
return &time.Time{}
97+
return time.Time{}
9898
}
9999
}
100100

101101
// DateTime
102-
func toDateTime(v interface{}) *time.Time {
102+
func toDateTime(v interface{}) time.Time {
103103
switch v.(type) {
104104
case string:
105105
t, _ := time.Parse(DATE_TIME_ISO_8601, v.(string))
106-
return &t
106+
return t
107107
default:
108-
return &time.Time{}
108+
return time.Time{}
109109
}
110110
}

0 commit comments

Comments
 (0)