enumer/sql.go

38 lines
633 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(), nil
}
`
const scanMethod = `func (i *%[1]s) Scan(value interface{}) error {
2016-02-11 21:27:29 +03:00
if value == nil {
return nil
}
2016-01-12 15:05:56 +03:00
bytes, ok := value.([]byte)
if !ok {
2016-02-02 17:40:25 +03:00
return fmt.Errorf("value is not a byte slice")
}
2016-01-12 15:05:56 +03:00
str := string(bytes[:])
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)
}