/
template.go
288 lines (233 loc) · 6.96 KB
/
template.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package container
import (
"fmt"
"reflect"
"strings"
"github.com/pspaces/gospace/function"
)
// Template structure used for matching against tuples.
// Template is a tuple with type information used for pattern matching.
type Template struct {
Flds []interface{} `bson:"fields" json:"fields" xml:"fields"`
}
// NewTemplate creates a template from the variadic fields provided.
// NewTemplate encapsulates the types of pointer values.
func NewTemplate(fields ...interface{}) (tp Template) {
tempfields := make([]interface{}, len(fields))
copy(tempfields, fields)
// Replace pointers with reflect.Type value used to match type.
for i, value := range fields {
if value != nil {
if reflect.TypeOf(value).Kind() == reflect.Ptr {
// Encapsulate the parameter with a TypeField.
tempfields[i] = CreateTypeField(reflect.ValueOf(value).Elem().Interface())
} else if function.IsFunc(value) && reflect.TypeOf(value).Kind() == reflect.Interface {
tempfields[i] = CreateTypeField(value)
}
}
}
tp = Template{tempfields}
return tp
}
// Equal returns true if both templates tp and tq are strictly equal, and false otherwise.
func (tp *Template) Equal(tq *Template) (e bool) {
e = false
if tp == nil && tp == tq {
e = true
} else if tp != nil && tq != nil {
e = tp.Length() == tq.Length()
length := tp.Length()
for i := 0; i < length && e; i++ {
tpf := tp.GetFieldAt(i)
tqf := tq.GetFieldAt(i)
if reflect.TypeOf(tpf) == reflect.TypeOf(TypeField{}) && reflect.TypeOf(tqf) == reflect.TypeOf(TypeField{}) {
atpf := tpf.(TypeField)
btpf := tqf.(TypeField)
e = e && atpf.Equal(btpf)
} else if function.IsFunc(tpf) && function.IsFunc(tqf) {
e = e && function.Name(tpf) == function.Name(tqf) && function.Signature(tpf) == function.Signature(tqf)
} else {
e = e && reflect.DeepEqual(tpf, tqf)
}
}
}
return e
}
// Match returns true if both templates tp and tq are equivalent up to their types, and false otherwise.
func (tp *Template) Match(tq *Template) (e bool) {
e = false
if tp == nil && tp == tq {
e = true
} else if tp != nil && tq != nil {
e = tp.Length() == tq.Length()
length := tp.Length()
for i := 0; i < length && e; i++ {
tpf := tp.GetFieldAt(i)
tqf := tq.GetFieldAt(i)
if reflect.TypeOf(tpf) == reflect.TypeOf(TypeField{}) || reflect.TypeOf(tqf) == reflect.TypeOf(TypeField{}) {
atpf, ais := tpf.(TypeField)
btpf, bis := tqf.(TypeField)
if ais && bis {
e = e && atpf.Equal(btpf)
} else if ais {
e = e && reflect.TypeOf(tqf) == atpf.GetType()
} else if bis {
e = e && reflect.TypeOf(tpf) == btpf.GetType()
}
} else if function.IsFunc(tpf) && function.IsFunc(tqf) {
e = e && function.Name(tpf) == function.Name(tqf) && function.Signature(tpf) == function.Signature(tqf)
} else {
e = e && reflect.DeepEqual(tpf, tqf)
}
}
}
return e
}
// ExactMatch returns true if both templates tp and tq are equivalent up to their types and the maximum amount of concrete values matched w.r.t. tq given the amount of concrete values in tp.
func (tp *Template) ExactMatch(tq *Template) (e bool, pno, qno uint) {
pno, qno = 0, 0
e = false
if tp == nil && tp == tq {
pno++
qno++
e = true
} else if tp != nil && tq != nil {
e = tp.Length() == tq.Length()
length := tp.Length()
for i := 0; i < length && e; i++ {
tpf := tp.GetFieldAt(i)
tqf := tq.GetFieldAt(i)
if reflect.TypeOf(tpf) == reflect.TypeOf(TypeField{}) || reflect.TypeOf(tqf) == reflect.TypeOf(TypeField{}) {
atpf, ais := tpf.(TypeField)
btpf, bis := tqf.(TypeField)
if ais && bis {
e = e && atpf.Equal(btpf)
if e {
pno++
}
} else if ais {
e = e && reflect.TypeOf(tqf) == atpf.GetType()
} else if bis {
e = e && reflect.TypeOf(tpf) == btpf.GetType()
}
if e {
qno++
}
} else if function.IsFunc(tpf) && function.IsFunc(tqf) {
nm := function.Name(tpf) == function.Name(tqf)
sm := function.Signature(tpf) == function.Signature(tqf)
e = e && nm && sm
if e {
pno++
}
if e && sm {
qno++
}
} else {
e = e && reflect.DeepEqual(tpf, tqf)
if e {
pno++
qno++
}
}
}
}
return e, pno, qno
}
// Length returns the size of the template.
func (tp *Template) Length() (sz int) {
sz = -1
if tp != nil {
sz = len((*tp).Flds)
}
return sz
}
// Fields returns the fields of the template.
func (tp *Template) Fields() (flds []interface{}) {
if tp != nil {
flds = (*tp).Flds
}
return flds
}
// GetFieldAt returns the i'th field of the template.
func (tp *Template) GetFieldAt(i int) (fld interface{}) {
if tp != nil {
fld = (*tp).Flds[i]
}
return fld
}
// setFieldAt sets the i'th field of the template to the value of val.
// setFieldAt returns true if the field is set, and false otherwise.
func (tp *Template) setFieldAt(i int, val interface{}) (b bool) {
b = false
if tp != nil {
(*tp).Flds[i] = val
b = true
}
return b
}
// Apply iterates through the template tp and applies the function fun to each field.
// Apply returns true function fun could be applied to all the fields, and false otherwise.
func (tp *Template) Apply(fun func(field interface{}) interface{}) (b bool) {
b = false
if tp != nil {
b = true
for i := 0; i < tp.Length(); i++ {
b = b && tp.setFieldAt(i, fun(tp.GetFieldAt(i)))
}
}
return b
}
// NewTuple returns a new tuple t from the template.
// NewTuple initializes all tuple fields in t with empty values depending on types in the template.
func (tp *Template) NewTuple() (t Tuple) {
var element interface{}
param := make([]interface{}, tp.Length())
for i := range param {
field := tp.GetFieldAt(i)
if field != nil {
if reflect.TypeOf(field) == reflect.TypeOf(TypeField{}) {
tf := reflect.ValueOf(field).Interface().(TypeField)
rt := (tf.GetType()).(reflect.Type)
element = reflect.New(rt).Elem().Interface()
} else {
ptf := reflect.TypeOf(field)
element = reflect.New(ptf).Elem().Interface()
}
} else {
element = field
}
param[i] = element
}
t = NewTuple(param...)
return t
}
// GetParenthesisType returns a pair of strings that encapsulates the template.
// GetParenthesisType is used in the String() method.
func (tp Template) GetParenthesisType() (string, string) {
return "(", ")"
}
// GetDelimiter returns the delimiter used to seperated the template fields.
// GetParenthesisType is used in the String() method.
func (tp Template) GetDelimiter() string {
return ", "
}
// String returns a print friendly representation of the template.
func (tp Template) String() string {
lp, rp := tp.GetParenthesisType()
delim := tp.GetDelimiter()
strs := make([]string, tp.Length())
for i := range strs {
field := tp.GetFieldAt(i)
if field != nil {
if reflect.TypeOf(field).Kind() == reflect.String {
strs[i] = fmt.Sprintf("%s%s%s", "\"", field, "\"")
} else {
strs[i] = fmt.Sprintf("%v", field)
}
} else {
strs[i] = "nil"
}
}
return fmt.Sprintf("%s%s%s", lp, strings.Join(strs, delim), rp)
}