-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.go
More file actions
334 lines (272 loc) · 5.75 KB
/
lib.go
File metadata and controls
334 lines (272 loc) · 5.75 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package jsl
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"github.com/dop251/goja"
)
type IterConfig struct {
Pre string
Post string
Iter string
Filter string
Accumulator string
Dedupe string
LibraryFilename string
Emitter func(interface{})
}
type Iterator interface {
Initialize(io.Writer)
PreIteration()
PostIteration()
IterFunc(interface{})
FilterFunc(interface{}) bool
}
type StatType uint
const (
IterOk StatType = 0
IterError StatType = 1
FilterTrue StatType = 2
FilterFalse StatType = 3
FilterError StatType = 4
)
const DEFAULT_JS_CODE = `// I recommend piping this to a package.js and editing from there.
// Pre is run once at the beginning of an interation.
function pre() {
return {};
}
// Filter is run on every row, falsy skips the
// dedupe, iter and accumulator steps.
function filter(i, accum) {
return true;
}
// Dedupe should return a string key, anytime a key
// is seen twice, the iter and accumulator steps are
// skipped.
function dedupe(i) {
return undefined;
}
// Return anything other than undefined and it will be emitted.
function iter(i, accum) {
return i;
}
// Accumulator should return the updated accumulator
// object.
function accumulator(i, accum) {
return accum;
}
// Run once at the end of the iteration.
function post(accum) {
return accum;
}
`
type GojaIterator struct {
VM *goja.Runtime
Accumulator goja.Value
Emitter func(interface{})
dedupeMap map[string]bool
hasFilter bool
hasAccumulator bool
hasIterator bool
hasDedupe bool
}
func NewIterator(ic *IterConfig) (*GojaIterator, error) {
iter := GojaIterator{
dedupeMap: make(map[string]bool, 0),
}
iter.VM = goja.New()
iter.Emitter = ic.Emitter
iter.VM.Set("print", func(call goja.FunctionCall) goja.Value {
var result []byte
result, _ = json.Marshal(call)
log.Printf("%s\n", result)
return nil
})
_, err := iter.VM.RunString(DEFAULT_JS_CODE)
if err != nil {
panic(err)
}
if len(ic.LibraryFilename) > 0 {
iter.hasAccumulator = true
iter.hasIterator = true
iter.hasFilter = true
iter.hasDedupe = true
data, err := ioutil.ReadFile(ic.LibraryFilename)
if err != nil {
panic(err)
}
log.Printf("Loading library file %s (%d)...\n", ic.LibraryFilename, len(data))
_, lib_err := iter.VM.RunString(string(data))
if lib_err != nil {
panic(lib_err)
}
} else {
if len(ic.Iter) == 0 && len(ic.Accumulator) == 0 {
ic.Iter = "i"
}
}
if len(ic.Iter) > 0 {
iter.hasIterator = true
iter.RunString(
fmt.Sprintf(
"function iter(i, accum) { return %s }",
ic.Iter,
),
)
}
if len(ic.Filter) > 0 {
iter.hasFilter = true
iter.RunString(
fmt.Sprintf(
"function filter(i, accum) { return %s }",
ic.Filter,
),
)
}
if len(ic.Accumulator) > 0 {
iter.hasAccumulator = true
iter.RunString(
fmt.Sprintf(
"function accumulator(i, accum) { %s; return accum }",
ic.Accumulator,
),
)
}
if len(ic.Pre) > 0 {
iter.RunString(
fmt.Sprintf(
"function pre() { return %s }",
ic.Pre,
),
)
} else {
iter.RunString("function pre() { return {} }")
}
if len(ic.Post) > 0 {
iter.RunString(
fmt.Sprintf(
"function post(accum) { return %s }",
ic.Post,
),
)
} else {
if iter.hasAccumulator {
iter.RunString(
"function post(accum) { return accum }",
)
} else {
iter.RunString(
"function post(accum) { return null }",
)
}
}
if len(ic.Dedupe) > 0 {
iter.hasDedupe = true
iter.RunString(
fmt.Sprintf(
"function dedupe(i) { return %s }",
ic.Dedupe,
),
)
}
return &iter, nil
}
func (it *GojaIterator) RunString(s string) (goja.Value, error) {
return it.VM.RunString(s)
}
func (it *GojaIterator) PreIteration() error {
value, err := it.VM.RunString("pre()")
if err != nil {
return err
}
it.Accumulator = value
return nil
}
func (it *GojaIterator) PostIteration() error {
it.VM.Set("accum", it.Accumulator)
value, err := it.VM.RunString("post(accum)")
if err != nil {
return err
}
it.Accumulator = value
if goja.IsUndefined(value) || goja.IsNull(value) {
return nil
}
it.Emitter(it.Accumulator)
return nil
}
func (it *GojaIterator) IterFunc(i interface{}) error {
it.VM.Set("i", i)
it.VM.Set("accum", it.Accumulator)
keep, err := it.FilterFunc(i)
if err != nil {
return err
}
if keep == false {
return nil
}
if it.hasDedupe {
value, err := it.VM.RunString("dedupe(i)")
if err != nil {
return err
}
if goja.IsUndefined(value) == false && goja.IsNull(value) == false {
var key string = value.String()
if _, found := it.dedupeMap[key]; found == true {
// We've seen this key before, skip.
return nil
} else {
it.dedupeMap[key] = true
}
}
}
if it.hasIterator {
value, err := it.VM.RunString("iter(i, accum)")
if err != nil {
return err
}
if goja.IsUndefined(value) == false {
it.Emitter(value)
}
}
if it.hasAccumulator {
newAccum, err := it.VM.RunString("accumulator(i, accum)")
it.Accumulator = newAccum
if err != nil {
return err
}
}
return nil
}
func (it *GojaIterator) FilterFunc(i interface{}) (bool, error) {
if it.hasFilter == false {
return true, nil
}
it.VM.Set("i", i)
it.VM.Set("accum", it.Accumulator)
value, err := it.VM.RunString("filter(i, accum)")
if err != nil {
return false, err
}
return value.ToBoolean(), nil
}
func (it *GojaIterator) HandleChannel(input chan interface{}, failOnError bool) error {
var err error
err = it.PreIteration()
if err != nil && failOnError {
return err
}
for i := range input {
err = it.IterFunc(i)
if err != nil && failOnError {
return err
}
}
err = it.PostIteration()
if err != nil && failOnError {
return err
}
return nil
}