Skip to content

Commit 7e9e00c

Browse files
fix issue where time.Time is considered a GORM model
1 parent 8be0b71 commit 7e9e00c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

gorm/utilities.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
jgorm "github.com/jinzhu/gorm"
1313
"github.com/jinzhu/inflection"
1414

15+
"time"
16+
1517
"github.com/infobloxopen/atlas-app-toolkit/rpc/resource"
1618
)
1719

@@ -122,7 +124,7 @@ func indirectType(t reflect.Type) reflect.Type {
122124
func isModel(t reflect.Type) bool {
123125
kind := t.Kind()
124126
_, isValuer := reflect.Zero(t).Interface().(driver.Valuer)
125-
if (kind == reflect.Struct || kind == reflect.Slice) && !isValuer {
127+
if (kind == reflect.Struct || kind == reflect.Slice) && !isValuer && t != reflect.TypeOf(time.Time{}) {
126128
return true
127129
}
128130
return false

gorm/utilities_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package gorm
22

33
import (
44
"context"
5+
"reflect"
56
"testing"
67

8+
"time"
9+
710
"github.com/stretchr/testify/assert"
811
)
912

@@ -46,3 +49,19 @@ func TestHandleFieldPath(t *testing.T) {
4649
}
4750
}
4851
}
52+
53+
func TestIsModel(t *testing.T) {
54+
for tName, tCase := range map[string]struct {
55+
input reflect.Type
56+
want bool
57+
}{
58+
"time.Time": {reflect.TypeOf(time.Time{}), false},
59+
"*time.Time": {reflect.TypeOf(&time.Time{}), false},
60+
} {
61+
t.Run(tName, func(t *testing.T) {
62+
if got, want := isModel(tCase.input), tCase.want; got != want {
63+
t.Errorf("got %v; want %v", got, want)
64+
}
65+
})
66+
}
67+
}

0 commit comments

Comments
 (0)