-
Notifications
You must be signed in to change notification settings - Fork 110
/
sql.go
40 lines (33 loc) · 677 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
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
}
str, ok := value.(string)
if !ok {
bytes, ok := value.([]byte)
if !ok {
return fmt.Errorf("value is not a byte slice")
}
str = string(bytes[:])
}
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)
}