Merge pull request #32 from vladimiroff/scan-type

Implement Scan with a type switch
This commit is contained in:
Dan Markham 2020-03-15 23:18:03 -07:00 committed by GitHub
commit 3100ad138c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 25 deletions

View File

@ -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)

20
sql.go
View File

@ -12,21 +12,23 @@ 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)
if err != nil { if err != nil {
return err return err
} }
*i = val *i = val
return nil return nil
} }