Skip to content

Commit

Permalink
Merge pull request #245 from mdouchement/feat-time_pointer_comparison
Browse files Browse the repository at this point in the history
Added *time.Time comparison for q
  • Loading branch information
asdine authored Feb 16, 2019
2 parents 39cfdfc + 68fed07 commit fe89819
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
17 changes: 16 additions & 1 deletion q/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,22 @@ func compare(a, b interface{}, tok token.Token) bool {
}
}

if reflect.TypeOf(a).String() == "time.Time" && reflect.TypeOf(b).String() == "time.Time" {
typea, typeb := reflect.TypeOf(a), reflect.TypeOf(b)

if typea != nil && (typea.String() == "time.Time" || typea.String() == "*time.Time") &&
typeb != nil && (typeb.String() == "time.Time" || typeb.String() == "*time.Time") {

if typea.String() == "*time.Time" && vala.IsNil() {
return true
}

if typeb.String() == "*time.Time" {
if valb.IsNil() {
return true
}
valb = valb.Elem()
}

var x, y int64
x = 1
if vala.MethodByName("Equal").Call([]reflect.Value{valb})[0].Bool() {
Expand Down
19 changes: 19 additions & 0 deletions q/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package q

import (
"go/token"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -36,9 +37,27 @@ func TestCompare(t *testing.T) {
t1 := time.Now()
t2 := t1.Add(2 * time.Hour)
t3 := t1.Add(-2 * time.Hour)
tnil := reflect.Zero(reflect.TypeOf(&t1)).Interface()
require.True(t, compare(t1, t1, token.EQL))
require.True(t, compare(t1, t2, token.LSS))
require.True(t, compare(t1, t3, token.GTR))
require.True(t, compare(&t1, t1, token.EQL))
require.True(t, compare(&t1, t2, token.LSS))
require.True(t, compare(&t1, t3, token.GTR))
require.True(t, compare(&t1, &t1, token.EQL))
require.True(t, compare(&t1, &t2, token.LSS))
require.True(t, compare(&t1, &t3, token.GTR))
require.True(t, compare(t1, &t1, token.EQL))
require.True(t, compare(t1, &t2, token.LSS))
require.True(t, compare(t1, &t3, token.GTR))
require.True(t, compare(tnil, t1, token.EQL))
require.True(t, compare(tnil, t2, token.LSS))
require.True(t, compare(tnil, t3, token.GTR))
require.True(t, compare(t1, tnil, token.EQL))
require.True(t, compare(t1, tnil, token.LSS))
require.True(t, compare(t1, tnil, token.GTR))
require.False(t, compare(nil, t1, token.EQL))
require.False(t, compare(t1, nil, token.EQL))
require.False(t, compare(&A{Name: "John"}, t1, token.EQL))
require.False(t, compare(&A{Name: "John"}, t1, token.LEQ))
require.True(t, compare(uint32(10), uint32(5), token.GTR))
Expand Down

0 comments on commit fe89819

Please sign in to comment.