Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

Commit

Permalink
add support custom type Nullfloat64 (#1450)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored Oct 5, 2019
1 parent 57a49c6 commit 33fc33b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions session_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func (session *Session) byte2Time(col *core.Column, data []byte) (outTime time.T
return session.str2Time(col, string(data))
}

var (
nullFloatType = reflect.TypeOf(sql.NullFloat64{})
)

// convert a db data([]byte) to a field value
func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value, data []byte) error {
if structConvert, ok := fieldValue.Addr().Interface().(core.Conversion); ok {
Expand Down Expand Up @@ -583,6 +587,12 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val
t := fieldValue.Convert(core.TimeType).Interface().(time.Time)
tf := session.engine.formatColTime(col, t)
return tf, nil
} else if fieldType.ConvertibleTo(nullFloatType) {
t := fieldValue.Convert(nullFloatType).Interface().(sql.NullFloat64)
if !t.Valid {
return nil, nil
}
return t.Float64, nil
}

if !col.SQLType.IsJson() {
Expand Down
22 changes: 22 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package xorm

import (
"database/sql"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -21,3 +22,24 @@ func TestClose(t *testing.T) {
sess2.Close()
assert.True(t, sess2.IsClosed())
}

func TestNullFloatStruct(t *testing.T) {
type MyNullFloat64 sql.NullFloat64

type MyNullFloatStruct struct {
Uuid string
Amount MyNullFloat64
}

assert.NoError(t, prepareEngine())
assert.NoError(t, testEngine.Sync2(new(MyNullFloatStruct)))

_, err := testEngine.Insert(&MyNullFloatStruct{
Uuid: "111111",
Amount: MyNullFloat64(sql.NullFloat64{
Float64: 0.1111,
Valid: true,
}),
})
assert.NoError(t, err)
}

0 comments on commit 33fc33b

Please sign in to comment.