forked from goplus/gop
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelper.gop
69 lines (66 loc) · 1.11 KB
/
helper.gop
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
import (
"go/ast"
)
func importSpec(path string) *ast.ImportSpec {
return {
Path: {
Value: path.quote,
},
}
}
func rmExargs(list []*ast.Field, exargs int) []*ast.Field {
if exargs == 0 {
return list
}
n := len(list)
ret := make([]*ast.Field, n)
for i, f <- list {
ret[i] = f
}
for n > 0 {
f := ret[n-1]
if c := len(f.Names); c > exargs {
f.Names = f.Names[:c-exargs]
break
} else {
exargs -= c
}
n--
}
return ret[:n]
}
func toParams(params *ast.FieldList, at string) *ast.FieldList {
if params == nil {
return nil
}
list := make([]*ast.Field, len(params.List))
for i, p <- params.List {
typ := p.Type
switch t := typ.(type) {
case *ast.Ident:
if t.isExported {
typ = &ast.SelectorExpr{
X: ast.newIdent(at),
Sel: t,
}
}
case *ast.StarExpr:
if x, ok := t.X.(*ast.Ident); ok && x.isExported {
typ = &ast.StarExpr{
X: &ast.SelectorExpr{
X: ast.newIdent(at),
Sel: x,
},
}
}
}
list[i] = {
Doc: p.Doc,
Names: p.Names,
Type: typ,
Tag: p.Tag,
Comment: p.Comment,
}
}
return {List: list}
}