forked from peterlabuschagne/jettison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jcode.go
303 lines (273 loc) · 6.32 KB
/
jcode.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
package main
import (
"bytes"
"encoding/base64"
"encoding/hex"
"flag"
"fmt"
"go/token"
"math/rand"
"os"
"strings"
"time"
"github.com/dave/dst"
"github.com/dave/dst/decorator"
"github.com/Zulzi/jettison/errors"
"github.com/Zulzi/jettison/j"
)
const (
statusError = 1
statusHelp = 2
statusInvalidFile = 3
jetPath = "\"github.com/Zulzi/jettison/errors\""
jPath = "\"github.com/Zulzi/jettison/j\""
)
var (
rewrite = flag.Bool("rewrite", false, "rewrite source files")
cFormat = flag.String("format", "err_hex_16", "error code format")
)
type codeFormat int
const (
formatBase64 codeFormat = 1
formatErrHex codeFormat = 2
)
type formatParams struct {
format codeFormat
label string
gen func(pkg, variable string) string
valid func(pkg, variable, code string) bool
}
var fmtParams = []formatParams{
{
formatBase64, "base64_8", fmtBase64, validBase64,
},
{
formatErrHex, "err_hex_16", fmtErrHex, validErrHex,
},
}
func main() {
rand.Seed(time.Now().UnixNano())
flag.Parse()
if flag.NArg() == 0 {
fmt.Fprintln(os.Stderr, "No file provided")
flag.Usage()
os.Exit(statusHelp)
}
fp := getFormatParams()
fail := false
for _, file := range flag.Args() {
res, err := checkFile(file, fp, *rewrite)
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(statusError)
}
if res.pass {
continue
}
for _, msg := range res.msgs {
fmt.Println(" ", msg)
}
if *rewrite {
err := os.WriteFile(file, res.out, 0o644)
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(statusError)
}
} else {
fail = true
}
}
if fail {
os.Exit(statusInvalidFile)
}
}
func getFormatParams() formatParams {
var fp formatParams
for _, p := range fmtParams {
if p.label == *cFormat {
fp = p
}
}
if fp.label == "" {
fmt.Fprintln(os.Stderr, "Unknown format")
flag.Usage()
os.Exit(statusHelp)
}
return fp
}
type checkResult struct {
pass bool
msgs []string
out []byte
}
func checkFile(file string, fp formatParams, rewrite bool) (checkResult, error) {
var (
msgs []string
jettisonPkg string
pkg string
importGenDecl *dst.GenDecl
foundJ bool
)
fset := token.NewFileSet()
f, err := decorator.ParseFile(fset, file, nil, 0)
if err != nil {
return checkResult{}, err
}
for _, decl := range f.Decls {
gd, ok := decl.(*dst.GenDecl)
if !ok {
continue
}
if gd.Tok == token.IMPORT {
// import block
importGenDecl = gd
for _, s := range gd.Specs {
is, ok := s.(*dst.ImportSpec)
if !ok {
return checkResult{}, errors.Wrap(err, "Unexpected non import spec",
j.KV("file", file))
}
alias := "errors"
if is.Name != nil {
alias = is.Name.Name
}
if string(is.Path.Value) == jetPath {
jettisonPkg = alias
} else if string(is.Path.Value) == jPath {
foundJ = true
}
}
continue
}
if gd.Tok == token.VAR {
// var block
for _, s := range gd.Specs {
// Check for jettison sentinel errors: var ErrFoo = errors.New("foo").
vp, ok := s.(*dst.ValueSpec)
if !ok {
continue
}
if len(vp.Names) != 1 || len(vp.Values) != 1 {
continue
}
ce, ok := vp.Values[0].(*dst.CallExpr)
if !ok {
continue
}
varName := vp.Names[0].Name
se, ok := ce.Fun.(*dst.SelectorExpr)
if !ok {
continue
}
if fmt.Sprint(se.X) != jettisonPkg || se.Sel.Name != "New" {
continue
}
// Check code
msg, err := checkInstance(jettisonPkg, pkg, varName, ce, fp)
if err != nil {
return checkResult{}, errors.Wrap(err, "error checking instance",
j.MKV{"file": file, "var": varName})
}
if msg != "" {
msgs = append(msgs, fmt.Sprintf("%s: %s: %s (fixed)", file, varName, msg))
}
}
}
}
var buf bytes.Buffer
if len(msgs) > 0 && rewrite {
if !foundJ {
// Add jettison/j import
importGenDecl.Specs = append(importGenDecl.Specs, &dst.ImportSpec{
Path: &dst.BasicLit{Value: jPath},
})
}
if err := decorator.Fprint(&buf, f); err != nil {
return checkResult{}, err
}
}
return checkResult{
pass: len(msgs) == 0,
msgs: msgs,
out: buf.Bytes(),
}, nil
}
func checkInstance(jettisonPkg string, pkg string, varName string, ce *dst.CallExpr, format formatParams) (string, error) {
if len(ce.Args) < 2 {
code := format.gen(pkg, varName)
ce.Args = append(ce.Args, makeCodeCall(code))
return "missing jettison code", nil
}
if len(ce.Args) > 2 {
return "", errors.New("only single jettison code option supported")
}
ce2, ok := ce.Args[1].(*dst.CallExpr)
if !ok {
return "", errors.New("invalid second argument, expect j.C")
}
se2, ok := ce2.Fun.(*dst.SelectorExpr)
if !ok {
return "", errors.New("invalid second argument, expect j.C")
}
selExp := fmt.Sprintf("%s.%s", se2.X, se2.Sel)
if selExp != jettisonPkg+".WithCode" && selExp != "j.C" {
return "", errors.New("invalid second argument, expect j.C")
}
bl, ok := ce2.Args[0].(*dst.BasicLit)
if !ok {
return "", errors.New("invalid code argument, expect string")
}
if !format.valid(pkg, varName, strings.Trim(bl.Value, "\"")) {
code := format.gen(pkg, varName)
ce.Args[1] = makeCodeCall(code)
return "incorrect jettison code", nil
}
return "", nil
}
func makeCodeCall(code string) *dst.CallExpr {
codeExp := &dst.SelectorExpr{
X: dst.NewIdent("j"),
Sel: dst.NewIdent("C"),
}
return &dst.CallExpr{
Fun: codeExp,
Args: []dst.Expr{dst.NewIdent(fmt.Sprintf("\"%s\"", code))},
}
}
// fmtBase64 returns a base64 string of length 8; which is equivalent to a random uint48.
func fmtBase64(_, _ string) string {
b := make([]byte, 6)
_, err := rand.Read(b)
if err != nil {
return "sorry error"
}
return base64.RawURLEncoding.EncodeToString(b)
}
func validBase64(_, _, code string) bool {
if len(code) != 8 {
return false
}
_, err := base64.RawURLEncoding.DecodeString(code)
return err == nil
}
// fmtErrHex returns string that matches the following regex "ERR_[0-9a-f]{16}",
// ie. "ERR_" followed by random 16 character hex string.
func fmtErrHex(_, _ string) string {
b := make([]byte, 8)
_, err := rand.Read(b)
if err != nil {
return "sorry error"
}
return fmt.Sprintf("ERR_%x", b)
}
func validErrHex(_, _, code string) bool {
s := strings.Split(code, "_")
if len(s) != 2 {
return false
}
if s[0] != "ERR" {
return false
}
_, err := hex.DecodeString(s[1])
return err == nil
}