2016-01-12 14:37:51 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
// Arguments to format are:
|
2023-04-10 22:37:44 +03:00
|
|
|
//
|
2016-01-12 14:37:51 +03:00
|
|
|
// [1]: type name
|
2016-02-07 00:27:43 +03:00
|
|
|
const valueMethod = `func (i %[1]s) Value() (driver.Value, error) {
|
2023-04-10 22:37:44 +03:00
|
|
|
return i.string()
|
2016-01-12 14:37:51 +03:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-05 17:18:52 +03:00
|
|
|
var str string
|
|
|
|
switch v := value.(type) {
|
|
|
|
case []byte:
|
2020-03-16 20:52:02 +03:00
|
|
|
str = string(v)
|
2020-03-05 17:18:52 +03:00
|
|
|
case string:
|
|
|
|
str = v
|
|
|
|
case fmt.Stringer:
|
|
|
|
str = v.String()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid value of %[1]s: %%[1]T(%%[1]v)", value)
|
2016-10-21 16:20:44 +03:00
|
|
|
}
|
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
|
|
|
|
}
|
2020-03-05 17:18:52 +03:00
|
|
|
|
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
|
|
|
}
|