/
types_interface.go
65 lines (55 loc) · 1.21 KB
/
types_interface.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
package gotype
import "bytes"
type typeInterface struct {
typeBase
methods types // 这个类型的方法集合
anonymo types // 组合的接口
}
func (t *typeInterface) String() string {
buf := bytes.NewBuffer(nil)
buf.WriteString("interface {")
if len(t.anonymo)+len(t.methods) != 0 {
buf.WriteByte('\n')
}
for _, v := range t.anonymo {
buf.WriteString(v.String())
buf.WriteByte('\n')
}
for _, v := range t.methods {
buf.WriteString(v.String())
buf.WriteByte('\n')
}
buf.WriteString("}\n")
return buf.String()
}
func (t *typeInterface) Kind() Kind {
return Interface
}
func (t *typeInterface) NumMethods() int {
return t.methods.Len()
}
func (t *typeInterface) Methods(i int) Type {
return t.methods.Index(i)
}
func (t *typeInterface) MethodsByName(name string) (Type, bool) {
b, ok := t.methods.Search(name)
if ok {
return b, true
}
for _, v := range t.anonymo {
b, ok := v.MethodsByName(name)
if ok {
return b, true
}
}
return nil, false
}
func (t *typeInterface) NumAnonymo() int {
return t.anonymo.Len()
}
func (t *typeInterface) Anonymo(i int) Type {
return t.anonymo.Index(i)
}
func (t *typeInterface) AnonymoByName(name string) (Type, bool) {
return t.anonymo.Search(name)
}