forked from dmarkham/enumer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
42 lines (36 loc) · 736 Bytes
/
sql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
// Arguments to format are:
// [1]: type name
const valueMethod = `func (i %[1]s) Value() (driver.Value, error) {
return i.String(), nil
}
`
const scanMethod = `func (i *%[1]s) Scan(value interface{}) error {
if value == nil {
return nil
}
var str string
switch v := value.(type) {
case []byte:
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)
}
val, err := %[1]sString(str)
if err != nil {
return err
}
*i = val
return nil
}
`
func (g *Generator) addValueAndScanMethod(typeName string) {
g.Printf("\n")
g.Printf(valueMethod, typeName)
g.Printf("\n\n")
g.Printf(scanMethod, typeName)
}