enumer/sql.go

34 lines
577 B
Go
Raw Normal View History

package main
// Arguments to format are:
// [1]: type name
const valuer = `func (i %[1]s) Value() (driver.Value, error) {
return i.String(), nil
}
`
const scanner = `func (i %[1]s) Scan(value interface{}) error {
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
}
`
func (g *Generator) addValuerAndScanner(typeName string) {
g.Printf("\n")
g.Printf(valuer, typeName)
g.Printf("\n\n")
g.Printf(scanner, typeName)
}