This repository is currently being migrated. It's locked while the migration is in progress.
forked from parsyl/parquet
/
parse.go
271 lines (232 loc) · 5.26 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
package parse
import (
"fmt"
"go/parser"
"go/token"
"log"
"strings"
"go/ast"
"github.com/BillHeroInc/parquet/cmd/parquetgen/fields"
flds "github.com/BillHeroInc/parquet/cmd/parquetgen/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
}
// Result holds the fields and errors that are generated
// by reading a go struct.
type Result struct {
// Fields are the fields that will be written to and read from a parquet file.
Parent flds.Field
// Errors is a list of errors that occurred while parsing a struct.
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 := getFields(f.n)
if err != nil {
return nil, err
}
parent, ok := fields[typ]
if !ok {
return nil, fmt.Errorf("could not find %s", typ)
}
errs := getChildren(&parent, fields)
return &Result{
Parent: flds.Field{Type: typ, Children: parent.Children},
Errors: errs,
}, nil
}
func getChildren(parent *flds.Field, fields map[string]flds.Field) []error {
var children []flds.Field
var errs []error
p, ok := fields[parent.Type]
if !ok {
errs = append(errs, fmt.Errorf("could not find %s", parent.Type))
}
for _, child := range p.Children {
if child.Primitive() {
children = append(children, child)
continue
}
f, ok := fields[child.Type]
if !ok {
f, ok = fields[child.Type]
if !ok {
errs = append(errs, fmt.Errorf("unsupported type %+v", child.Type))
continue
}
}
errs = append(errs, getChildren(&child, fields)...)
f.Name = child.Name
f.Type = child.Type
f.ColumnName = child.ColumnName
f.Children = child.Children
f.RepetitionType = child.RepetitionType
if child.Embedded {
for _, ch := range f.Children {
children = append(children, ch)
}
} else {
children = append(children, f)
}
}
parent.Children = children
return errs
}
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 getFields(n map[string]ast.Node) (map[string]fields.Field, error) {
fields := map[string]flds.Field{}
for k, n := range n {
_, ok := n.(*ast.TypeSpec)
if !ok {
continue
}
parent := flds.Field{
Type: k,
}
ast.Inspect(n, func(n ast.Node) bool {
if n == nil {
return false
}
switch x := n.(type) {
case *ast.Field:
if len(x.Names) == 1 && !isPrivate(x) {
f, skip := getField(x.Names[0].Name, x, nil)
if !skip {
parent.Children = append(parent.Children, f)
}
} else if len(x.Names) == 0 && !isPrivate(x) {
f, skip := getField(fmt.Sprintf("%s", x.Type), x, nil)
f.Embedded = true
if !skip {
parent.Children = append(parent.Children, f)
}
}
}
return true
})
fields[k] = parent
}
return fields, nil
}
func getType(typ string) string {
parts := strings.Split(typ, ".")
return parts[len(parts)-1]
}
func getField(name string, x ast.Node, parent *flds.Field) (flds.Field, bool) {
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
}
rt := fields.Required
if repeated {
rt = fields.Repeated
} else if optional {
rt = fields.Optional
}
return flds.Field{
Type: typ,
Name: name,
ColumnName: tag,
RepetitionType: rt,
}, tag == "-"
}
func parseTag(t string) string {
i := strings.Index(t, `parquet:"`)
if i == -1 {
return ""
}
t = t[i+9:]
return t[:strings.Index(t, `"`)]
}
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
}
var types = map[string]bool{
"int32": true,
"uint32": true,
"int64": true,
"uint64": true,
"float32": true,
"float64": true,
"bool": true,
"string": true,
}