-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment_bindings.go
460 lines (371 loc) · 12.6 KB
/
environment_bindings.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Code generated by tygor; DO NOT EDIT.
// ˮsummaryˮ
package environment
import (
"errors"
"fmt"
"github.com/dop251/goja"
"go.k6.io/k6/js/modules"
"time"
)
// k6Module represents k6 JavaScript extension module.
type k6Module struct {
goModuleConstructor goModuleConstructor
}
// NewModuleInstance creates new per VU module instance.
func (m *k6Module) NewModuleInstance(vu modules.VU) modules.Instance {
mi := new(k6ModuleInstance)
adaptee := m.goModuleConstructor(vu)
adapter := jsModuleFrom(adaptee)
vm := vu.Runtime()
dict := make(map[string]interface{})
dict["Environment"] = vm.ToValue(newEnvironmentConstructor(adaptee.newEnvironment))
mi.exports.Named = dict
obj := adapter.defaultEnvironmentGetter(goja.FunctionCall{This: goja.Undefined()}, vm).ToObject(vm)
mi.exports.Default = obj
return mi
}
// register registers k6 JavaScript extension module.
func register(ctor goModuleConstructor) {
m := new(k6Module)
m.goModuleConstructor = ctor
modules.Register("k6/x/environment", m)
}
// k6ModuleInstance represents per VU module instance.
type k6ModuleInstance struct {
exports modules.Exports
}
// Exports returns exported symbols.
func (mi *k6ModuleInstance) Exports() modules.Exports {
return mi.exports
}
// jsModule is the go binding for the JavaScript Module type.
//
// TSDoc:
// ˮsummaryˮ
type jsModule interface {
// defaultEnvironmentGetter is the go getter binding for the JavaScript defaultEnvironment property.
//
// TSDoc:
// Default Environment instance.
defaultEnvironmentGetter(call goja.FunctionCall, vm *goja.Runtime) goja.Value
}
// jsModuleAdapter converts goModule to jsModule.
type jsModuleAdapter struct {
adaptee goModule
}
var _ jsModule = (*jsModuleAdapter)(nil)
// defaultEnvironmentGetter is a jsModule property getter adapter method.
func (self *jsModuleAdapter) defaultEnvironmentGetter(call goja.FunctionCall, vm *goja.Runtime) goja.Value {
v, err := self.adaptee.defaultEnvironmentGetter()
if err != nil {
panic(err)
}
return goEnvironmentToObject(v, vm)
}
// jsModuleFrom returns a jsModule based on a goModule.
func jsModuleFrom(adaptee goModule) jsModule {
return &jsModuleAdapter{adaptee: adaptee}
}
// goModule is the go representation of the JavaScript Module type.
//
// TSDoc:
// ˮsummaryˮ
type goModule interface {
// newEnvironment is the go factory method for the Environment type.
newEnvironment(paramsArg interface{}) (goEnvironment, error)
// defaultEnvironmentGetter is the go getter method for the defaultEnvironment property.
//
// TSDoc:
// Default Environment instance.
defaultEnvironmentGetter() (goEnvironment, error)
}
// goModuleConstructor creates new goModule instance.
type goModuleConstructor func(vu modules.VU) goModule
// jsEnvironment is the go binding for the JavaScript Environment type.
//
// TSDoc:
// This is the primary class of the environment extension.
type jsEnvironment interface {
// initMethod is the go binding for the JavaScript init method.
//
// TSDoc:
// init creates an Environment as defined in constructor.
initMethod(call goja.FunctionCall, vm *goja.Runtime) goja.Value
// deleteMethod is the go binding for the JavaScript delete method.
//
// TSDoc:
// delete removes an existing Environment.
deleteMethod(call goja.FunctionCall, vm *goja.Runtime) goja.Value
// applyMethod is the go binding for the JavaScript apply method.
//
// TSDoc:
// apply reads the contents of the file and applies them to the virtual cluster.
applyMethod(call goja.FunctionCall, vm *goja.Runtime) goja.Value
// applySpecMethod is the go binding for the JavaScript applySpec method.
//
// TSDoc:
// applySpec applies the spec to the virtual cluster.
applySpecMethod(call goja.FunctionCall, vm *goja.Runtime) goja.Value
// waitMethod is the go binding for the JavaScript wait method.
//
// TSDoc:
// `wait` method blocks execution of the test iteration until a certain condition
// is reached or until a timeout. There are 3 major types of conditions now:
//
// 1. Wait until a given Kubernetes event.
//
// 2. Wait until a given `.status.conditions[]` reaches a given value.
//
// 3. Wait until a custom field in `.status` reaches a given value.
waitMethod(call goja.FunctionCall, vm *goja.Runtime) goja.Value
// getNMethod is the go binding for the JavaScript getN method.
//
// TSDoc:
// getN is a substitute for get(), hopefully temporary. See [tygor's](https://github.com/szkiba/tygor) roadmap about support for arrays.
getNMethod(call goja.FunctionCall, vm *goja.Runtime) goja.Value
}
// goEnvironment is the go representation of the JavaScript Environment type.
//
// TSDoc:
// This is the primary class of the environment extension.
type goEnvironment interface {
// initMethod is the go representation of the init method.
//
// TSDoc:
// init creates an Environment as defined in constructor.
initMethod() (interface{}, error)
// deleteMethod is the go representation of the delete method.
//
// TSDoc:
// delete removes an existing Environment.
deleteMethod() (interface{}, error)
// applyMethod is the go representation of the apply method.
//
// TSDoc:
// apply reads the contents of the file and applies them to the virtual cluster.
applyMethod(fileArg string) (interface{}, error)
// applySpecMethod is the go representation of the applySpec method.
//
// TSDoc:
// applySpec applies the spec to the virtual cluster.
applySpecMethod(specArg string) (interface{}, error)
// waitMethod is the go representation of the wait method.
//
// TSDoc:
// `wait` method blocks execution of the test iteration until a certain condition
// is reached or until a timeout. There are 3 major types of conditions now:
//
// 1. Wait until a given Kubernetes event.
//
// 2. Wait until a given `.status.conditions[]` reaches a given value.
//
// 3. Wait until a custom field in `.status` reaches a given value.
waitMethod(conditionArg interface{}, optsArg interface{}) (interface{}, error)
// getNMethod is the go representation of the getN method.
//
// TSDoc:
// getN is a substitute for get(), hopefully temporary. See [tygor's](https://github.com/szkiba/tygor) roadmap about support for arrays.
getNMethod(typeArg string, optsArg interface{}) (float64, error)
}
// jsEnvironmentAdapter converts goEnvironment to jsEnvironment.
type jsEnvironmentAdapter struct {
adaptee goEnvironment
}
var _ jsEnvironment = (*jsEnvironmentAdapter)(nil)
// initMethod is a jsEnvironment adapter method.
func (self *jsEnvironmentAdapter) initMethod(call goja.FunctionCall, vm *goja.Runtime) goja.Value {
v, err := self.adaptee.initMethod()
if err != nil {
panic(err)
}
return vm.ToValue(v)
}
// deleteMethod is a jsEnvironment adapter method.
func (self *jsEnvironmentAdapter) deleteMethod(call goja.FunctionCall, vm *goja.Runtime) goja.Value {
v, err := self.adaptee.deleteMethod()
if err != nil {
panic(err)
}
return vm.ToValue(v)
}
// applyMethod is a jsEnvironment adapter method.
func (self *jsEnvironmentAdapter) applyMethod(call goja.FunctionCall, vm *goja.Runtime) goja.Value {
v, err := self.adaptee.applyMethod(call.Argument(0).String())
if err != nil {
panic(err)
}
return vm.ToValue(v)
}
// applySpecMethod is a jsEnvironment adapter method.
func (self *jsEnvironmentAdapter) applySpecMethod(call goja.FunctionCall, vm *goja.Runtime) goja.Value {
v, err := self.adaptee.applySpecMethod(call.Argument(0).String())
if err != nil {
panic(err)
}
return vm.ToValue(v)
}
// waitMethod is a jsEnvironment adapter method.
func (self *jsEnvironmentAdapter) waitMethod(call goja.FunctionCall, vm *goja.Runtime) goja.Value {
v, err := self.adaptee.waitMethod(call.Argument(0).Export(), call.Argument(1).Export())
if err != nil {
panic(err)
}
return vm.ToValue(v)
}
// getNMethod is a jsEnvironment adapter method.
func (self *jsEnvironmentAdapter) getNMethod(call goja.FunctionCall, vm *goja.Runtime) goja.Value {
v, err := self.adaptee.getNMethod(call.Argument(0).String(), call.Argument(1).Export())
if err != nil {
panic(err)
}
return vm.ToValue(v)
}
// goEnvironmentAdapter converts goja Object to goEnvironment.
type goEnvironmentAdapter struct {
adaptee *goja.Object
vm *goja.Runtime
}
var _ goEnvironment = (*goEnvironmentAdapter)(nil)
// initMethod is a init adapter method.
func (self *goEnvironmentAdapter) initMethod() (interface{}, error) {
fun, ok := goja.AssertFunction(self.adaptee.Get("init"))
if !ok {
return nil, fmt.Errorf("%w: init", errors.ErrUnsupported)
}
res, err := fun(self.adaptee)
if err != nil {
return nil, err
}
return res.Export(), nil
}
// deleteMethod is a delete adapter method.
func (self *goEnvironmentAdapter) deleteMethod() (interface{}, error) {
fun, ok := goja.AssertFunction(self.adaptee.Get("delete"))
if !ok {
return nil, fmt.Errorf("%w: delete", errors.ErrUnsupported)
}
res, err := fun(self.adaptee)
if err != nil {
return nil, err
}
return res.Export(), nil
}
// applyMethod is a apply adapter method.
func (self *goEnvironmentAdapter) applyMethod(fileArg string) (interface{}, error) {
fun, ok := goja.AssertFunction(self.adaptee.Get("apply"))
if !ok {
return nil, fmt.Errorf("%w: apply", errors.ErrUnsupported)
}
res, err := fun(self.adaptee)
if err != nil {
return nil, err
}
return res.Export(), nil
}
// applySpecMethod is a applySpec adapter method.
func (self *goEnvironmentAdapter) applySpecMethod(specArg string) (interface{}, error) {
fun, ok := goja.AssertFunction(self.adaptee.Get("applySpec"))
if !ok {
return nil, fmt.Errorf("%w: applySpec", errors.ErrUnsupported)
}
res, err := fun(self.adaptee)
if err != nil {
return nil, err
}
return res.Export(), nil
}
// waitMethod is a wait adapter method.
func (self *goEnvironmentAdapter) waitMethod(conditionArg interface{}, optsArg interface{}) (interface{}, error) {
fun, ok := goja.AssertFunction(self.adaptee.Get("wait"))
if !ok {
return nil, fmt.Errorf("%w: wait", errors.ErrUnsupported)
}
res, err := fun(self.adaptee)
if err != nil {
return nil, err
}
return res.Export(), nil
}
// getNMethod is a getN adapter method.
func (self *goEnvironmentAdapter) getNMethod(typeArg string, optsArg interface{}) (float64, error) {
fun, ok := goja.AssertFunction(self.adaptee.Get("getN"))
if !ok {
return 0, fmt.Errorf("%w: getN", errors.ErrUnsupported)
}
res, err := fun(self.adaptee)
if err != nil {
return 0, err
}
return res.ToFloat(), nil
}
// jsEnvironmentTo setup Environment JavaScript object from jsEnvironment.
func jsEnvironmentTo(src jsEnvironment, obj *goja.Object, vm *goja.Runtime) error {
if err := obj.Set("init", src.initMethod); err != nil {
return err
}
if err := obj.Set("delete", src.deleteMethod); err != nil {
return err
}
if err := obj.Set("apply", src.applyMethod); err != nil {
return err
}
if err := obj.Set("applySpec", src.applySpecMethod); err != nil {
return err
}
if err := obj.Set("wait", src.waitMethod); err != nil {
return err
}
return obj.Set("getN", src.getNMethod)
}
// jsEnvironmentFrom returns a jsEnvironment based on a goEnvironment.
func jsEnvironmentFrom(adaptee goEnvironment) jsEnvironment {
return &jsEnvironmentAdapter{adaptee: adaptee}
}
// goEnvironmentFrom returns a goEnvironment from goja Object.
func goEnvironmentFrom(adaptee *goja.Object, vm *goja.Runtime) goEnvironment {
return &goEnvironmentAdapter{adaptee: adaptee, vm: vm}
}
// goEnvironmentToObject returns a goja Object from goEnvironment.
func goEnvironmentToObject(v goEnvironment, vm *goja.Runtime) *goja.Object {
obj := vm.NewObject()
err := jsEnvironmentTo(jsEnvironmentFrom(v), obj, vm)
if err != nil {
panic(err)
}
return obj
}
// goEnvironmentConstructor creates new goEnvironment instance.
type goEnvironmentConstructor func(paramsArg interface{}) (goEnvironment, error)
// newEnvironmentConstructor creates Environment JavaScript constructor.
func newEnvironmentConstructor(ctor goEnvironmentConstructor) func(call goja.ConstructorCall, vm *goja.Runtime) *goja.Object {
return func(call goja.ConstructorCall, vm *goja.Runtime) *goja.Object {
adaptee, err := ctor(call.Argument(0).Export())
if err != nil {
panic(err)
}
adapter := jsEnvironmentFrom(adaptee)
if err := jsEnvironmentTo(adapter, call.This, vm); err != nil {
panic(err)
}
return nil
}
}
func goTimeFromDate(v goja.Value, vm *goja.Runtime) time.Time {
getTime, ok := goja.AssertFunction(v.ToObject(vm).Get("getTime"))
if !ok {
panic(fmt.Errorf("%w: getTime", errors.ErrUnsupported))
}
ret, err := getTime(v)
if err != nil {
panic(err)
}
return time.UnixMilli(ret.ToInteger())
}
func jsDateFromTime(t time.Time, vm *goja.Runtime) goja.Value {
d, err := vm.New(vm.Get("Date"), vm.ToValue(t.UnixMilli()))
if err != nil {
panic(err)
}
return d
}