enumer/sql.go

44 lines
734 B
Go
Raw Normal View History

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) {
return i.string()
}
`
const scanMethod = `func (i *%[1]s) Scan(value interface{}) error {
2016-02-11 21:27:29 +03:00
if value == nil {
return nil
}
var str string
switch v := value.(type) {
case []byte:
2020-03-16 20:52:02 +03:00
str = string(v)
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-01-12 15:05:56 +03:00
val, err := %[1]sString(str)
if err != nil {
return err
}
*i = val
return nil
}
`
2016-02-07 00:27:43 +03:00
func (g *Generator) addValueAndScanMethod(typeName string) {
g.Printf("\n")
2016-02-07 00:27:43 +03:00
g.Printf(valueMethod, typeName)
g.Printf("\n\n")
2016-02-07 00:27:43 +03:00
g.Printf(scanMethod, typeName)
}