-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
189 lines (153 loc) · 3.38 KB
/
main.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
package main
import (
"bytes"
_ "embed"
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"log"
"os"
"text/template"
)
//go:embed template/template.tmpl
var tmplSrc string
var (
source = flag.String("source", "", "")
destination = flag.String("destination", "", "")
typeName = flag.String("type", "", "")
packageName = flag.String("package", "", "")
)
type Value struct {
FieldName string
Path string
Type string
ReleaseKeys bool
Stack []Value
}
func (v Value) copy() Value {
var stack = make([]Value, len(v.Stack))
copy(stack, v.Stack)
return Value{
FieldName: v.FieldName,
Type: v.Type,
ReleaseKeys: v.ReleaseKeys,
Path: v.Path,
Stack: stack,
}
}
type Info struct {
TypeName string
PackageName string
Values []Value
}
func main() {
checkFlags()
file, err := parser.ParseFile(token.NewFileSet(), *source, nil, parser.ParseComments)
if err != nil {
log.Fatal(err)
}
var info *Info
ast.Inspect(file, func(x ast.Node) bool {
ts, ok := x.(*ast.TypeSpec)
if !ok {
return true
}
if ts.Name.String() != *typeName {
return true
}
_x, ok := ts.Type.(*ast.StructType)
if !ok {
return true
}
info = &Info{TypeName: ts.Name.String(), PackageName: *packageName}
for _, field := range _x.Fields.List {
info.Values = append(info.Values, process(Value{Path: field.Names[0].Name}, field, field.Type)...)
}
return false
})
tmpl, err := template.New("template").Funcs(template.FuncMap{
"inc": func(n int) int {
return n + 1
},
}).Parse(tmplSrc)
if err != nil {
log.Fatal(err)
}
var goCode bytes.Buffer
if err = tmpl.Execute(&goCode, info); err != nil {
log.Fatal(err)
}
formattedGoCode, err := format.Source(goCode.Bytes())
if err != nil {
log.Fatal(err)
}
if err = os.WriteFile(*destination, formattedGoCode, 0600); err != nil {
log.Fatal(err)
}
}
func process(val Value, field *ast.Field, expr ast.Expr) []Value {
ident, ok := expr.(*ast.Ident)
if ok {
val.FieldName = field.Names[0].Name
val.Type = toParquetType(ident.Name)
return []Value{val}
}
arrayType, ok := expr.(*ast.ArrayType)
if ok {
val.Stack = append(val.Stack, Value{
FieldName: field.Names[0].Name,
Type: "Array",
})
return process(val, field, arrayType.Elt)
}
mapType, ok := expr.(*ast.MapType)
if ok {
var (
keyVal = val.copy()
valVal = val.copy()
)
keyVal.ReleaseKeys = false
keyVal.Path += "_mapKey"
keyVal.Stack = append(keyVal.Stack, Value{
FieldName: field.Names[0].Name,
Type: "MapKey",
})
valVal.ReleaseKeys = true
valVal.Path += "_mapValue"
valVal.Stack = append(valVal.Stack, Value{
FieldName: field.Names[0].Name,
Type: "MapValue",
})
return append(
process(keyVal, field, mapType.Key),
process(valVal, field, mapType.Value)...,
)
}
panic(fmt.Sprintf("not supported: %T", field.Type))
}
func checkFlags() {
flag.Parse()
if len(*source) == 0 {
log.Fatal("source was not provided --source file.go")
}
if len(*destination) == 0 {
log.Fatal("destination was not provided --destination file_gen.go")
}
if len(*typeName) == 0 {
log.Fatal("type was not provided --type TypeName")
}
if len(*packageName) == 0 {
log.Fatal("package was not provided --package pkg")
}
}
func toParquetType(v string) string {
switch v {
case "string":
return "parquet.ByteArray"
default:
return v
}
}