-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprotocol.go
More file actions
242 lines (191 loc) · 4.6 KB
/
protocol.go
File metadata and controls
242 lines (191 loc) · 4.6 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
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
package vtypes
import (
"context"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter"
)
type StructAssociative struct{}
func (self StructAssociative) Applicable(a vfilter.Any, b vfilter.Any) bool {
switch a.(type) {
case StructObject, *StructObject:
_, ok := b.(string)
if ok {
return true
}
}
return false
}
func (self StructAssociative) Associative(scope vfilter.Scope,
a vfilter.Any, b vfilter.Any) (vfilter.Any, bool) {
lhs, ok := a.(*StructObject)
if !ok {
return vfilter.Null{}, false
}
rhs, ok := b.(string)
if !ok {
return vfilter.Null{}, false
}
// A Struct definition overrides default fields - this way a
// struct may define a field called "Offset" and it will be
// honored but if not defined we return the default offset.
if lhs.HasField(rhs) {
return lhs.Get(rhs)
}
switch rhs {
case "SizeOf", "Size":
return lhs.Size(), true
case "StartOf", "Start", "OffsetOf":
return lhs.Start(), true
case "ParentOf", "Parent":
return lhs.Parent(), true
case "EndOf", "End":
return lhs.End(), true
default:
// scope.Log("No field %v defined on struct %v", b, lhs.TypeName())
return nil, false
}
}
func (self StructAssociative) GetMembers(scope vfilter.Scope, a vfilter.Any) []string {
lhs, ok := a.(*StructObject)
if !ok {
return nil
}
return lhs.parser.field_names
}
type ArrayAssociative struct{}
func (self ArrayAssociative) Applicable(a vfilter.Any, b vfilter.Any) bool {
switch a.(type) {
case ArrayObject, *ArrayObject:
_, ok := b.(string)
if ok {
return true
}
_, ok = to_int64(b)
if ok {
return true
}
}
return false
}
func (self ArrayAssociative) Associative(scope vfilter.Scope,
a vfilter.Any, b vfilter.Any) (vfilter.Any, bool) {
lhs, ok := a.(*ArrayObject)
if !ok {
return vfilter.Null{}, false
}
// Indexing the array
idx, ok := to_int64(b)
if ok {
res, err := lhs.Get(idx)
if err != nil {
return nil, false
}
return res, true
}
rhs, ok := b.(string)
if !ok {
return vfilter.Null{}, false
}
switch rhs {
case "SizeOf":
return lhs.Size(), true
case "ContentsOf":
return lhs.Contents(), true
case "StartOf":
return lhs.Start(), true
case "EndOf":
return lhs.End(), true
// Provide a way to access the raw array
case "Value":
return lhs.contents, true
default:
// Fallback to associative on the underlying array.
return scope.Associative(lhs.contents, b)
}
}
func (self ArrayAssociative) GetMembers(scope vfilter.Scope, a vfilter.Any) []string {
return nil
}
// Arrays also participate in the iterator protocol
type ArrayIterator struct{}
func (self ArrayIterator) Applicable(a vfilter.Any) bool {
_, ok := a.(*ArrayObject)
return ok
}
func (self ArrayIterator) Iterate(
ctx context.Context, scope vfilter.Scope, a vfilter.Any) <-chan vfilter.Row {
output_chan := make(chan vfilter.Row)
go func() {
defer close(output_chan)
obj, ok := a.(*ArrayObject)
if !ok {
return
}
for _, item := range obj.contents {
switch item.(type) {
// We must emit objects with a valid Associative protocol
// because this will form the basis for the columns in
// foreach. These objects are ok to emit directly.
case *ordereddict.Dict, *StructObject:
default:
// Anything else place inside a dict.
item = ordereddict.NewDict().Set("_value", item)
}
select {
case <-ctx.Done():
return
case output_chan <- item:
}
}
}()
return output_chan
}
type StructFieldReferenceAssociative struct{}
func (self StructFieldReferenceAssociative) Applicable(a vfilter.Any, b vfilter.Any) bool {
switch a.(type) {
case StructFieldReference, *StructFieldReference:
_, ok := b.(string)
if ok {
return true
}
}
return false
}
func (self StructFieldReferenceAssociative) Associative(scope vfilter.Scope,
a vfilter.Any, b vfilter.Any) (vfilter.Any, bool) {
lhs, ok := a.(*StructFieldReference)
if !ok {
return vfilter.Null{}, false
}
rhs, ok := b.(string)
if !ok {
return vfilter.Null{}, false
}
switch rhs {
case "SizeOf", "Size":
return lhs.Size(), true
case "StartOf", "Start", "OffsetOf":
return lhs.Start(), true
case "RelOffset":
return lhs.RelOffset(), true
case "RelEndOf":
return lhs.RelOffset() + int64(lhs.Size()), true
case "EndOf", "End":
return lhs.End(), true
case "Value":
return lhs.Value(), true
default:
return nil, false
}
}
func (self StructFieldReferenceAssociative) GetMembers(scope vfilter.Scope, a vfilter.Any) []string {
return nil
}
func GetProtocols() []vfilter.Any {
return []vfilter.Any{
&StructAssociative{},
&ArrayAssociative{},
&ArrayIterator{},
&StructFieldReferenceAssociative{},
}
}