diff --git a/nilable_structs.go b/nilable_structs.go index e77663a..8ce06f4 100644 --- a/nilable_structs.go +++ b/nilable_structs.go @@ -2,29 +2,10 @@ package gobatis import ( "database/sql" - "database/sql/driver" - "time" ) type NullBool = sql.NullBool type NullFloat64 = sql.NullFloat64 type NullInt64 = sql.NullInt64 type NullString = sql.NullString - -// There is implementation of this in lib/pg -type NullTime struct { - Time time.Time - Valid bool // Valid is true if Time is not NULL -} -// Scan implements the Scanner interface. -func (nt *NullTime) Scan(value interface{}) error { - nt.Time, nt.Valid = value.(time.Time) - return nil -} -// Value implements the driver Valuer interface. -func (nt NullTime) Value() (driver.Value, error) { - if !nt.Valid { - return nil, nil - } - return nt.Time, nil -} +type NullTime = sql.NullTime diff --git a/proc_params.go b/proc_params.go index b64410d..674070a 100644 --- a/proc_params.go +++ b/proc_params.go @@ -144,6 +144,10 @@ func structToMap(s interface{}) map[string]interface{} { func fieldToVal(field interface{}) (interface{}, bool) { objVal := reflect.ValueOf(field) + if objVal.IsNil() { + return nil, false + } + if objVal.Kind() == reflect.Ptr { objVal = objVal.Elem() } @@ -151,48 +155,36 @@ func fieldToVal(field interface{}) (interface{}, bool) { tp := objVal.Type() switch tp.Name() { case "Time": - if nil != field { - return field.(time.Time).Format("2006-01-02 15:04:05"), true - } + return field.(time.Time).Format("2006-01-02 15:04:05"), true case "NullString": - if nil != field { - ns := field.(NullString) - if ns.Valid { - str, _ := ns.Value() - return str, true - } + ns := field.(NullString) + if ns.Valid { + str, _ := ns.Value() + return str, true } case "NullInt64": - if nil != field { - ni64 := field.(NullInt64) - if ni64.Valid { - i, _ := ni64.Value() - return i, true - } + ni64 := field.(NullInt64) + if ni64.Valid { + i, _ := ni64.Value() + return i, true } case "NullBool": - if nil != field { - nb := field.(NullBool) - if nb.Valid { - b, _ := nb.Value() - return b, true - } + nb := field.(NullBool) + if nb.Valid { + b, _ := nb.Value() + return b, true } case "NullFloat64": - if nil != field { - nf := field.(NullFloat64) - if nf.Valid { - f, _ := nf.Value() - return f, true - } + nf := field.(NullFloat64) + if nf.Valid { + f, _ := nf.Value() + return f, true } case "NullTime": - if nil != field { - nt := field.(NullTime) - if nt.Valid { - t, _ := nt.Value() - return t.(time.Time).Format("2006-01-02 15:04:05"), true - } + nt := field.(NullTime) + if nt.Valid { + t, _ := nt.Value() + return t.(time.Time).Format("2006-01-02 15:04:05"), true } default: return field, true diff --git a/util.go b/util.go index 27e9886..e32b91d 100644 --- a/util.go +++ b/util.go @@ -2,6 +2,31 @@ package gobatis import "time" +// PI64 to NullInt64 +func PI64(i int64) *int64 { + return &i +} + +// PS to NullString +func PS(s string) *string { + return &s +} + +// PF64 to NullFloat64 +func PF64(f float64) *float64 { + return &f +} + +// PT to NullTime +func PT(t time.Time) *time.Time { + return &t +} + +// NB to NullBool +func PB(b bool) *bool { + return &b +} + // NI64 to NullInt64 func NI64(i int64) NullInt64 { return NullInt64{Int64: i, Valid: true}