forked from alvaroloes/enumer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_int.go
45 lines (37 loc) · 808 Bytes
/
sql_int.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
43
44
45
package main
// Arguments to format are:
// [1]: type name
const intValueMethod = `func (i %[1]s) Value() (driver.Value, error) {
return int(i), nil
}
`
const intScanMethod = `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[:])
}
intVal, err := strconv.Atoi(str)
if err != nil {
return err
}
t := %[1]s(intVal)
if !t.IsA%[1]s() {
return fmt.Errorf("%%d does not beling to %[1]s", intVal)
}
*i = t
return nil
}
`
func (g *Generator) addIntValueAndScanMethod(typeName string) {
g.Printf("\n")
g.Printf(intValueMethod, typeName)
g.Printf("\n\n")
g.Printf(intScanMethod, typeName)
}