feat(Type): add time.Time as a possible type for Scan
This commit is contained in:
@@ -3,6 +3,7 @@ package Type
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
Assert "github.com/lbatuska/goutils/assert"
|
Assert "github.com/lbatuska/goutils/assert"
|
||||||
)
|
)
|
||||||
@@ -201,6 +202,21 @@ func (opt *Optional[T]) Scan(src interface{}) error {
|
|||||||
opt.present = true
|
opt.present = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case *time.Time:
|
||||||
|
if t, ok := src.(time.Time); ok {
|
||||||
|
*v = t
|
||||||
|
opt.present = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if b, ok := src.([]byte); ok {
|
||||||
|
parsedTime, err := time.Parse(time.RFC3339, string(b))
|
||||||
|
if err == nil {
|
||||||
|
*v = parsedTime
|
||||||
|
opt.present = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// We couldnt parse the value
|
// We couldnt parse the value
|
||||||
opt.present = false
|
opt.present = false
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
Assert "github.com/lbatuska/goutils/assert"
|
Assert "github.com/lbatuska/goutils/assert"
|
||||||
)
|
)
|
||||||
@@ -230,6 +231,23 @@ func (res *Result[T]) Scan(src interface{}) error {
|
|||||||
}
|
}
|
||||||
res.err = mismatchErr
|
res.err = mismatchErr
|
||||||
return res.err
|
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
|
// We couldnt parse the value
|
||||||
err := fmt.Errorf("Unsupported type %T, and the type doesn't implement sql.Scanner!", src)
|
err := fmt.Errorf("Unsupported type %T, and the type doesn't implement sql.Scanner!", src)
|
||||||
|
|||||||
Reference in New Issue
Block a user