-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprotocol.go
157 lines (122 loc) · 2.87 KB
/
protocol.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
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
}
switch rhs {
case "SizeOf":
return lhs.Size(), true
case "StartOf":
return lhs.Start(), true
case "ParentOf":
return lhs.Parent(), true
case "EndOf":
return lhs.End(), true
default:
return lhs.Get(rhs)
}
}
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
}
}
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
}
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
}