Skip to content

Commit a0d6ff1

Browse files
time.Time, []byte type add alias support. (rebase master) (#4992)
* time.Time, []byte type add alias support * reformat
1 parent eae7362 commit a0d6ff1

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

schema/field.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
346346
}
347347
}
348348

349-
if _, ok := field.TagSettings["EMBEDDED"]; ok || (fieldStruct.Anonymous && !isValuer && (field.Creatable || field.Updatable || field.Readable)) {
349+
if _, ok := field.TagSettings["EMBEDDED"]; field.GORMDataType != Time && field.GORMDataType != Bytes &&
350+
(ok || (fieldStruct.Anonymous && !isValuer && (field.Creatable || field.Updatable || field.Readable))) {
350351
kind := reflect.Indirect(fieldValue).Kind()
351352
switch kind {
352353
case reflect.Struct:

schema/field_test.go

+22-15
Original file line numberDiff line numberDiff line change
@@ -262,21 +262,24 @@ func TestParseFieldWithPermission(t *testing.T) {
262262
}
263263

264264
type (
265-
ID int64
266-
INT int
267-
INT8 int8
268-
INT16 int16
269-
INT32 int32
270-
INT64 int64
271-
UINT uint
272-
UINT8 uint8
273-
UINT16 uint16
274-
UINT32 uint32
275-
UINT64 uint64
276-
FLOAT32 float32
277-
FLOAT64 float64
278-
BOOL bool
279-
STRING string
265+
ID int64
266+
INT int
267+
INT8 int8
268+
INT16 int16
269+
INT32 int32
270+
INT64 int64
271+
UINT uint
272+
UINT8 uint8
273+
UINT16 uint16
274+
UINT32 uint32
275+
UINT64 uint64
276+
FLOAT32 float32
277+
FLOAT64 float64
278+
BOOL bool
279+
STRING string
280+
TIME time.Time
281+
BYTES []byte
282+
280283
TypeAlias struct {
281284
ID
282285
INT `gorm:"column:fint"`
@@ -293,6 +296,8 @@ type (
293296
FLOAT64 `gorm:"column:ffloat64"`
294297
BOOL `gorm:"column:fbool"`
295298
STRING `gorm:"column:fstring"`
299+
TIME `gorm:"column:ftime"`
300+
BYTES `gorm:"column:fbytes"`
296301
}
297302
)
298303

@@ -318,6 +323,8 @@ func TestTypeAliasField(t *testing.T) {
318323
{Name: "FLOAT64", DBName: "ffloat64", BindNames: []string{"FLOAT64"}, DataType: schema.Float, Creatable: true, Updatable: true, Readable: true, Size: 64, Tag: `gorm:"column:ffloat64"`},
319324
{Name: "BOOL", DBName: "fbool", BindNames: []string{"BOOL"}, DataType: schema.Bool, Creatable: true, Updatable: true, Readable: true, Tag: `gorm:"column:fbool"`},
320325
{Name: "STRING", DBName: "fstring", BindNames: []string{"STRING"}, DataType: schema.String, Creatable: true, Updatable: true, Readable: true, Tag: `gorm:"column:fstring"`},
326+
{Name: "TIME", DBName: "ftime", BindNames: []string{"TIME"}, DataType: schema.Time, Creatable: true, Updatable: true, Readable: true, Tag: `gorm:"column:ftime"`},
327+
{Name: "BYTES", DBName: "fbytes", BindNames: []string{"BYTES"}, DataType: schema.Bytes, Creatable: true, Updatable: true, Readable: true, Tag: `gorm:"column:fbytes"`},
321328
}
322329

323330
for _, f := range fields {

statement.go

+3
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) {
232232
case reflect.Slice, reflect.Array:
233233
if rv.Len() == 0 {
234234
writer.WriteString("(NULL)")
235+
} else if rv.Type().Elem() == reflect.TypeOf(uint8(0)) {
236+
stmt.Vars = append(stmt.Vars, v)
237+
stmt.DB.Dialector.BindVarTo(writer, stmt, v)
235238
} else {
236239
writer.WriteByte('(')
237240
for i := 0; i < rv.Len(); i++ {

0 commit comments

Comments
 (0)