-
Notifications
You must be signed in to change notification settings - Fork 0
/
callExpr.go
104 lines (92 loc) · 2.63 KB
/
callExpr.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
package checksetter
import (
"fmt"
"go/ast"
"strings"
)
// funcDetails records details about a function grouping them into a single
// place for convenience
type funcDetails struct {
name string
setName string
expectedArgs string
}
// getFuncDetails will return the function name and expected arguments and
// return any errors
func getFuncDetails(e *ast.CallExpr, funcSet string) (funcDetails, error) {
fd := funcDetails{setName: funcSet}
nameToArgs, ok := mapOfNameToArgMaps[funcSet]
if !ok {
return fd, fmt.Errorf("unknown function set: %s", funcSet)
}
fID, ok := e.Fun.(*ast.Ident)
if !ok {
return fd, fmt.Errorf("%s: syntax error: unexpected call type: %T",
funcSet, e.Fun)
}
fd.name = fID.Name
expArgs, ok := nameToArgs[fd.name]
if !ok {
return fd, fmt.Errorf("unknown %s: %s", funcSet, fd.name)
}
fd.expectedArgs = expArgs
return fd, nil
}
// checkArgCount will return an error if the number of arguments in the
// CallExpr is not equal to the given value, nil otherwise
func checkArgCount(e *ast.CallExpr, n int) error {
if len(e.Args) != n {
return fmt.Errorf("the call has %d arguments, it should have %d",
len(e.Args), n)
}
return nil
}
// getArg retrieves the idx'th argument from the call expression
func getArg(e *ast.CallExpr, idx int) (ast.Expr, error) {
if idx >= len(e.Args) {
return nil, fmt.Errorf("can't get argument %d, too few parameters", idx)
}
return e.Args[idx], nil
}
// getArgAsInt gets an int64 arg from the list of arguments to the function
// returning an error if there are any problems
func getArgAsInt(e *ast.CallExpr, idx int) (int64, error) {
argExpr, err := getArg(e, idx)
if err != nil {
return 0, err
}
i, err := getInt(argExpr)
if err != nil {
return 0, fmt.Errorf("can't convert argument %d to an int: %s",
idx, err)
}
return i, nil
}
// getArgAsFloat gets a float64 arg from the list of arguments to the function
// returning an error if there are any problems
func getArgAsFloat(e *ast.CallExpr, idx int) (float64, error) {
argExpr, err := getArg(e, idx)
if err != nil {
return 0.0, err
}
f, err := getFloat(argExpr)
if err != nil {
return 0.0, fmt.Errorf("can't convert argument %d to a float: %s",
idx, err)
}
return f, nil
}
// getArgAsString gets a string arg from the list of arguments to the
// function returning an error if there are any problems
func getArgAsString(e *ast.CallExpr, idx int) (string, error) {
argExpr, err := getArg(e, idx)
if err != nil {
return "", err
}
s, err := getString(argExpr)
if err != nil {
return "", fmt.Errorf("can't convert argument %d to a string: %s",
idx, err)
}
return strings.Trim(s, "\""), nil
}