forked from parsyl/parquet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
309 lines (272 loc) · 6.89 KB
/
parse.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
298
299
300
301
302
303
304
305
306
307
308
309
package parse
import (
"fmt"
"go/parser"
"go/token"
"log"
"strings"
"go/ast"
"github.com/parsyl/parquet/internal/fields"
flds "github.com/parsyl/parquet/internal/fields"
)
const letters = "abcdefghijklmnopqrstuvwxyz"
type field struct {
Field fields.Field
tagNames []string
fieldName string
fieldType string
omit bool
embedded bool
optional bool
repeated bool
err error
}
type Result struct {
Fields []flds.Field
Errors []error
}
// Fields gets the fields of the given struct.
// pth must be a go file that defines the typ struct.
// Any embedded structs must also be in that same file.
func Fields(typ, pth string) (*Result, error) {
fullTyp := typ
typ = getType(fullTyp)
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, pth, nil, 0)
if err != nil {
log.Fatal(err)
}
f := &finder{n: map[string]ast.Node{}}
ast.Walk(visitorFunc(f.findTypes), file)
if f.n == nil {
return nil, fmt.Errorf("could not find %s", typ)
}
fields, err := doGetFields(f.n)
if err != nil {
return nil, err
}
var out []field
var errs []error
var i int
for _, f := range fields[typ] {
i, out, errs = getOut(i, f, fields, errs, out)
}
return &Result{
Fields: getFields(fullTyp, out, fields),
Errors: errs,
}, nil
}
func getOut(i int, f field, fields map[string][]field, errs []error, out []field) (int, []field, []error) {
ff, ok := fields[f.fieldType]
var o flds.RepetitionType = flds.Required
if strings.Contains(f.Field.TypeName, "*") {
o = flds.Optional
} else if f.repeated || strings.Contains(f.Field.TypeName, "[]") {
o = flds.Repeated
}
if ok {
for _, fld := range ff {
if fld.embedded {
x, more, moreerrs := getOut(0, fld, fields, nil, nil)
i += x
out = append(out, more...)
errs = append(errs, moreerrs...)
} else {
if (!fld.optional && (o == flds.Optional || f.optional)) || (!fld.repeated && (o == flds.Repeated || f.repeated)) {
fld = makeOptional(fld)
}
if !f.embedded {
fld.Field.RepetitionTypes = append(append(f.Field.RepetitionTypes[:0:0], f.Field.RepetitionTypes...), o) //make a copy
fld.Field.FieldNames = append(f.Field.FieldNames, fld.Field.FieldNames...)
fld.Field.FieldTypes = append(f.Field.FieldTypes, fld.Field.FieldTypes...)
fld.Field.ColumnNames = append(f.Field.ColumnNames, fld.Field.ColumnNames...)
}
i, out, errs = getOut(i, fld, fields, errs, out)
}
}
return i, out, errs
} else if f.err == nil {
_, ok := types[f.fieldType]
if ok {
f.Field.RepetitionTypes = append(f.Field.RepetitionTypes, o)
out = append(out, f)
i++
} else {
errs = append(errs, fmt.Errorf("unsupported type: %s", f.fieldName))
}
}
return i, out, errs
}
func makeOptional(f field) field {
f.optional = true
fn, cat, pt := lookupTypeAndCategory(strings.Replace(strings.Replace(f.Field.TypeName, "*", "", 1), "[]", "", 1), true, true)
f.Field.FieldType = fn
f.Field.ParquetType = pt
f.Field.Category = cat
return f
}
func getType(typ string) string {
parts := strings.Split(typ, ".")
return parts[len(parts)-1]
}
func getFields(fullTyp string, fields []field, m map[string][]field) []flds.Field {
typ := getType(fullTyp)
out := make([]flds.Field, 0, len(fields))
for _, f := range fields {
_, ok := m[typ]
if f.omit || !ok {
continue
}
if f.repeated {
f.Field.TypeName = fmt.Sprintf("[]%s", f.Field.TypeName)
}
f.Field.Type = fullTyp
out = append(out, f.Field)
}
return out
}
func isPrivate(x *ast.Field) bool {
var s string
if len(x.Names) == 0 {
s = fmt.Sprintf("%s", x.Type)
} else {
s = fmt.Sprintf("%s", x.Names[0])
}
return strings.Contains(letters, string(s[0]))
}
func doGetFields(n map[string]ast.Node) (map[string][]field, error) {
fields := map[string][]field{}
for k, n := range n {
ast.Inspect(n, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.Field:
if len(x.Names) == 1 && !isPrivate(x) {
f := getField(x.Names[0].Name, x)
fields[k] = append(fields[k], f)
} else if len(x.Names) == 0 && !isPrivate(x) {
fields[k] = append(fields[k], field{embedded: true, fieldType: fmt.Sprintf("%s", x.Type), Field: flds.Field{TypeName: fmt.Sprintf("%s", x.Type)}})
}
case *ast.ArrayType:
s := fields[k]
f := s[len(s)-1]
f.repeated = true
s[len(s)-1] = f
fields[k] = s
}
return true
})
}
return fields, nil
}
func getField(name string, x ast.Node) field {
var typ, tag string
var optional, repeated bool
ast.Inspect(x, func(n ast.Node) bool {
switch t := n.(type) {
case *ast.Field:
if t.Tag != nil {
tag = parseTag(t.Tag.Value)
}
typ = fmt.Sprintf("%s", t.Type)
case *ast.ArrayType:
at := n.(*ast.ArrayType)
s := fmt.Sprintf("%v", at.Elt)
typ = s
repeated = true
case *ast.StarExpr:
optional = true
typ = fmt.Sprintf("%s", t.X)
case ast.Expr:
s := fmt.Sprintf("%v", t)
_, ok := types[s]
if ok {
typ = s
}
}
return true
})
if tag == "" {
tag = name
}
fn, cat, pt := lookupTypeAndCategory(typ, optional, repeated)
return field{
Field: flds.Field{
FieldNames: []string{name},
FieldTypes: []string{typ},
ColumnNames: []string{tag},
TypeName: getTypeName(typ, optional),
FieldType: fn,
ParquetType: pt,
Category: cat},
fieldName: name,
fieldType: typ,
omit: tag == "-",
optional: optional,
repeated: repeated,
}
}
func parseTag(t string) string {
i := strings.Index(t, `parquet:"`)
if i == -1 {
return ""
}
t = t[i+9:]
return t[:strings.Index(t, `"`)]
}
func getTypeName(s string, optional bool) string {
var star string
if optional {
star = "*"
}
return fmt.Sprintf("%s%s", star, s)
}
func lookupTypeAndCategory(name string, optional, repeated bool) (string, string, string) {
var op string
if optional || repeated {
op = "Optional"
}
f, ok := types[name]
if !ok {
return "", "", ""
}
return fmt.Sprintf(f.name, op, "Field"), fmt.Sprintf(f.category, op), fmt.Sprintf(f.name, "", "Type")
}
type fieldType struct {
name string
category string
}
var types = map[string]fieldType{
"int32": {"Int32%s%s", "numeric%s"},
"uint32": {"Uint32%s%s", "numeric%s"},
"int64": {"Int64%s%s", "numeric%s"},
"uint64": {"Uint64%s%s", "numeric%s"},
"float32": {"Float32%s%s", "numeric%s"},
"float64": {"Float64%s%s", "numeric%s"},
"bool": {"Bool%s%s", "bool%s"},
"string": {"String%s%s", "string%s"},
}
type visitorFunc func(n ast.Node) ast.Visitor
func (f visitorFunc) Visit(n ast.Node) ast.Visitor {
return f(n)
}
type finder struct {
n map[string]ast.Node
}
func (f *finder) findTypes(n ast.Node) ast.Visitor {
switch n := n.(type) {
case *ast.ImportSpec:
return visitorFunc(f.findTypes)
case *ast.Package:
return visitorFunc(f.findTypes)
case *ast.File:
return visitorFunc(f.findTypes)
case *ast.GenDecl:
if n.Tok == token.TYPE {
return visitorFunc(f.findTypes)
}
case *ast.TypeSpec:
f.n[n.Name.Name] = n
return visitorFunc(f.findTypes)
}
return nil
}