-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_mappings.go
297 lines (268 loc) · 5.72 KB
/
generate_mappings.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"reflect"
"strings"
"text/template"
"github.com/Masterminds/sprig"
"github.com/jackc/pgtype"
"github.com/lawrencejones/pgsink/pkg/decode"
)
/*
pgconn and pgtype have a good list of Postgres type parsers, and map them against name and
OID. This file uses the pgx implementation to generate a list of supported mappings for
pgsink.
If a type is supported, that means we can parse it into a native format. Postgres text
arrays, for example, are parsed into Golang []string.
Decoders can be configured to a type as a fallback mechanism, such as parsing the plain
text format of a Postgres column in the situation where it would otherwise be unreadable.
Whether this is desirable will depend on how pgsink is being used, and is why it should be
configurable outside of the mapping package.
Run go generate ./... to invoke this file, and modify the output by adjusting the
configuration struct.
*/
type TemplatedTypeMapping struct {
Name string
OID uint32
Scanner string
Empty string
}
type Config struct {
Unsupported []string
Templates map[string]TemplatedTypeMapping
}
func init() {
for _, name := range cfg.Unsupported {
if _, ok := cfg.Templates[name]; ok {
log.Fatalf("type '%s' is both supported and unsupported", name)
}
}
}
var cfg = Config{
Unsupported: []string{
"tid",
"point",
"lseg",
"path",
"box",
"polygon",
"line",
"_cidr",
"circle",
"macaddr",
"inet",
"_bytea",
"_bpchar",
"aclitem",
"_aclitem",
"_inet",
"bpchar",
"interval",
"bit",
"varbit",
"uuid",
"_uuid",
"int4range",
"numrange",
"tsrange",
"tstzrange",
"daterange",
"int8range",
"unknown",
},
Templates: map[string]TemplatedTypeMapping{
"bool": {
Empty: "new(bool)",
},
"bytea": {
Empty: "new([]uint8)",
},
"name": {
Empty: "new(string)",
},
"int8": {
Empty: "new(int64)",
},
"int2": {
Empty: "new(int16)",
},
"int4": {
Empty: "new(int32)",
},
"text": {
Empty: "new(string)",
},
"oid": {
Empty: "new(uint32)",
},
"xid": {
Empty: "new(uint32)",
},
"cid": {
Empty: "new(uint32)",
},
"json": {
Empty: "new(string)",
},
"float4": {
Empty: "new(float32)",
},
"float8": {
Empty: "new(float64)",
},
"numeric": {
Empty: "new(float64)",
},
"_bool": {
Empty: "new([]bool)",
},
"_int2": {
Empty: "new([]int16)",
},
"_int4": {
Empty: "new([]int32)",
},
"_text": {
Empty: "new([]string)",
},
"_varchar": {
Empty: "new([]string)",
},
"_int8": {
Empty: "new([]int64)",
},
"_float4": {
Empty: "new([]float32)",
},
"_float8": {
Empty: "new([]float64)",
},
"_numeric": {
Empty: "new([]float64)",
},
"varchar": {
Empty: "new(string)",
},
"date": {
Empty: "new(time.Time)",
},
"time": {
Empty: "new(time.Time)",
},
"timestamp": {
Empty: "new(time.Time)",
},
"_timestamp": {
Empty: "new([]time.Time)",
},
"_date": {
Empty: "new([]time.Time)",
},
"timestamptz": {
Empty: "new(time.Time)",
},
"_timestamptz": {
Empty: "new([]time.Time)",
},
"jsonb": {
Empty: "new(string)",
},
},
}
var sourceTemplate = template.Must(template.New("mappings.go").Funcs(sprig.TxtFuncMap()).Parse(`package mappings
import (
"github.com/lawrencejones/pgsink/pkg/decode"
)
// Auto-generated by pkg/decode/cmd/generate_mappings.go
func Each(do func(name string, mapping decode.TypeMapping)) {
for _, mapping := range Mappings {
do(mapping.Name, mapping)
}
}
// Mappings are the types we support natively from Postgres, with proper parsing
var Mappings = []decode.TypeMapping{
{{- range .Supported }}
{
Name: {{ .Name | quote }},
OID: {{ .OID }},
Scanner: {{ .Scanner }},
Empty: {{ .Empty }},
},
{{- end }}
}
// Unsupported is a list of types that Postgres normally supports that we choose not to,
// as we can't parse them into a sensibly common Golang types.
var Unsupported = []decode.TypeMapping{
{{- range .Unsupported }}
{
Name: {{ .Name | quote }},
OID: {{ .OID }},
},
{{- end }}
}
`))
func buildMappings(cfg Config) (supported, unsupported []TemplatedTypeMapping) {
ci := pgtype.NewConnInfo()
nextDataType:
for oid := uint32(1); oid < 4096; oid++ {
dataType, found := ci.DataTypeForOID(oid)
if !found {
continue // no type registered for oid
}
valueScanner, ok := dataType.Value.(decode.Scanner)
if !ok {
continue // we only want scanners that support our interface
}
for _, name := range cfg.Unsupported {
if name == dataType.Name {
unsupported = append(unsupported, TemplatedTypeMapping{
Name: dataType.Name,
OID: dataType.OID,
})
continue nextDataType
}
}
template, found := cfg.Templates[dataType.Name]
if !found {
log.Fatalf("no template set for %s (oid=%v)", dataType.Name, dataType.OID)
}
supported = append(supported, TemplatedTypeMapping{
Name: dataType.Name,
OID: dataType.OID,
Scanner: fmt.Sprintf("&%s{}", reflect.TypeOf(valueScanner).Elem().String()),
Empty: template.Empty,
})
}
return
}
func main() {
supported, unsupported := buildMappings(cfg)
var buffer = bytes.NewBuffer([]byte{})
err := sourceTemplate.Execute(buffer, struct {
Supported, Unsupported []TemplatedTypeMapping
}{supported, unsupported})
if err != nil {
panic(err.Error())
}
cmd := exec.Command("goimports")
cmd.Stderr = os.Stderr
cmd.Stdin = strings.NewReader(buffer.String())
output, err := cmd.Output()
if err != nil {
log.Fatalf(err.Error())
}
if len(os.Args) > 2 {
log.Printf("outputting to %s", os.Args[2])
if err := ioutil.WriteFile(os.Args[2], output, 0644); err != nil {
log.Fatalf(err.Error())
}
} else {
log.Printf("outputting to stdout")
fmt.Println(string(output))
}
}