forked from mirror/enumer
Implement Scan with a type switch
This is just a minor (and a bit opinionated) tweak in Scan. While using the generated Scan method, realized that what's being returned from any driver I've stumbled upon is actually []byte instead of string, thus always causing a one failed type assertion on Scan.
This commit is contained in:
parent
d46c853929
commit
78ada3fc29
|
@ -1682,14 +1682,16 @@ func (i *Prime) Scan(value interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
str, ok := value.(string)
|
var str string
|
||||||
if !ok {
|
switch v := value.(type) {
|
||||||
bytes, ok := value.([]byte)
|
case []byte:
|
||||||
if !ok {
|
str = string(b)
|
||||||
return fmt.Errorf("value is not a byte slice")
|
case string:
|
||||||
}
|
str = v
|
||||||
|
case fmt.Stringer:
|
||||||
str = string(bytes[:])
|
str = v.String()
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("invalid value of Prime: %[1]T(%[1]v)", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
val, err := PrimeString(str)
|
val, err := PrimeString(str)
|
||||||
|
@ -1867,14 +1869,16 @@ func (i *Prime) Scan(value interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
str, ok := value.(string)
|
var str string
|
||||||
if !ok {
|
switch v := value.(type) {
|
||||||
bytes, ok := value.([]byte)
|
case []byte:
|
||||||
if !ok {
|
str = string(b)
|
||||||
return fmt.Errorf("value is not a byte slice")
|
case string:
|
||||||
}
|
str = v
|
||||||
|
case fmt.Stringer:
|
||||||
str = string(bytes[:])
|
str = v.String()
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("invalid value of Prime: %[1]T(%[1]v)", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
val, err := PrimeString(str)
|
val, err := PrimeString(str)
|
||||||
|
|
18
sql.go
18
sql.go
|
@ -12,14 +12,16 @@ const scanMethod = `func (i *%[1]s) Scan(value interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
str, ok := value.(string)
|
var str string
|
||||||
if !ok {
|
switch v := value.(type) {
|
||||||
bytes, ok := value.([]byte)
|
case []byte:
|
||||||
if !ok {
|
str = string(b)
|
||||||
return fmt.Errorf("value is not a byte slice")
|
case string:
|
||||||
}
|
str = v
|
||||||
|
case fmt.Stringer:
|
||||||
str = string(bytes[:])
|
str = v.String()
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("invalid value of %[1]s: %%[1]T(%%[1]v)", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
val, err := %[1]sString(str)
|
val, err := %[1]sString(str)
|
||||||
|
|
Loading…
Reference in New Issue