-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtypes.go
96 lines (87 loc) · 2.12 KB
/
types.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"fmt"
"log"
)
const (
tpl = `type %s struct {
%s}
`
)
var (
// key - pg type, value - go type
nonNullableTypes map[string]string = map[string]string{
"int2": "int16",
"int4": "int32",
"int8": "int64",
"float4": "float32",
"float8": "float64",
"numeric": "float64",
"money": "float64",
"bpchar": "string",
"varchar": "string",
"text": "string",
"bytea": "[]byte",
"uuid": "uuid.UUID",
"timestamp": "time.Time",
"timestamptz": "time.Time",
"time": "time.Time",
"timetz": "time.Time",
"date": "time.Time",
"interval": "time.Time",
"bool": "bool",
"bit": "uint32",
"varbit": "uint32",
"json": "struct{...}",
"xml": "struct{...}",
}
nullableTypes map[string]string = map[string]string{
"int2": "null.Int",
"int4": "null.Int",
"int8": "null.Int",
"float4": "null.Float",
"float8": "null.Float",
"numeric": "null.Float",
"money": "null.Float",
"bpchar": "null.String",
"varchar": "null.String",
"text": "null.String",
"bytea": "*[]byte",
"uuid": "*uuid.UUID",
"timestamp": "null.Time",
"timestamptz": "null.Time",
"time": "null.Time",
"timetz": "null.Time",
"date": "null.Time",
"interval": "null.Time",
"bool": "null.Bool",
"bit": "*uint32",
"varbit": "*uint32",
"json": "*struct{...}",
"xml": "*struct{...}",
}
)
func getStruct(tab string, cols []*column) string {
var body string
for _, c := range cols {
body += fmt.Sprintf("\t%s %s `db:%q json:%[4]q` // sqltype: %s\n",
snake2Camel(c.Name), convertType(c), c.Name, snake2CamelLower(c.Name), c.Type)
}
return fmt.Sprintf(tpl, snake2Camel(tab), body)
}
func getMap(n bool) map[string]string {
if n {
return nullableTypes
}
return nonNullableTypes
}
func convertType(c *column) string {
t, ok := getMap(c.IsNull)[c.Type]
if !ok {
log.Fatalln("unknown type: ", t)
}
if c.IsArray {
return "[]" + t
}
return t
}