Skip to content

Commit

Permalink
Merge pull request #57 from wenj91/dev
Browse files Browse the repository at this point in the history
f-fix the nil != nil => true
  • Loading branch information
wenj91 authored Feb 4, 2021
2 parents 7c67715 + 6818c72 commit 0708b4c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 53 deletions.
21 changes: 1 addition & 20 deletions nilable_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 25 additions & 33 deletions proc_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,55 +144,47 @@ 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()
}

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
Expand Down
25 changes: 25 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 0708b4c

Please sign in to comment.