/
types_struct.go
70 lines (61 loc) · 1.13 KB
/
types_struct.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
package gotype
import (
"bytes"
)
type typeStruct struct {
typeBase
fields types // fields
}
func (t *typeStruct) String() string {
buf := bytes.NewBuffer(nil)
buf.WriteString("struct {")
if len(t.fields) != 0 {
buf.WriteByte('\n')
}
for _, v := range t.fields {
buf.WriteString(v.String())
buf.WriteByte('\n')
}
buf.WriteString("}\n")
return buf.String()
}
func (t *typeStruct) Kind() Kind {
return Struct
}
func (t *typeStruct) NumField() int {
return t.fields.Len()
}
func (t *typeStruct) Field(i int) Type {
return t.fields.Index(i)
}
func (t *typeStruct) FieldByName(name string) (Type, bool) {
anonymo := []int{}
for i, v := range t.fields {
fieldName := v.Name()
if fieldName == name {
return v, true
}
if v.IsAnonymous() {
anonymo = append(anonymo, i)
}
}
for _, i := range anonymo {
v := t.fields[i]
t, ok := v.FieldByName(name)
if ok {
return t, true
}
}
return nil, false
}
func (t *typeStruct) MethodByName(name string) (Type, bool) {
for _, v := range t.fields {
if v.IsAnonymous() {
b, ok := v.MethodByName(name)
if ok {
return b, true
}
}
}
return nil, false
}