From 5e4241fc875385899a24641a258e9a278810f339 Mon Sep 17 00:00:00 2001 From: zav8 Date: Thu, 6 Dec 2018 16:07:07 +0800 Subject: [PATCH] add support for field of type sql.NullXxx in rawSet.setFieldValue() --- orm/orm_raw.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/orm/orm_raw.go b/orm/orm_raw.go index c8ef4398..08efa4e5 100644 --- a/orm/orm_raw.go +++ b/orm/orm_raw.go @@ -150,8 +150,10 @@ func (o *rawSet) setFieldValue(ind reflect.Value, value interface{}) { case reflect.Struct: if value == nil { ind.Set(reflect.Zero(ind.Type())) - - } else if _, ok := ind.Interface().(time.Time); ok { + return + } + switch indi := ind.Interface().(type) { + case time.Time: var str string switch d := value.(type) { case time.Time: @@ -178,6 +180,26 @@ func (o *rawSet) setFieldValue(ind reflect.Value, value interface{}) { } } } + case sql.NullString: + err := indi.Scan(value) + if err == nil { + ind.Set(reflect.ValueOf(indi)) + } + case sql.NullInt64: + err := indi.Scan(value) + if err == nil { + ind.Set(reflect.ValueOf(indi)) + } + case sql.NullFloat64: + err := indi.Scan(value) + if err == nil { + ind.Set(reflect.ValueOf(indi)) + } + case sql.NullBool: + err := indi.Scan(value) + if err == nil { + ind.Set(reflect.ValueOf(indi)) + } } } }