-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfn.go
More file actions
104 lines (93 loc) · 2.37 KB
/
fn.go
File metadata and controls
104 lines (93 loc) · 2.37 KB
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
package golang
import (
"fmt"
"go/ast"
"github.com/code-visible/golang/parsedtypes"
"github.com/code-visible/golang/utils"
)
type Callable struct {
ID string `json:"id"`
Pos string `json:"pos"`
Name string `json:"name"`
Signature string `json:"signature"`
Abstract string `json:"abstract"`
File string `json:"file"`
Pkg string `json:"pkg"`
Comment string `json:"comment"`
Parameters []string `json:"parameters"`
Results []string `json:"results"`
Method bool `json:"method"`
Private bool `json:"private"`
abstract string
ident *ast.Ident
recv parsedtypes.Field
params parsedtypes.Fields
results parsedtypes.Fields
file *File
}
func NewCallable(decl *ast.FuncDecl, file *File) *Callable {
pCnt := 0
rCnt := 0
if decl.Type.Params != nil {
pCnt = len(decl.Type.Params.List)
}
if decl.Type.Results != nil {
rCnt = len(decl.Type.Results.List)
}
c := &Callable{
Name: decl.Name.Name,
ident: decl.Name,
params: make(parsedtypes.Fields, 0, pCnt),
results: make(parsedtypes.Fields, 0, rCnt),
file: file,
}
if pCnt > 0 {
for _, pf := range decl.Type.Params.List {
c.params.Parse(pf)
}
}
if rCnt > 0 {
for _, rf := range decl.Type.Results.List {
c.results.Parse(rf)
}
}
if decl.Recv != nil && len(decl.Recv.List) > 0 {
var recv = make(parsedtypes.Fields, 0, 1)
recv.Parse(decl.Recv.List[0])
c.recv = recv[0]
}
return c
}
func (c *Callable) SetupID() {
c.ID = utils.Hash(c.LookupName())
}
func (c *Callable) LookupName() string {
if c.Method {
return fmt.Sprintf("%s:(%s).%s", c.file.LookupName(), c.abstract, c.Name)
}
return fmt.Sprintf("%s:%s", c.file.LookupName(), c.Name)
}
func (c *Callable) Complete() {
c.Parameters = c.params.List()
c.Results = c.results.List()
if c.recv.ID != nil {
c.Method = true
c.abstract = c.recv.Type.Key
}
c.Private = (c.Name[0] >= 'a' && c.Name[0] <= 'z') || c.Name[0] == '_'
parametersStr := c.params.Format(",")
resultsStr := c.results.Format(",")
if c.Method {
c.Signature = fmt.Sprintf("(%s).%s(%s) -> (%s)", c.abstract, c.Name, parametersStr, resultsStr)
} else {
c.Signature = fmt.Sprintf("%s(%s) -> (%s)", c.Name, parametersStr, resultsStr)
}
}
func (c *Callable) SetupMethod() {
if c.Method {
abs := c.file.pkg.LookupAbstract(c.abstract)
if abs != nil {
c.Abstract = abs.ID
}
}
}