package models
import (
"encoding/json"
"errors"
"fmt"
"strconv"
)
type Boolean bool
func (st *Boolean) UnmarshalJSON(b []byte) error {
var item interface{}
if err := json.Unmarshal(b, &item); err != nil {
return err
}
switch v := item.(type) {
case bool:
*st = Boolean(v)
case int, uint:
if v == 1 {
*st = true
} else {
*st = false
}
}
return nil
}
func (st *Boolean) Scan(value interface{}) error {
data := 0
switch v := value.(type) {
case []byte:
str, err := strconv.ParseInt(string(v), 10, 8)
if err != nil {
return errors.New(fmt.Sprint("Failed to unmarshal JSON value:", value))
}
data = int(str)
case int64:
data = int(v)
}
if data == 1 {
*st = true
} else {
*st = false
}
return nil
}