This repository has been archived by the owner on Jul 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable.go
96 lines (90 loc) · 2.03 KB
/
variable.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
package humanize
import (
"go/ast"
"go/token"
)
// Variable is a string represent of a function parameter
type Variable struct {
Name string
Type Type
Docs Docs
caller *ast.CallExpr
indx int
}
func variableFromValue(name string, indx int, e []ast.Expr, src string, f *File, p *Package) *Variable {
var t Type
var caller *ast.CallExpr
var ok bool
first := e[0]
// if the caller is a CallExpr, then late bind will take care of it
if caller, ok = first.(*ast.CallExpr); !ok {
switch data := e[indx].(type) {
case *ast.CompositeLit:
//if data.Type != nil {
// the type is here
t = getType(data.Type, src, f, p)
//}
case *ast.BasicLit:
switch data.Kind {
case token.INT:
t = &IdentType{
srcBase{p, getSource(data, src)},
"int",
}
case token.FLOAT:
t = &IdentType{
srcBase{p, getSource(data, src)},
"float64",
}
case token.IMAG:
t = &IdentType{
srcBase{p, getSource(data, src)},
"complex64",
}
case token.CHAR:
t = &IdentType{
srcBase{p, getSource(data, src)},
"char",
}
case token.STRING:
t = &IdentType{
srcBase{p, getSource(data, src)},
"string",
}
}
//default:
//fmt.Printf("var value => %T", e[indx])
//fmt.Printf("%s", src[data.Pos()-1:data.End()-1])
}
}
return &Variable{
Name: name,
Type: t,
caller: caller,
indx: indx,
}
}
func variableFromExpr(name string, e ast.Expr, src string, f *File, p *Package) *Variable {
return &Variable{
Name: name,
Type: getType(e, src, f, p),
}
}
// NewVariable return an array of variables in the scope
func NewVariable(v *ast.ValueSpec, c *ast.CommentGroup, src string, f *File, p *Package) []*Variable {
var res []*Variable
for i := range v.Names {
name := nameFromIdent(v.Names[i])
var n *Variable
if v.Type != nil {
n = variableFromExpr(name, v.Type, src, f, p)
} else {
if len(v.Values) != 0 {
n = variableFromValue(name, i, v.Values, src, f, p)
}
}
n.Docs = docsFromNodeDoc(c, v.Doc)
res = append(res, n)
}
return res
}