-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod.go
More file actions
54 lines (45 loc) · 1.45 KB
/
method.go
File metadata and controls
54 lines (45 loc) · 1.45 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
package call
import (
"reflect"
)
// Methods is a slice of Method.
type Methods []Method
// Named returns the Method with the following name or ErrNotFound.
func (m Methods) Named(name string) (Method, error) {
for _, elem := range m {
if elem.Name == name {
return elem, nil
}
}
return Method{}, ErrNotFound
}
// Method contains information about a single method on a Go type.
//
// Each instance of Method has an internal *Instance pointer that ties it
// to its receiver. Calling Instance.Rebind() updates these internal pointers
// to the new receiver.
type Method struct {
// Name is the method name.
Name string
// Method is the reflect.Method value.
Method reflect.Method
// A Method is a superset of a Func.
*Func
// The Instance containing the receiver we are tied to.
instance *Instance
}
// Args returns an *Args type where its Values and Pointers members are populated with
// the necessary values to call the method via Method.Call().
//
// Args calls down to Func.Args() but then sets the 0 index of Values and Pointers to
// the correct receiver and nil respectively.
func (m Method) Args() *Args {
args := m.Func.Args()
args.Values[0], args.Pointers[0] = m.instance.receiverValue, nil
return args
}
// Pretty returns a string representing the method-name( args... ) return-value(s).
func (m Method) Pretty() string {
// Get Pretty from Func but replace leading 4 (func) with our method name.
return m.Name + m.Func.Pretty()[4:]
}