feat(Type): add time.Time as a possible type for Scan
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user