enumer/sql.go

41 lines
677 B
Go
Raw Permalink 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
}
str, ok := value.(string)
if !ok {
bytes, ok := value.([]byte)
if !ok {
return fmt.Errorf("value is not a byte slice")
}
str = string(bytes[:])
}
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)
}