-
Notifications
You must be signed in to change notification settings - Fork 3
/
svgen.go
238 lines (193 loc) · 4.37 KB
/
svgen.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
package main
import (
"flag"
"fmt"
"go/ast"
"go/types"
"os"
"path/filepath"
"golang.org/x/tools/go/loader"
"github.com/gojuno/generator"
)
type (
options struct {
Package string
OutputFileName string
}
typeValue struct {
name string
value ast.Expr
}
typeInfo struct {
Kind string //basic type
FoundScan bool
FoundValue bool
Values []string
}
visitor struct {
gen *generator.Generator
packageInfo *loader.PackageInfo
types map[string]*typeInfo
}
)
func main() {
opts := processFlags()
packagePath, err := generator.PackageAbsPath(opts.Package)
if err != nil {
die(err)
}
cfg := loader.Config{
AllowErrors: true,
TypeCheckFuncBodies: func(string) bool { return false },
TypeChecker: types.Config{
IgnoreFuncBodies: true,
FakeImportC: true,
DisableUnusedImportCheck: true,
Error: func(err error) {},
},
}
cfg.Import(opts.Package)
outputFile := filepath.Join(packagePath, opts.OutputFileName)
if err := os.Remove(outputFile); err != nil && !os.IsNotExist(err) {
die(err)
}
prog, err := cfg.Load()
if err != nil {
die(err)
}
gen := generator.New(prog)
gen.SetPackageName(prog.Package(opts.Package).Pkg.Name())
gen.SetHeader("DO NOT EDIT! This code was generated automatically using github.com/gojuno/svgen v1.0")
v := &visitor{
gen: gen,
packageInfo: prog.Package(opts.Package),
types: make(map[string]*typeInfo),
}
for _, file := range prog.Package(opts.Package).Files {
ast.Walk(v, file)
}
if err := gen.ProcessTemplate("interface", template, v.types); err != nil {
die(err)
}
if err := gen.WriteToFilename(outputFile); err != nil {
die(err)
}
}
func (v *visitor) Visit(node ast.Node) ast.Visitor {
ts, ok := node.(*ast.ValueSpec)
if !ok {
return v
}
if _, ok := ts.Type.(*ast.SelectorExpr); ok {
return nil //type of the constant defined in different package
}
ident, ok := ts.Type.(*ast.Ident)
if !ok {
return nil
}
typeName := ident.Name
ti, exist := v.types[typeName]
if !exist {
ti = &typeInfo{}
}
for _, name := range ts.Names {
ti.Values = append(ti.Values, name.Name)
}
if exist {
return nil
}
obj, ok := v.packageInfo.Defs[ts.Names[0]]
if !ok {
return nil
}
if _, ok := obj.(*types.Const); !ok { //not a constant
return nil
}
nt, ok := obj.Type().(*types.Named)
if !ok { //not a named type
return nil
}
b, ok := nt.Underlying().(*types.Basic)
if !ok { //not an alias for a basic type
return nil
}
switch b.Info() {
case types.IsInteger:
ti.Kind = "int64"
case types.IsString:
ti.Kind = "string"
default:
return nil
}
for i := 0; i < nt.NumMethods() && (!ti.FoundScan || !ti.FoundValue); i++ {
method := nt.Method(i)
switch method.Name() {
case "Scan":
ti.FoundScan = true
case "Value":
ti.FoundValue = true
}
}
if ti.FoundScan && ti.FoundValue {
return nil
}
v.types[typeName] = ti
return v
}
const template = `
{{range $typeName, $typeInfo := . }}
{{if not $typeInfo.FoundScan }}
func (t *{{$typeName}}) Scan(i interface{}) error {
var vv {{$typeName}}
switch v := i.(type) {
case nil:
return nil
{{if eq $typeInfo.Kind "string"}}case []byte:
vv = {{$typeName}}(v){{end}}
case {{$typeInfo.Kind}}:
vv = {{$typeName}}(v)
default:
return fmt.Errorf("can't scan %T into %T", v, t)
}
switch vv {
{{range $value := $typeInfo.Values}}case {{$value}}:{{end}}
default:
return fmt.Errorf("invalid value of type {{$typeName}}: %v", vv)
}
*t = vv
return nil
}
{{end}}
{{if not $typeInfo.FoundValue }}
func (t {{$typeName}}) Value() (driver.Value, error) {
{{if eq $typeInfo.Kind "string"}}if t == "" {
return nil, nil
}
{{end}}switch t {
{{range $value := $typeInfo.Values}}case {{$value}}:{{end}}
default:
return nil, fmt.Errorf("invalid value of type {{$typeName}}: %v", t)
}
return {{$typeInfo.Kind}}(t), nil
}
{{end}}
{{end}}`
func processFlags() *options {
var (
input = flag.String("i", "", "import path of the package containing type declarations")
output = flag.String("o", "scanners_valuers.go", "output file name")
)
flag.Parse()
if *input == "" {
flag.Usage()
os.Exit(1)
}
return &options{
Package: *input,
OutputFileName: *output,
}
}
func die(err error) {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}