I am trying to create a string to bool converstion data type, to further explain what I am trying to achieve I will give an example of a string concat.
The following struct will fail at field 2 and 3 when using gorm, if you debug this it will basically throw an error saying sql: Scan error on column index 2, name "nlfieldTwo": unsupported Scan, storing driver.Value type []uint8 into type *[]string;
type Name struct {
FieldOne *String `json:"fieldOne,omitempty" gorm:"column:nlfieldOne"`
FieldTwo *[]String `json:"fieldTwo,omitempty" gorm:"column:nlfieldTwo"`
FieldThree *[]String `json:"fieldThree,omitempty" gorm:"column:nlfieldThree"`
}
A fix around this is to create your own type
type StringConcat []string
func (s *StringConcat) Scan(value interface{}) error {
str, _ := value.([]byte)
// log.Println(value)
// if !ok {
// return errors.New(fmt.Sprint("Failed to unmarshal value:", value))
// }
*s = strings.Split(string(str), ",")
return nil
}
// Value return json value, implement driver.Valuer interface
func (s StringConcat) Value() (driver.Value, error) {
if len(s) == 0 {
return nil, nil
}
return strings.Join(s, ","), nil
}
and then simply replace FieldTwo *[]String with FieldTwo StringConcat
THE PROBLEM
I am trying to do something similar for a bool type as seen in FieldFour
type Name struct {
FieldOne *String `json:"fieldOne,omitempty" gorm:"column:nlfieldOne"`
FieldTwo *[]String `json:"fieldTwo,omitempty" gorm:"column:nlfieldTwo"`
FieldThree *[]String `json:"fieldThree,omitempty" gorm:"column:nlfieldThree"`
FieldFour *bool `json:"fieldThree,omitempty" gorm:"column:nlfieldFour"`
}
FieldFour is a string type so if I try doing the above it will simply fail and throw an error sql: Scan error on column index 2, name "nlfieldFour": sql/driver: couldn't convert "" into type bool; sql: Scan error on column index 2, name "nlfieldFour": sql/driver: couldn't convert "None" into type bool; sql: Scan error on column index 2, name "nlfieldFour": sql/driver: couldn't convert "Creative" into type bool; ....... and so on
I am trying to convert that to return a bool however, I do not want to check for empty and None string values as shown in the error above "" and "None" everything else I want to return a true if they got a string value or false if they got nothing.
I tried looking into strconv.FormatBool but I could not figure out a way of doing this.