2016-01-12 14:37:51 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
// Arguments to format are:
|
|
|
|
// [1]: type name
|
2016-02-07 00:27:43 +03:00
|
|
|
const valueMethod = `func (i %[1]s) Value() (driver.Value, error) {
|
2016-01-12 14:37:51 +03:00
|
|
|
return i.String(), nil
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-02-09 15:14:00 +03:00
|
|
|
const scanMethod = `func (i *%[1]s) Scan(value interface{}) error {
|
2016-02-11 21:27:29 +03:00
|
|
|
if value == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-21 16:20:44 +03:00
|
|
|
str, ok := value.(string)
|
2016-01-12 14:37:51 +03:00
|
|
|
if !ok {
|
2016-10-21 16:20:44 +03:00
|
|
|
bytes, ok := value.([]byte)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("value is not a byte slice")
|
|
|
|
}
|
2016-01-12 14:37:51 +03:00
|
|
|
|
2016-10-21 16:20:44 +03:00
|
|
|
str = string(bytes[:])
|
|
|
|
}
|
2016-01-12 15:05:56 +03:00
|
|
|
|
2016-01-12 14:37:51 +03:00
|
|
|
val, err := %[1]sString(str)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-09 15:14:00 +03:00
|
|
|
*i = val
|
2016-01-12 14:37:51 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-02-07 00:27:43 +03:00
|
|
|
func (g *Generator) addValueAndScanMethod(typeName string) {
|
2016-01-12 14:37:51 +03:00
|
|
|
g.Printf("\n")
|
2016-02-07 00:27:43 +03:00
|
|
|
g.Printf(valueMethod, typeName)
|
2016-01-12 14:37:51 +03:00
|
|
|
g.Printf("\n\n")
|
2016-02-07 00:27:43 +03:00
|
|
|
g.Printf(scanMethod, typeName)
|
2016-01-12 14:37:51 +03:00
|
|
|
}
|