45 lines
683 B
Go
45 lines
683 B
Go
package base_model
|
|
|
|
import (
|
|
"fmt"
|
|
"encoding/json"
|
|
"database/sql/driver"
|
|
)
|
|
|
|
type StringArray []string
|
|
|
|
func (s StringArray) Value() (driver.Value, error) {
|
|
if len(s) == 0 {
|
|
return "[]", nil // empty array
|
|
}
|
|
return json.Marshal(s)
|
|
}
|
|
|
|
func (s *StringArray) Scan(value interface{}) error {
|
|
if value == nil {
|
|
*s = StringArray{}
|
|
return nil
|
|
}
|
|
|
|
var bytes []byte
|
|
switch v := value.(type) {
|
|
case []byte:
|
|
bytes = v
|
|
case string:
|
|
bytes = []byte(v)
|
|
default:
|
|
return fmt.Errorf("unsupported type: %T", value)
|
|
}
|
|
|
|
if len(bytes) == 0 {
|
|
*s = StringArray{}
|
|
return nil
|
|
}
|
|
|
|
if string(bytes) == "[]" {
|
|
*s = StringArray{}
|
|
return nil
|
|
}
|
|
|
|
return json.Unmarshal(bytes, s)
|
|
} |