-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoparser.go
649 lines (553 loc) · 11.5 KB
/
goparser.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
package goparser
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"strconv"
"strings"
)
// represents integer types
type iInt interface {
uint8 | uint16 | uint32 | uint64 | int8 | int16 | int32 | int64
}
// represents float types
type iFloat interface {
float32 | float64
}
// represents basic literal types
type iLit interface {
string | iInt | iFloat | bool
}
// LitValue contains basic literal value
type LitValue[V iLit] struct {
Doc string
Name string
Value V
}
// SliceLitValue contains a slice of basic literal values
type SliceLitValue[V iLit] struct {
Doc string
Name string
Value []V
}
// MapLitValue contains a map with basic literal values as keys and values
type MapLitValue[K, V iLit] struct {
Doc string
Name string
Value map[K]V
}
// LitVal represents a basic response type for walk callback function
type LitVal[K, V iLit] interface {
LitValue[V] | SliceLitValue[V] | MapLitValue[K, V]
}
// GoParser contains an instance of ast.File
type GoParser struct {
f *ast.File
}
// New returns a new instance of GoParser
func New(path string) (*GoParser, error) {
stat, err := os.Stat(path)
if err != nil {
return nil, err
}
if stat.IsDir() {
return nil, fmt.Errorf("%q is a directory", path)
}
fileAst, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ParseComments)
if err != nil {
return nil, err
}
return &GoParser{f: fileAst}, nil
}
// GetBasicValues returns a list of values containing literal values by godoc label
//
// // someLabel
// var testVar = "3"
func GetBasicValues[V iLit](g *GoParser, docLabels ...string) []LitValue[V] {
if len(docLabels) == 0 {
return nil
}
docMap := make(map[string]struct{}, len(docLabels))
for _, doc := range docLabels {
docMap[doc] = struct{}{}
}
return getBasicValues[V](g.f, docMap)
}
func getBasicValues[V iLit](f *ast.File, docMap map[string]struct{}) []LitValue[V] {
return walkDecls[int64, V, LitValue[V]](f, docMap, func(doc, name string, val ast.Expr) *LitValue[V] {
var tVal V
_, isBool := (interface{})(tVal).(bool)
switch v := val.(type) {
case *ast.Ident:
if !isBool {
return nil
}
b, ok := parseBool(v)
if !ok {
return nil
}
tVal = (interface{})(b).(V)
case *ast.BasicLit:
b := parseBasicLit[V](v)
if b == nil {
return nil
}
tVal = *b
default:
return nil
}
lVal := &LitValue[V]{
Doc: doc,
Name: name,
Value: tVal,
}
return lVal
})
}
// GetSliceValues returns a list of values containing slices of literal values by godoc label
//
// // someLabel
// var testVar = []string{"3"}
func GetSliceValues[V iLit](g *GoParser, docLabels ...string) []SliceLitValue[V] {
if len(docLabels) == 0 {
return nil
}
docMap := make(map[string]struct{}, len(docLabels))
for _, doc := range docLabels {
docMap[doc] = struct{}{}
}
return getSliceValues[V](g.f, docMap)
}
func getSliceValues[V iLit](f *ast.File, docMap map[string]struct{}) []SliceLitValue[V] {
return walkDecls[int64, V, SliceLitValue[V]](f, docMap, func(doc, name string, val ast.Expr) *SliceLitValue[V] {
cmpVal, ok := val.(*ast.CompositeLit)
if !ok {
return nil
}
sValues := make([]V, 0, len(cmpVal.Elts))
for _, elt := range cmpVal.Elts {
bVal, ok := elt.(*ast.BasicLit)
if !ok {
continue
}
pVal := parseBasicLit[V](bVal)
if pVal == nil {
return nil
}
if pVal == nil {
continue
}
sValues = append(sValues, *pVal)
}
if len(sValues) > 0 {
return &SliceLitValue[V]{
Doc: doc,
Name: name,
Value: sValues,
}
}
return nil
})
}
// GetMapValues returns a list of values containing maps with literal types as keys and values by godoc label
//
// // someLabel
// var testVar = map[int]string{3: "3"}
func GetMapValues[K, V iLit](g *GoParser, docLabels ...string) []MapLitValue[K, V] {
if len(docLabels) == 0 {
return nil
}
docMap := make(map[string]struct{}, len(docLabels))
for _, doc := range docLabels {
docMap[doc] = struct{}{}
}
return getMapValues[K, V](g.f, docMap)
}
func getMapValues[K, V iLit](f *ast.File, docMap map[string]struct{}) []MapLitValue[K, V] {
return walkDecls[K, V, MapLitValue[K, V]](f, docMap, func(doc, name string, val ast.Expr) *MapLitValue[K, V] {
cmpVal, ok := val.(*ast.CompositeLit)
if !ok {
return nil
}
cValues := make(map[K]V, len(cmpVal.Elts))
for _, elt := range cmpVal.Elts {
cVal, ok := elt.(*ast.KeyValueExpr)
if !ok {
continue
}
keyVal := cVal.Key
valVal := cVal.Value
bKey, keyOk := keyVal.(*ast.BasicLit)
bVal, valOk := valVal.(*ast.BasicLit)
if !keyOk || !valOk {
continue
}
k := parseBasicLit[K](bKey)
v := parseBasicLit[V](bVal)
if k == nil || v == nil {
continue
}
cValues[*k] = *v
}
if len(cValues) > 0 {
return &MapLitValue[K, V]{
Doc: doc,
Name: name,
Value: cValues,
}
}
return nil
})
}
func walkDecls[K, V iLit, T LitVal[K, V]](f *ast.File, docMap map[string]struct{}, fn func(doc, name string, val ast.Expr) *T) []T {
result := make([]T, 0)
for _, d := range f.Decls {
switch decl := d.(type) {
case *ast.GenDecl:
for _, spec := range decl.Specs {
switch s := spec.(type) {
case *ast.ValueSpec:
for _, n := range s.Names {
if n.Obj == nil {
continue
}
vSpec, ok := n.Obj.Decl.(*ast.ValueSpec)
if !ok {
continue
}
if vSpec.Doc == nil || len(vSpec.Doc.List) < 1 {
continue
}
var foundDoc string
for _, doc := range vSpec.Doc.List {
docTxt := strings.TrimLeft(doc.Text, "/ ")
if _, ok := docMap[docTxt]; ok {
foundDoc = docTxt
break
}
}
if foundDoc == "" {
continue
}
val := vSpec.Values[0]
res := fn(foundDoc, n.Name, val)
if res != nil {
result = append(result, *res)
}
}
}
}
}
}
return result
}
// GetFuncNames returns a list of function names by receiver type or param types
func GetFuncNames(g *GoParser, recType string, paramTypes ...string) []string {
result := make([]string, 0)
outer:
for _, d := range g.f.Decls {
switch decl := d.(type) {
case *ast.FuncDecl:
rec := decl.Recv
if rec == nil && recType != "" {
continue
}
if rec != nil {
r := rec.List[0]
switch rType := r.Type.(type) {
case *ast.Ident:
if rType.Name != recType {
continue
}
case *ast.StarExpr:
id, ok := rType.X.(*ast.Ident)
if !ok || id.Name != recType {
continue
}
}
}
t := decl.Type
if (t == nil || t.Params == nil) && len(paramTypes) > 0 {
continue
}
if t != nil && t.Params != nil {
paramsMap := make(map[string]struct{}, len(t.Params.List))
for _, par := range t.Params.List {
switch pType := par.Type.(type) {
case *ast.Ident:
paramsMap[pType.Name] = struct{}{}
case *ast.StarExpr:
switch sType := pType.X.(type) {
case *ast.Ident:
paramsMap[sType.Name] = struct{}{}
case *ast.SelectorExpr:
if sType.Sel == nil {
continue
}
paramsMap[sType.Sel.Name] = struct{}{}
default:
continue
}
case *ast.SelectorExpr:
if pType.Sel == nil {
continue
}
paramsMap[pType.Sel.Name] = struct{}{}
}
}
for _, par := range paramTypes {
_, ok := paramsMap[par]
_, okQt := paramsMap[fmt.Sprintf("%q", par)]
if !ok && !okQt {
continue outer
}
}
}
result = append(result, decl.Name.Name)
}
}
return result
}
func parseBool(val *ast.Ident) (result bool, ok bool) {
b, err := strconv.ParseBool(val.Name)
if err != nil {
return false, false
}
return b, true
}
func parseBasicLit[V iLit](val *ast.BasicLit) *V {
var zeroVal V
switch (interface{})(zeroVal).(type) {
case string:
if val.Kind != token.STRING {
return nil
}
strVal, err := strconv.Unquote(val.Value)
if err != nil {
return nil
}
zeroVal = (interface{})(strVal).(V)
case int8:
iv := parseIntLit[int8](val)
if iv == nil {
return nil
}
zeroVal = (interface{})(*iv).(V)
case int16:
iv := parseIntLit[int16](val)
if iv == nil {
return nil
}
zeroVal = (interface{})(*iv).(V)
case int32:
iv := parseIntLit[int32](val)
if iv == nil {
return nil
}
zeroVal = (interface{})(*iv).(V)
case int64:
iv := parseIntLit[int64](val)
if iv == nil {
return nil
}
zeroVal = (interface{})(*iv).(V)
case uint8:
iv := parseIntLit[uint8](val)
if iv == nil {
return nil
}
zeroVal = (interface{})(*iv).(V)
case uint16:
iv := parseIntLit[uint16](val)
if iv == nil {
return nil
}
zeroVal = (interface{})(*iv).(V)
case uint32:
iv := parseIntLit[uint32](val)
if iv == nil {
return nil
}
zeroVal = (interface{})(*iv).(V)
case uint64:
iv := parseIntLit[uint64](val)
if iv == nil {
return nil
}
zeroVal = (interface{})(*iv).(V)
case float32:
fv := parseFloatLit[float32](val)
if fv == nil {
return nil
}
zeroVal = (interface{})(*fv).(V)
case float64:
fv := parseFloatLit[float64](val)
if fv == nil {
return nil
}
zeroVal = (interface{})(*fv).(V)
default:
return nil
}
return &zeroVal
}
func parseIntLit[I iInt](val *ast.BasicLit) *I {
if val.Kind != token.INT {
return nil
}
var parsed I
parseI := func() bool {
v := parseInt[I](val.Value)
if v == nil {
return false
}
parsed = *v
return true
}
parseU := func() bool {
v := parseUint[I](val.Value)
if v == nil {
return false
}
parsed = *v
return true
}
switch (interface{})(parsed).(type) {
case int8:
if !parseI() {
return nil
}
case int16:
if !parseI() {
return nil
}
case int32:
if !parseI() {
return nil
}
case int64:
if !parseI() {
return nil
}
case uint8:
if !parseU() {
return nil
}
case uint16:
if !parseU() {
return nil
}
case uint32:
if !parseU() {
return nil
}
case uint64:
if !parseU() {
return nil
}
default:
return nil
}
return &parsed
}
func parseUint[I iInt](s string) *I {
var uintVal uint64
var err error
parse := func(bitSize int) {
uintVal, err = strconv.ParseUint(s, 10, bitSize)
}
var zeroVal I
switch (interface{})(zeroVal).(type) {
case uint8:
parse(8)
case uint16:
parse(16)
case uint32:
parse(32)
case uint64:
parse(64)
default:
return nil
}
if err != nil {
return nil
}
zeroVal = (interface{})(uintVal).(I)
return &zeroVal
}
func parseInt[I iInt](s string) *I {
var intVal int64
var err error
parse := func(bitSize int) {
intVal, err = strconv.ParseInt(s, 10, bitSize)
}
var zeroVal I
switch (interface{})(zeroVal).(type) {
case int8:
parse(8)
case int16:
parse(16)
case int32:
parse(32)
case int64:
parse(64)
default:
return nil
}
if err != nil {
return nil
}
zeroVal = (interface{})(intVal).(I)
return &zeroVal
}
func parseFloatLit[F iFloat](val *ast.BasicLit) *F {
if val.Kind != token.FLOAT {
return nil
}
var parsed F
parse := func() bool {
v := parseFloat[F](val.Value)
if v == nil {
return false
}
parsed = *v
return true
}
switch (interface{})(parsed).(type) {
case float32:
if !parse() {
return nil
}
case float64:
if !parse() {
return nil
}
default:
return nil
}
return &parsed
}
func parseFloat[F iFloat](s string) *F {
var fVal float64
var err error
parse := func(bitSize int) {
fVal, err = strconv.ParseFloat(s, bitSize)
}
var zeroVal F
switch (interface{})(zeroVal).(type) {
case float32:
parse(32)
case float64:
parse(64)
default:
return nil
}
if err != nil {
return nil
}
zeroVal = (interface{})(fVal).(F)
return &zeroVal
}