feat(Type): add time.Time as a possible type for Scan

This commit is contained in:
2024-12-31 17:58:13 +01:00
parent 1ab0570883
commit b5fc0df93b
2 changed files with 34 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import (
"database/sql"
"errors"
"fmt"
"time"
Assert "github.com/lbatuska/goutils/assert"
)
@@ -230,6 +231,23 @@ func (res *Result[T]) Scan(src interface{}) error {
}
res.err = mismatchErr
return res.err
case *time.Time:
if t, ok := src.(time.Time); ok {
*v = t
res.err = nil
return nil
}
if b, ok := src.([]byte); ok {
parsedTime, err := time.Parse(time.RFC3339, string(b)) // or use other formats as necessary
if err == nil {
*v = parsedTime
res.err = nil
return nil
}
}
res.err = mismatchErr
return res.err
}
// We couldnt parse the value
err := fmt.Errorf("Unsupported type %T, and the type doesn't implement sql.Scanner!", src)