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.
This commit is contained in:
Hein
2026-01-13 15:09:56 +02:00
parent 276854768e
commit 13009167cf

View File

@@ -74,6 +74,10 @@ func (n *SqlNull[T]) Scan(value any) error {
return n.FromString(v)
case []byte:
return n.FromString(string(v))
case float32, float64:
return n.FromString(fmt.Sprintf("%f", value))
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return n.FromString(fmt.Sprintf("%d", value))
default:
return n.FromString(fmt.Sprintf("%v", value))
}
@@ -94,6 +98,10 @@ func (n *SqlNull[T]) FromString(s string) error {
reflect.ValueOf(&n.Val).Elem().SetInt(i)
n.Valid = true
}
if f, err := strconv.ParseFloat(s, 64); err == nil {
reflect.ValueOf(&n.Val).Elem().SetInt(int64(f))
n.Valid = true
}
case float32, float64:
if f, err := strconv.ParseFloat(s, 64); err == nil {
reflect.ValueOf(&n.Val).Elem().SetFloat(f)