Skip to content

Commit a980201

Browse files
author
Hein
committed
feat(spectypes): ✨ enhance SqlNull to support float and int types
* Add handling for float32 and float64 in Scan method. * Implement parsing for integer types in Scan and FromString methods. * Improve flexibility of SqlNull for various numeric inputs.
1 parent 2768547 commit a980201

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

pkg/spectypes/sql_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ func (n *SqlNull[T]) Scan(value any) error {
7474
return n.FromString(v)
7575
case []byte:
7676
return n.FromString(string(v))
77+
case float32, float64:
78+
return n.FromString(fmt.Sprintf("%f", value))
79+
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
80+
return n.FromString(fmt.Sprintf("%d", value))
7781
default:
7882
return n.FromString(fmt.Sprintf("%v", value))
7983
}
@@ -94,6 +98,10 @@ func (n *SqlNull[T]) FromString(s string) error {
9498
reflect.ValueOf(&n.Val).Elem().SetInt(i)
9599
n.Valid = true
96100
}
101+
if f, err := strconv.ParseFloat(s, 64); err == nil {
102+
reflect.ValueOf(&n.Val).Elem().SetInt(int64(f))
103+
n.Valid = true
104+
}
97105
case float32, float64:
98106
if f, err := strconv.ParseFloat(s, 64); err == nil {
99107
reflect.ValueOf(&n.Val).Elem().SetFloat(f)

0 commit comments

Comments
 (0)