-
Notifications
You must be signed in to change notification settings - Fork 33
/
struct.go
175 lines (136 loc) · 3.11 KB
/
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package bpl
import (
"bufio"
"fmt"
"reflect"
"strings"
"github.com/qiniu/x/log"
)
// -----------------------------------------------------------------------------
type ret func(ctx *Context) (v interface{}, err error)
func (p ret) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
v, err = p(ctx)
if err != nil {
return
}
ctx.dom = v
return
}
func (p ret) RetType() reflect.Type {
return TyInterface
}
func (p ret) SizeOf() int {
return -1
}
// Return returns a matching unit that returns fnRet(ctx).
//
func Return(fnRet func(ctx *Context) (v interface{}, err error)) Ruler {
return ret(fnRet)
}
// -----------------------------------------------------------------------------
// A Member is typeinfo of a `Struct` member.
//
type Member struct {
Name string
Type Ruler
}
// Match is required by a matching unit. see Ruler interface.
//
func (p *Member) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
v, err = p.Type.Match(in, ctx.NewSub())
if err != nil {
return
}
if p.Name != "_" {
ctx.SetVar(p.Name, v)
}
return
}
// RetType returns matching result type.
//
func (p *Member) RetType() reflect.Type {
return p.Type.RetType()
}
// SizeOf is required by a matching unit. see Ruler interface.
//
func (p *Member) SizeOf() int {
return p.Type.SizeOf()
}
// -----------------------------------------------------------------------------
type structType struct {
rulers []Ruler
size int
}
func (p *structType) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
for _, r := range p.rulers {
_, err = r.Match(in, ctx)
if err != nil {
return
}
}
return ctx.Dom(), nil
}
func (p *structType) RetType() reflect.Type {
return TyInterface
}
func (p *structType) SizeOf() int {
if p.size == -2 {
p.size = p.sizeof()
}
return p.size
}
func (p *structType) sizeof() int {
size := 0
for _, r := range p.rulers {
if n := r.SizeOf(); n < 0 {
size = -1
break
} else {
size += n
}
}
return size
}
// Struct returns a compound matching unit.
//
func Struct(members []Ruler) Ruler {
n := len(members)
if n == 0 {
return Nil
}
return &structType{rulers: members, size: -2}
}
// -----------------------------------------------------------------------------
func structFrom(t reflect.Type) (r Ruler, err error) {
n := t.NumField()
rulers := make([]Ruler, n)
for i := 0; i < n; i++ {
sf := t.Field(i)
r, err = TypeFrom(sf.Type)
if err != nil {
log.Warn("bpl.TypeFrom failed:", err)
return
}
rulers[i] = &Member{Name: strings.ToLower(sf.Name), Type: r}
}
return Struct(rulers), nil
}
// TypeFrom creates a matching unit from a Go type.
//
func TypeFrom(t reflect.Type) (r Ruler, err error) {
retry:
kind := t.Kind()
switch {
case kind == reflect.Struct:
return structFrom(t)
case kind >= reflect.Int8 && kind <= reflect.Float64:
return BaseType(kind), nil
case kind == reflect.String:
return CString, nil
case kind == reflect.Ptr:
t = t.Elem()
goto retry
}
return nil, fmt.Errorf("bpl.TypeFrom: unsupported type - %v", t)
}
// -----------------------------------------------------------------------------