Skip to content

Commit

Permalink
support to convert float32 to other types
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Aug 26, 2024
1 parent 75cdbe0 commit 12ff323
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ func (s nullScanner) Scan(src any) (err error) {
case int64:
*v = time.Duration(s) * time.Millisecond

case float32:
*v = time.Duration(float64(s) * float64(time.Second))

case float64:
*v = time.Duration(s * float64(time.Second))

Expand All @@ -241,6 +244,8 @@ func (s nullScanner) Scan(src any) (err error) {
switch s := src.(type) {
case int64:
*v = s != 0
case float32:
*v = s != 0
case float64:
*v = s != 0
case bool:
Expand All @@ -262,6 +267,9 @@ func (s nullScanner) Scan(src any) (err error) {
case int64:
*v = int(s)

case float32:
*v = int(s)

case float64:
*v = int(s)

Expand Down Expand Up @@ -296,6 +304,9 @@ func (s nullScanner) Scan(src any) (err error) {
case int64:
*v = int8(s)

case float32:
*v = int8(s)

case float64:
*v = int8(s)

Expand Down Expand Up @@ -327,6 +338,9 @@ func (s nullScanner) Scan(src any) (err error) {
case int64:
*v = int16(s)

case float32:
*v = int16(s)

case float64:
*v = int16(s)

Expand Down Expand Up @@ -358,6 +372,9 @@ func (s nullScanner) Scan(src any) (err error) {
case int64:
*v = int32(s)

case float32:
*v = int32(s)

case float64:
*v = int32(s)

Expand Down Expand Up @@ -389,6 +406,9 @@ func (s nullScanner) Scan(src any) (err error) {
case int64:
*v = s

case float32:
*v = int64(s)

case float64:
*v = int64(s)

Expand Down Expand Up @@ -417,6 +437,9 @@ func (s nullScanner) Scan(src any) (err error) {
case int64:
*v = uint(s)

case float32:
*v = uint(s)

case float64:
*v = uint(s)

Expand Down Expand Up @@ -451,6 +474,9 @@ func (s nullScanner) Scan(src any) (err error) {
case int64:
*v = uint8(s)

case float32:
*v = uint8(s)

case float64:
*v = uint8(s)

Expand Down Expand Up @@ -482,6 +508,9 @@ func (s nullScanner) Scan(src any) (err error) {
case int64:
*v = uint16(s)

case float32:
*v = uint16(s)

case float64:
*v = uint16(s)

Expand Down Expand Up @@ -513,6 +542,9 @@ func (s nullScanner) Scan(src any) (err error) {
case int64:
*v = uint32(s)

case float32:
*v = uint32(s)

case float64:
*v = uint32(s)

Expand Down Expand Up @@ -544,6 +576,9 @@ func (s nullScanner) Scan(src any) (err error) {
case int64:
*v = uint64(s)

case float32:
*v = uint64(s)

case float64:
*v = uint64(s)

Expand Down Expand Up @@ -572,6 +607,9 @@ func (s nullScanner) Scan(src any) (err error) {
case int64:
*v = float32(s)

case float32:
*v = s

case float64:
*v = float32(s)

Expand Down Expand Up @@ -603,6 +641,9 @@ func (s nullScanner) Scan(src any) (err error) {
case int64:
*v = float64(s)

case float32:
*v = float64(s)

case float64:
*v = s

Expand All @@ -628,6 +669,9 @@ func (s nullScanner) Scan(src any) (err error) {
case int64:
*v = strconv.FormatInt(s, 10)

case float32:
*v = strconv.FormatFloat(float64(s), 'f', -1, 64)

case float64:
*v = strconv.FormatFloat(s, 'f', -1, 64)

Expand Down Expand Up @@ -669,6 +713,10 @@ func toTime(src any, loc *time.Location) (time.Time, error) {
case int64:
return time.Unix(s, 0).In(loc), nil

case float32:
int, frac := math.Modf(float64(s))
return time.Unix(int64(int), int64(frac*1000000000)).In(loc), nil

case float64:
int, frac := math.Modf(s)
return time.Unix(int64(int), int64(frac*1000000000)).In(loc), nil
Expand Down

0 comments on commit 12ff323

Please sign in to comment.