39 lines
764 B
Go
39 lines
764 B
Go
package entities
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"database/sql/driver"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
)
|
||
|
|
|
||
|
|
// StringSlice is a custom type for []string that implements sql.Scanner and driver.Valuer
|
||
|
|
type StringSlice []string
|
||
|
|
|
||
|
|
// Scan implements the sql.Scanner interface for StringSlice
|
||
|
|
func (s *StringSlice) Scan(value interface{}) error {
|
||
|
|
if value == nil {
|
||
|
|
*s = StringSlice{}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
var bytes []byte
|
||
|
|
switch v := value.(type) {
|
||
|
|
case []byte:
|
||
|
|
bytes = v
|
||
|
|
case string:
|
||
|
|
bytes = []byte(v)
|
||
|
|
default:
|
||
|
|
return fmt.Errorf("cannot scan %T into StringSlice", value)
|
||
|
|
}
|
||
|
|
|
||
|
|
return json.Unmarshal(bytes, s)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Value implements the driver.Valuer interface for StringSlice
|
||
|
|
func (s StringSlice) Value() (driver.Value, error) {
|
||
|
|
if s == nil {
|
||
|
|
return nil, nil
|
||
|
|
}
|
||
|
|
return json.Marshal(s)
|
||
|
|
}
|