-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathparse.go
830 lines (762 loc) · 19.8 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
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
package parse
import (
"errors"
"fmt"
"go/ast"
"go/doc"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"sort"
"strings"
"time"
"github.com/magefile/mage/internal"
)
const importTag = "mage:import"
var debug = log.New(ioutil.Discard, "DEBUG: ", log.Ltime|log.Lmicroseconds)
// EnableDebug turns on debug logging.
func EnableDebug() {
debug.SetOutput(os.Stderr)
}
// PkgInfo contains inforamtion about a package of files according to mage's
// parsing rules.
type PkgInfo struct {
AstPkg *ast.Package
DocPkg *doc.Package
Description string
Funcs Functions
DefaultFunc *Function
Aliases map[string]*Function
Imports Imports
}
// Function represented a job function from a mage file
type Function struct {
PkgAlias string
Package string
ImportPath string
Name string
Receiver string
IsError bool
IsContext bool
Synopsis string
Comment string
Args []Arg
}
var _ sort.Interface = (Functions)(nil)
// Functions implements sort interface to optimize compiled output with
// deterministic generated mainfile.
type Functions []*Function
func (s Functions) Len() int {
return len(s)
}
func (s Functions) Less(i, j int) bool {
return s[i].TargetName() < s[j].TargetName()
}
func (s Functions) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Arg is an argument to a Function.
type Arg struct {
Name, Type string
}
// ID returns user-readable information about where this function is defined.
func (f Function) ID() string {
path := "<current>"
if f.ImportPath != "" {
path = f.ImportPath
}
receiver := ""
if f.Receiver != "" {
receiver = f.Receiver + "."
}
return fmt.Sprintf("%s.%s%s", path, receiver, f.Name)
}
// TargetName returns the name of the target as it should appear when used from
// the mage cli. It is always lowercase.
func (f Function) TargetName() string {
var names []string
for _, s := range []string{f.PkgAlias, f.Receiver, f.Name} {
if s != "" {
names = append(names, s)
}
}
return strings.Join(names, ":")
}
// ExecCode returns code for the template switch to run the target.
// It wraps each target call to match the func(context.Context) error that
// runTarget requires.
func (f Function) ExecCode() string {
name := f.Name
if f.Receiver != "" {
name = f.Receiver + "{}." + name
}
if f.Package != "" {
name = f.Package + "." + name
}
var parseargs string
for x, arg := range f.Args {
switch arg.Type {
case "string":
parseargs += fmt.Sprintf(`
arg%d := args.Args[x]
x++`, x)
case "int":
parseargs += fmt.Sprintf(`
arg%d, err := strconv.Atoi(args.Args[x])
if err != nil {
logger.Printf("can't convert argument %%q to int\n", args.Args[x])
os.Exit(2)
}
x++`, x)
case "bool":
parseargs += fmt.Sprintf(`
arg%d, err := strconv.ParseBool(args.Args[x])
if err != nil {
logger.Printf("can't convert argument %%q to bool\n", args.Args[x])
os.Exit(2)
}
x++`, x)
case "time.Duration":
parseargs += fmt.Sprintf(`
arg%d, err := time.ParseDuration(args.Args[x])
if err != nil {
logger.Printf("can't convert argument %%q to time.Duration\n", args.Args[x])
os.Exit(2)
}
x++`, x)
}
}
out := parseargs + `
wrapFn := func(ctx context.Context) error {
`
if f.IsError {
out += "return "
}
out += name + "("
var args []string
if f.IsContext {
args = append(args, "ctx")
}
for x := 0; x < len(f.Args); x++ {
args = append(args, fmt.Sprintf("arg%d", x))
}
out += strings.Join(args, ", ")
out += ")"
if !f.IsError {
out += `
return nil`
}
out += `
}
ret := runTarget(wrapFn)`
return out
}
// PrimaryPackage parses a package. If files is non-empty, it will only parse the files given.
func PrimaryPackage(gocmd, path string, files []string) (*PkgInfo, error) {
info, err := Package(path, files)
if err != nil {
return nil, err
}
if err := setImports(gocmd, info); err != nil {
return nil, err
}
setDefault(info)
setAliases(info)
return info, nil
}
func checkDupes(info *PkgInfo, imports []*Import) error {
funcs := map[string][]*Function{}
for _, f := range info.Funcs {
funcs[strings.ToLower(f.TargetName())] = append(funcs[strings.ToLower(f.TargetName())], f)
}
for _, imp := range imports {
for _, f := range imp.Info.Funcs {
target := strings.ToLower(f.TargetName())
funcs[target] = append(funcs[target], f)
}
}
for alias, f := range info.Aliases {
if len(funcs[alias]) != 0 {
var ids []string
for _, f := range funcs[alias] {
ids = append(ids, f.ID())
}
return fmt.Errorf("alias %q duplicates existing target(s): %s\n", alias, strings.Join(ids, ", "))
}
funcs[alias] = append(funcs[alias], f)
}
var dupes []string
for target, list := range funcs {
if len(list) > 1 {
dupes = append(dupes, target)
}
}
if len(dupes) == 0 {
return nil
}
errs := make([]string, 0, len(dupes))
for _, d := range dupes {
var ids []string
for _, f := range funcs[d] {
ids = append(ids, f.ID())
}
errs = append(errs, fmt.Sprintf("%q target has multiple definitions: %s\n", d, strings.Join(ids, ", ")))
}
return errors.New(strings.Join(errs, "\n"))
}
// Package compiles information about a mage package.
func Package(path string, files []string) (*PkgInfo, error) {
start := time.Now()
defer func() {
debug.Println("time parse Magefiles:", time.Since(start))
}()
fset := token.NewFileSet()
pkg, err := getPackage(path, files, fset)
if err != nil {
return nil, err
}
p := doc.New(pkg, "./", 0)
pi := &PkgInfo{
AstPkg: pkg,
DocPkg: p,
Description: toOneLine(p.Doc),
}
setNamespaces(pi)
setFuncs(pi)
hasDupes, names := checkDupeTargets(pi)
if hasDupes {
msg := "Build targets must be case insensitive, thus the following targets conflict:\n"
for _, v := range names {
if len(v) > 1 {
msg += " " + strings.Join(v, ", ") + "\n"
}
}
return nil, errors.New(msg)
}
return pi, nil
}
func getNamedImports(gocmd string, pkgs map[string]string) ([]*Import, error) {
var imports []*Import
for pkg, alias := range pkgs {
debug.Printf("getting import package %q, alias %q", pkg, alias)
imp, err := getImport(gocmd, pkg, alias)
if err != nil {
return nil, err
}
imports = append(imports, imp)
}
return imports, nil
}
// getImport returns the metadata about a package that has been mage:import'ed.
func getImport(gocmd, importpath, alias string) (*Import, error) {
out, err := internal.OutputDebug(gocmd, "list", "-f", "{{.Dir}}||{{.Name}}", importpath)
if err != nil {
return nil, err
}
parts := strings.Split(out, "||")
if len(parts) != 2 {
return nil, fmt.Errorf("incorrect data from go list: %s", out)
}
dir, name := parts[0], parts[1]
debug.Printf("parsing imported package %q from dir %q", importpath, dir)
// we use go list to get the list of files, since go/parser doesn't differentiate between
// go files with build tags etc, and go list does. This prevents weird problems if you
// have more than one package in a folder because of build tags.
out, err = internal.OutputDebug(gocmd, "list", "-f", `{{join .GoFiles "||"}}`, importpath)
if err != nil {
return nil, err
}
files := strings.Split(out, "||")
info, err := Package(dir, files)
if err != nil {
return nil, err
}
for i := range info.Funcs {
debug.Printf("setting alias %q and package %q on func %v", alias, name, info.Funcs[i].Name)
info.Funcs[i].PkgAlias = alias
info.Funcs[i].ImportPath = importpath
}
return &Import{Alias: alias, Name: name, Path: importpath, Info: *info}, nil
}
// Import represents the data about a mage:import package
type Import struct {
Alias string
Name string
UniqueName string // a name unique across all imports
Path string
Info PkgInfo
}
var _ sort.Interface = (Imports)(nil)
// Imports implements sort interface to optimize compiled output with
// deterministic generated mainfile.
type Imports []*Import
func (s Imports) Len() int {
return len(s)
}
func (s Imports) Less(i, j int) bool {
return s[i].UniqueName < s[j].UniqueName
}
func (s Imports) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func setFuncs(pi *PkgInfo) {
for _, f := range pi.DocPkg.Funcs {
if f.Recv != "" {
debug.Printf("skipping method %s.%s", f.Recv, f.Name)
// skip methods
continue
}
if !ast.IsExported(f.Name) {
debug.Printf("skipping non-exported function %s", f.Name)
// skip non-exported functions
continue
}
fn, err := funcType(f.Decl.Type)
if err != nil {
debug.Printf("skipping function with invalid signature func %s: %v", f.Name, err)
continue
}
debug.Printf("found target %v", f.Name)
fn.Name = f.Name
fn.Comment = toOneLine(f.Doc)
fn.Synopsis = sanitizeSynopsis(f)
pi.Funcs = append(pi.Funcs, fn)
}
}
func setNamespaces(pi *PkgInfo) {
for _, t := range pi.DocPkg.Types {
if !isNamespace(t) {
continue
}
debug.Printf("found namespace %s %s", pi.DocPkg.ImportPath, t.Name)
for _, f := range t.Methods {
if !ast.IsExported(f.Name) {
continue
}
fn, err := funcType(f.Decl.Type)
if err != nil {
debug.Printf("skipping invalid namespace method %s %s.%s: %v", pi.DocPkg.ImportPath, t.Name, f.Name, err)
continue
}
debug.Printf("found namespace method %s %s.%s", pi.DocPkg.ImportPath, t.Name, f.Name)
fn.Name = f.Name
fn.Comment = toOneLine(f.Doc)
fn.Synopsis = sanitizeSynopsis(f)
fn.Receiver = t.Name
pi.Funcs = append(pi.Funcs, fn)
}
}
}
func setImports(gocmd string, pi *PkgInfo) error {
importNames := map[string]string{}
rootImports := []string{}
for _, f := range pi.AstPkg.Files {
for _, d := range f.Decls {
gen, ok := d.(*ast.GenDecl)
if !ok || gen.Tok != token.IMPORT {
continue
}
for j := 0; j < len(gen.Specs); j++ {
spec := gen.Specs[j]
impspec := spec.(*ast.ImportSpec)
if len(gen.Specs) == 1 && gen.Lparen == token.NoPos && impspec.Doc == nil {
impspec.Doc = gen.Doc
}
name, alias, ok := getImportPath(impspec)
if !ok {
continue
}
if alias != "" {
debug.Printf("found %s: %s (%s)", importTag, name, alias)
importNames[name] = alias
} else {
debug.Printf("found %s: %s", importTag, name)
rootImports = append(rootImports, name)
}
}
}
}
imports, err := getNamedImports(gocmd, importNames)
if err != nil {
return err
}
for _, s := range rootImports {
imp, err := getImport(gocmd, s, "")
if err != nil {
return err
}
imports = append(imports, imp)
}
if err := checkDupes(pi, imports); err != nil {
return err
}
// have to set unique package names on imports
used := map[string]bool{}
for _, imp := range imports {
unique := imp.Name + "_mageimport"
x := 1
for used[unique] {
unique = fmt.Sprintf("%s_mageimport%d", imp.Name, x)
x++
}
used[unique] = true
imp.UniqueName = unique
for _, f := range imp.Info.Funcs {
f.Package = unique
}
}
pi.Imports = imports
return nil
}
func getImportPath(imp *ast.ImportSpec) (path, alias string, ok bool) {
if imp.Doc == nil || len(imp.Doc.List) == 9 {
return "", "", false
}
// import is always the last comment
s := imp.Doc.List[len(imp.Doc.List)-1].Text
// trim comment start and normalize for anyone who has spaces or not between
// "//"" and the text
vals := strings.Fields(strings.ToLower(s[2:]))
if len(vals) == 0 {
return "", "", false
}
if vals[0] != importTag {
return "", "", false
}
path, ok = lit2string(imp.Path)
if !ok {
return "", "", false
}
switch len(vals) {
case 1:
// just the import tag, this is a root import
return path, "", true
case 2:
// also has an alias
return path, vals[1], true
default:
log.Println("warning: ignoring malformed", importTag, "for import", path)
return "", "", false
}
}
func isNamespace(t *doc.Type) bool {
if len(t.Decl.Specs) != 1 {
return false
}
id, ok := t.Decl.Specs[0].(*ast.TypeSpec)
if !ok {
return false
}
sel, ok := id.Type.(*ast.SelectorExpr)
if !ok {
return false
}
ident, ok := sel.X.(*ast.Ident)
if !ok {
return false
}
return ident.Name == "mg" && sel.Sel.Name == "Namespace"
}
// checkDupeTargets checks a package for duplicate target names.
func checkDupeTargets(info *PkgInfo) (hasDupes bool, names map[string][]string) {
names = map[string][]string{}
lowers := map[string]bool{}
for _, f := range info.Funcs {
low := strings.ToLower(f.Name)
if f.Receiver != "" {
low = strings.ToLower(f.Receiver) + ":" + low
}
if lowers[low] {
hasDupes = true
}
lowers[low] = true
names[low] = append(names[low], f.Name)
}
return hasDupes, names
}
// sanitizeSynopsis sanitizes function Doc to create a summary.
func sanitizeSynopsis(f *doc.Func) string {
synopsis := doc.Synopsis(f.Doc)
// If the synopsis begins with the function name, remove it. This is done to
// not repeat the text.
// From:
// clean Clean removes the temporarily generated files
// To:
// clean removes the temporarily generated files
if syns := strings.Split(synopsis, " "); strings.EqualFold(f.Name, syns[0]) {
return strings.Join(syns[1:], " ")
}
return synopsis
}
func setDefault(pi *PkgInfo) {
for _, v := range pi.DocPkg.Vars {
for x, name := range v.Names {
if name != "Default" {
continue
}
spec := v.Decl.Specs[x].(*ast.ValueSpec)
if len(spec.Values) != 1 {
log.Println("warning: default declaration has multiple values")
}
f, err := getFunction(spec.Values[0], pi)
if err != nil {
log.Println("warning, default declaration malformed:", err)
return
}
pi.DefaultFunc = f
return
}
}
}
func lit2string(l *ast.BasicLit) (string, bool) {
if !strings.HasPrefix(l.Value, `"`) || !strings.HasSuffix(l.Value, `"`) {
return "", false
}
return strings.Trim(l.Value, `"`), true
}
func setAliases(pi *PkgInfo) {
for _, v := range pi.DocPkg.Vars {
for x, name := range v.Names {
if name != "Aliases" {
continue
}
spec, ok := v.Decl.Specs[x].(*ast.ValueSpec)
if !ok {
log.Println("warning: aliases declaration is not a value")
return
}
if len(spec.Values) != 1 {
log.Println("warning: aliases declaration has multiple values")
}
comp, ok := spec.Values[0].(*ast.CompositeLit)
if !ok {
log.Println("warning: aliases declaration is not a map")
return
}
pi.Aliases = map[string]*Function{}
for _, elem := range comp.Elts {
kv, ok := elem.(*ast.KeyValueExpr)
if !ok {
log.Printf("warning: alias declaration %q is not a map element", elem)
continue
}
k, ok := kv.Key.(*ast.BasicLit)
if !ok || k.Kind != token.STRING {
log.Printf("warning: alias key is not a string literal %q", elem)
continue
}
alias, ok := lit2string(k)
if !ok {
log.Println("warning: malformed name for alias", elem)
continue
}
f, err := getFunction(kv.Value, pi)
if err != nil {
log.Printf("warning, alias malformed: %v", err)
continue
}
pi.Aliases[alias] = f
}
return
}
}
}
func getFunction(exp ast.Expr, pi *PkgInfo) (*Function, error) {
// selector expressions are in LIFO format.
// So, in foo.bar.baz the first selector.Name is
// actually "baz", the second is "bar", and the last is "foo"
var pkg, receiver, funcname string
switch v := exp.(type) {
case *ast.Ident:
// "foo" : Bar
funcname = v.Name
case *ast.SelectorExpr:
// need to handle
// namespace.Func
// import.Func
// import.namespace.Func
// "foo" : ?.bar
funcname = v.Sel.Name
switch x := v.X.(type) {
case *ast.Ident:
// "foo" : baz.bar
// this is either a namespace or package
firstname := x.Name
for _, f := range pi.Funcs {
if firstname == f.Receiver && funcname == f.Name {
return f, nil
}
}
// not a namespace, let's try imported packages
for _, imp := range pi.Imports {
if firstname == imp.Name {
for _, f := range imp.Info.Funcs {
if funcname == f.Name {
return f, nil
}
}
break
}
}
return nil, fmt.Errorf("%q is not a known target", exp)
case *ast.SelectorExpr:
// "foo" : bar.Baz.Bat
// must be package.Namespace.Func
sel, ok := v.X.(*ast.SelectorExpr)
if !ok {
return nil, fmt.Errorf("%q is must denote a target function but was %T", exp, v.X)
}
receiver = sel.Sel.Name
id, ok := sel.X.(*ast.Ident)
if !ok {
return nil, fmt.Errorf("%q is must denote a target function but was %T", exp, v.X)
}
pkg = id.Name
default:
return nil, fmt.Errorf("%q is not valid", exp)
}
default:
return nil, fmt.Errorf("target %s is not a function", exp)
}
if pkg == "" {
for _, f := range pi.Funcs {
if f.Name == funcname && f.Receiver == receiver {
return f, nil
}
}
return nil, fmt.Errorf("unknown function %s.%s", receiver, funcname)
}
for _, imp := range pi.Imports {
if imp.Name == pkg {
for _, f := range imp.Info.Funcs {
if f.Name == funcname && f.Receiver == receiver {
return f, nil
}
}
return nil, fmt.Errorf("unknown function %s.%s.%s", pkg, receiver, funcname)
}
}
return nil, fmt.Errorf("unknown package for function %q", exp)
}
// getPackage returns the importable package at the given path.
func getPackage(path string, files []string, fset *token.FileSet) (*ast.Package, error) {
var filter func(f os.FileInfo) bool
if len(files) > 0 {
fm := make(map[string]bool, len(files))
for _, f := range files {
fm[f] = true
}
filter = func(f os.FileInfo) bool {
return fm[f.Name()]
}
}
pkgs, err := parser.ParseDir(fset, path, filter, parser.ParseComments)
if err != nil {
return nil, fmt.Errorf("failed to parse directory: %v", err)
}
switch len(pkgs) {
case 1:
var pkg *ast.Package
for _, pkg = range pkgs {
}
return pkg, nil
case 0:
return nil, fmt.Errorf("no importable packages found in %s", path)
default:
var names []string
for name := range pkgs {
names = append(names, name)
}
return nil, fmt.Errorf("multiple packages found in %s: %v", path, strings.Join(names, ", "))
}
}
// hasContextParams returns whether or not the first parameter is a context.Context. If it
// determines that hte first parameter makes this function invalid for mage, it'll return a non-nil
// error.
func hasContextParam(ft *ast.FuncType) (bool, error) {
if ft.Params.NumFields() < 1 {
return false, nil
}
param := ft.Params.List[0]
sel, ok := param.Type.(*ast.SelectorExpr)
if !ok {
return false, nil
}
pkg, ok := sel.X.(*ast.Ident)
if !ok {
return false, nil
}
if pkg.Name != "context" {
return false, nil
}
if sel.Sel.Name != "Context" {
return false, nil
}
if len(param.Names) > 1 {
// something like foo, bar context.Context
return false, errors.New("ETOOMANYCONTEXTS")
}
return true, nil
}
func hasVoidReturn(ft *ast.FuncType) bool {
res := ft.Results
return res.NumFields() == 0
}
func hasErrorReturn(ft *ast.FuncType) (bool, error) {
res := ft.Results
if res.NumFields() == 0 {
// void return is ok
return false, nil
}
if res.NumFields() > 1 {
return false, errors.New("ETOOMANYRETURNS")
}
ret := res.List[0]
if len(ret.Names) > 1 {
return false, errors.New("ETOOMANYERRORS")
}
if fmt.Sprint(ret.Type) == "error" {
return true, nil
}
return false, errors.New("EBADRETURNTYPE")
}
func funcType(ft *ast.FuncType) (*Function, error) {
var err error
f := &Function{}
f.IsContext, err = hasContextParam(ft)
if err != nil {
return nil, err
}
f.IsError, err = hasErrorReturn(ft)
if err != nil {
return nil, err
}
x := 0
if f.IsContext {
x++
}
for ; x < len(ft.Params.List); x++ {
param := ft.Params.List[x]
t := fmt.Sprint(param.Type)
typ, ok := argTypes[t]
if !ok {
return nil, fmt.Errorf("unsupported argument type: %s", t)
}
// support for foo, bar string
for _, name := range param.Names {
f.Args = append(f.Args, Arg{Name: name.Name, Type: typ})
}
}
return f, nil
}
func toOneLine(s string) string {
return strings.TrimSpace(strings.Replace(s, "\n", " ", -1))
}
var argTypes = map[string]string{
"string": "string",
"int": "int",
"&{time Duration}": "time.Duration",
"bool": "bool",
}