db/types.go
2023-04-12 15:58:25 +08:00

44 lines
634 B
Go

package db
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
type Strings []string
func (c *Strings) Scan(value any) error {
if value == nil {
return nil
}
return json.Unmarshal(value.([]byte), c)
}
func (c Strings) Value() (driver.Value, error) {
b, err := json.Marshal(c)
return string(b), err
}
type NullString string
func (this *NullString) Scan(value any) error {
if value == nil {
return nil
}
switch s := value.(type) {
case []byte:
*this = NullString(s)
default:
*this = NullString(fmt.Sprintf("%v", s))
}
return nil
}
func (c NullString) Value() (driver.Value, error) {
return c, nil
}