This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
forked from gocassa/gocassa
-
Notifications
You must be signed in to change notification settings - Fork 13
/
mock.go
1081 lines (905 loc) · 25.2 KB
/
mock.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gocassa
import (
"bytes"
"errors"
"fmt"
"reflect"
"strings"
"sync"
"context"
"github.com/gocql/gocql"
"github.com/google/btree"
)
// MockKeySpace implements the KeySpace interface and constructs in-memory tables.
type mockKeySpace struct {
k
}
type mockOp struct {
options Options
funcs []func(mockOp) error
preflightErr error
}
func newOp(f func(mockOp) error) mockOp {
return mockOp{
funcs: []func(mockOp) error{f},
}
}
func (m mockOp) Add(ops ...Op) Op {
return mockMultiOp{m}.Add(ops...)
}
func (m mockOp) Run() error {
for _, f := range m.funcs {
err := f(m)
if err != nil {
return err
}
}
return nil
}
func (m mockOp) RunWithContext(ctx context.Context) error {
return m.WithOptions(Options{Context: ctx}).Run()
}
func (m mockOp) Options() Options {
return m.options
}
func (m mockOp) WithOptions(opt Options) Op {
return mockOp{
options: m.options.Merge(opt),
funcs: m.funcs,
}
}
func (m mockOp) RunAtomically() error {
return m.Run()
}
func (m mockOp) RunLoggedBatchWithContext(ctx context.Context) error {
return m.WithOptions(Options{Context: ctx}).Run()
}
func (m mockOp) RunAtomicallyWithContext(ctx context.Context) error {
return m.RunLoggedBatchWithContext(ctx)
}
func (m mockOp) GenerateStatement() Statement {
return noOpStatement{}
}
func (m mockOp) QueryExecutor() QueryExecutor {
return nil
}
func (m mockOp) Preflight() error {
return m.preflightErr
}
type mockMultiOp []Op
func (mo mockMultiOp) Run() error {
if err := mo.Preflight(); err != nil {
return err
}
for i, op := range mo {
errorInjector := getErrorInjector(op.Options().Context)
if errToReturn := errorInjector.shouldReturnErr(op, i, len(mo)); errToReturn != nil {
return errToReturn
}
if err := op.Run(); err != nil {
return err
}
}
return nil
}
func (mo mockMultiOp) RunWithContext(ctx context.Context) error {
return mo.WithOptions(Options{Context: ctx}).Run()
}
func (mo mockMultiOp) RunAtomically() error {
return mo.Run()
}
func (mo mockMultiOp) RunLoggedBatchWithContext(ctx context.Context) error {
return mo.WithOptions(Options{Context: ctx}).Run()
}
func (mo mockMultiOp) RunAtomicallyWithContext(ctx context.Context) error {
return mo.RunLoggedBatchWithContext(ctx)
}
func (mo mockMultiOp) GenerateStatement() Statement {
return noOpStatement{}
}
func (mo mockMultiOp) QueryExecutor() QueryExecutor {
return nil
}
func (mo mockMultiOp) Add(inOps ...Op) Op {
ops := make(mockMultiOp, 0, len(inOps))
for _, op := range inOps {
// If any multiOps were passed, flatten them out
switch op := op.(type) {
case mockMultiOp:
ops = append(ops, op...)
case mockOp:
ops = append(ops, op)
case multiOp:
if len(op) == 0 {
continue
}
panic("can't Add non-mock ops to mockMultiOp")
default:
panic("can't Add non-mock ops to mockMultiOp")
}
}
return append(mo, ops...)
}
func (mo mockMultiOp) Options() Options {
var opts Options
for _, op := range mo {
opts = opts.Merge(op.Options())
}
return opts
}
func (mo mockMultiOp) WithOptions(opts Options) Op {
result := make(mockMultiOp, len(mo))
for i, op := range mo {
result[i] = op.WithOptions(opts)
}
return result
}
func (mo mockMultiOp) Preflight() error {
for _, op := range mo {
if err := op.Preflight(); err != nil {
return err
}
}
return nil
}
func (ks *mockKeySpace) NewTable(name string, entity interface{}, fieldSource map[string]interface{}, keys Keys) Table {
mt := &MockTable{
RWMutex: &sync.RWMutex{},
ksName: ks.Name(),
tableName: name,
entity: entity,
keys: keys,
fieldSource: fieldSource,
rows: map[rowKey]*btree.BTree{},
mtx: &sync.RWMutex{},
}
fields := []string{}
for _, k := range sortedKeys(fieldSource) {
fields = append(fields, k)
}
mt.fields = fields
return mt
}
func NewMockKeySpace() KeySpace {
ks := &mockKeySpace{}
ks.tableFactory = ks
return ks
}
// MockTable implements the Table interface and stores rows in-memory.
type MockTable struct {
*sync.RWMutex
// rows is mapping from row key to column group key to column map
mtx *sync.RWMutex
ksName string
tableName string
rows map[rowKey]*btree.BTree
entity interface{}
fieldSource map[string]interface{}
fields []string
keys Keys
options Options
}
type rowKey string
type superColumn struct {
Key key
Columns map[string]interface{}
}
func (c *superColumn) Less(item btree.Item) bool {
other, ok := item.(*superColumn)
if !ok {
return false
}
return c.Key.Less(other.Key)
}
type gocqlTypeInfo struct {
proto byte
typ gocql.Type
}
func (t gocqlTypeInfo) New() interface{} {
return &gocqlTypeInfo{t.proto, t.typ}
}
func (t gocqlTypeInfo) Type() gocql.Type {
return t.typ
}
func (t gocqlTypeInfo) Version() byte {
return t.proto
}
func (t gocqlTypeInfo) Custom() string {
return ""
}
type keyPart struct {
Key string
Value interface{}
ClusteringOrder ColumnDirection
}
func (k *keyPart) Bytes() []byte {
typeInfo := &gocqlTypeInfo{
proto: 0x03,
typ: cassaType(k.Value),
}
marshalled, err := gocql.Marshal(typeInfo, k.Value)
if err != nil {
panic(err)
}
return marshalled
}
type key []keyPart
func (k key) Less(other key) bool {
for i := 0; i < len(k) && i < len(other); i++ {
cmp := bytes.Compare(k[i].Bytes(), other[i].Bytes())
if cmp == 0 {
continue
}
if k[i].ClusteringOrder { // desc
return cmp > 0
}
return cmp < 0
}
return false
}
func (k key) RowKey() rowKey {
buf := bytes.Buffer{}
for _, part := range k {
buf.Write(part.Bytes())
}
return rowKey(buf.String())
}
func (k key) ToSuperColumn() *superColumn {
return &superColumn{Key: k}
}
func (k key) Append(column string, value interface{}) key {
newKey := make([]keyPart, len(k)+1)
copy(newKey, k)
newKey[len(k)] = keyPart{Key: column, Value: value}
return newKey
}
func (t *MockTable) partitionKeyFromColumnValues(values map[string]interface{}, keyNames []string) (key, error) {
var key key
// For a single partition key of type string, check that it is not
// empty, this is same as this error from a real C* cluster-
// InvalidRequest: Error from server: code=2200 [Invalid query]
// message="Key may not be empty"
if len(keyNames) == 1 {
value, ok := values[keyNames[0]]
stringVal, isString := value.(string)
if !ok || (isString && stringVal == "") {
return nil, fmt.Errorf("Missing mandatory PRIMARY KEY part %s", keyNames[0])
}
key = key.Append(keyNames[0], value)
return key, nil
}
// Cassandra _does_ allow you to have a composite partition key in which
// all the components can be empty
for _, keyName := range keyNames {
value, ok := values[keyName]
if !ok {
return nil, fmt.Errorf("Missing mandatory PRIMARY KEY part %s", keyName)
}
key = key.Append(keyName, value)
}
return key, nil
}
func (t *MockTable) clusteringKeyFromColumnValues(values map[string]interface{}, keyNames []string) (key, error) {
var key key
for _, keyName := range keyNames {
value, ok := values[keyName]
if !ok {
return nil, fmt.Errorf("Missing mandatory PRIMARY KEY part %s", keyName)
}
key = key.Append(keyName, value)
}
return key, nil
}
func (t *MockTable) Name() string {
if len(t.options.TableName) > 0 {
return t.options.TableName
}
return t.tableName
}
func (t *MockTable) getOrCreateRow(rowKey key) *btree.BTree {
t.mtx.Lock()
defer t.mtx.Unlock()
row := t.rows[rowKey.RowKey()]
if row == nil {
row = btree.New(2)
t.rows[rowKey.RowKey()] = row
}
return row
}
func (t *MockTable) getOrCreateColumnGroup(rowKey, superColumnKey key) map[string]interface{} {
row := t.getOrCreateRow(rowKey)
scol := superColumnKey.ToSuperColumn()
// Retrieve the clustering order from the table options
// and assign to the key parts to set the sort ordering on each column
keyOrder := make(map[string]ColumnDirection, 0)
for _, v := range t.options.ClusteringOrder {
keyOrder[v.Column] = v.Direction
}
for i, kp := range scol.Key {
scol.Key[i].ClusteringOrder = keyOrder[kp.Key]
}
if row.Has(scol) {
return row.Get(scol).(*superColumn).Columns
}
row.ReplaceOrInsert(scol)
scol.Columns = map[string]interface{}{}
return scol.Columns
}
func (t *MockTable) SetWithOptions(i interface{}, options Options) Op {
return newOp(func(m mockOp) error {
t.Lock()
defer t.Unlock()
columns, ok := toMap(i)
if !ok {
return errors.New("Can't create: value not understood")
}
rowKey, err := t.partitionKeyFromColumnValues(columns, t.keys.PartitionKeys)
if err != nil {
return err
}
superColumnKey, err := t.clusteringKeyFromColumnValues(columns, t.keys.ClusteringColumns)
if err != nil {
return err
}
superColumn := t.getOrCreateColumnGroup(rowKey, superColumnKey)
if err := assignRecords(columns, superColumn); err != nil {
return err
}
return nil
})
}
func (t *MockTable) Set(i interface{}) Op {
return t.SetWithOptions(i, t.options)
}
func (t *MockTable) Where(relations ...Relation) Filter {
return &MockFilter{
table: t,
relations: relations,
}
}
func (t *MockTable) Create() error {
return nil
}
func (t *MockTable) CreateStatement() (Statement, error) {
return noOpStatement{}, nil
}
func (t *MockTable) CreateIfNotExist() error {
return nil
}
func (t *MockTable) CreateIfNotExistStatement() (Statement, error) {
return noOpStatement{}, nil
}
func (t *MockTable) Recreate() error {
return nil
}
func (t *MockTable) WithOptions(o Options) Table {
return &MockTable{
RWMutex: t.RWMutex,
ksName: t.ksName,
tableName: t.tableName,
rows: t.rows,
entity: t.entity,
keys: t.keys,
fieldSource: t.fieldSource,
fields: t.fields,
options: t.options.Merge(o),
mtx: t.mtx,
}
}
// MockFilter implements the Filter interface and works with MockTable.
type MockFilter struct {
table *MockTable
relations []Relation
}
func (f *MockFilter) Table() Table {
return f.table
}
func (f *MockFilter) Relations() []Relation {
return f.relations
}
func (f *MockFilter) rowMatch(row map[string]interface{}) bool {
for _, relation := range f.relations {
value := row[relation.Field()]
if !relation.accept(value) {
return false
}
}
return true
}
func (f *MockFilter) fieldRelationMap() map[string]Relation {
result := map[string]Relation{}
for _, relation := range f.relations {
result[relation.Field()] = relation
}
return result
}
func (f *MockFilter) fieldsFromRelations(fields []string) ([]key, error) {
fieldRelationMap := f.fieldRelationMap()
var rowKey key
var result []key
if len(fields) == 0 {
return []key{key{}}, nil
}
for i, keyName := range fields {
lastKey := i == len(fields)-1
relation, ok := fieldRelationMap[keyName]
if !ok {
return nil, fmt.Errorf("Missing mandatory PRIMARY KEY part `%s`", keyName)
}
if relation.Comparator() != CmpEquality && !(lastKey && relation.Comparator() == CmpIn) {
return nil, fmt.Errorf("Invalid use of PK `%s`", keyName)
}
if !lastKey {
rowKey = rowKey.Append(keyName, relation.Terms()[0])
} else {
for _, term := range relation.Terms() {
result = append(result, rowKey.Append(relation.Field(), term))
}
}
}
return result, nil
}
func (f *MockFilter) UpdateWithOptions(m map[string]interface{}, options Options) Op {
return newOp(func(mock mockOp) error {
f.table.Lock()
defer f.table.Unlock()
rowKeys, err := f.fieldsFromRelations(f.table.keys.PartitionKeys)
if err != nil {
return err
}
for _, rowKey := range rowKeys {
superColumnKeys, err := f.fieldsFromRelations(f.table.keys.ClusteringColumns)
if err != nil {
return err
}
for _, superColumnKey := range superColumnKeys {
superColumn := f.table.getOrCreateColumnGroup(rowKey, superColumnKey)
for _, key := range []key{rowKey, superColumnKey} {
for _, keyPart := range key {
superColumn[keyPart.Key] = keyPart.Value
}
}
if err := assignRecords(m, superColumn); err != nil {
return err
}
}
}
return nil
})
}
func (f *MockFilter) Update(m map[string]interface{}) Op {
return f.UpdateWithOptions(m, Options{})
}
func (f *MockFilter) Delete() Op {
return newOp(func(m mockOp) error {
f.table.Lock()
defer f.table.Unlock()
rowKeys, err := f.fieldsFromRelations(f.table.keys.PartitionKeys)
if err != nil {
return err
}
f.table.mtx.Lock()
defer f.table.mtx.Unlock()
for _, rowKey := range rowKeys {
row := f.table.rows[rowKey.RowKey()]
if row == nil {
return nil
}
row.Ascend(func(item btree.Item) bool {
columns := item.(*superColumn).Columns
if f.rowMatch(columns) {
row.Delete(item)
}
return true
})
}
return nil
})
}
func (q *MockFilter) Read(out interface{}) Op {
return newOp(func(m mockOp) error {
q.table.Lock()
defer q.table.Unlock()
var (
result []map[string]interface{}
err error
)
switch {
case len(q.Relations()) == 0:
result = q.readAllRows()
default:
result, err = q.readSomeRows()
}
if err != nil {
return err
}
opt := q.table.options.Merge(m.options)
if opt.Limit > 0 && opt.Limit < len(result) {
result = result[:opt.Limit]
}
fieldNames := opt.Select
if len(opt.Select) == 0 {
fieldNames = q.table.fields
}
stmt := SelectStatement{keyspace: q.table.ksName, table: q.table.Name(), fields: fieldNames}
iter := newMockIterator(result, stmt.fields)
_, err = NewScanner(stmt, out).ScanIter(iter)
return err
})
}
func (q *MockFilter) readSomeRows() ([]map[string]interface{}, error) {
q.table.mtx.RLock()
defer q.table.mtx.RUnlock()
rowKeys, err := q.fieldsFromRelations(q.table.keys.PartitionKeys)
if err != nil {
return nil, err
}
var result []map[string]interface{}
for _, rowKey := range rowKeys {
row := q.table.rows[rowKey.RowKey()]
if row == nil {
continue
}
row.Ascend(func(item btree.Item) bool {
columns := item.(*superColumn).Columns
if q.rowMatch(columns) {
result = append(result, columns)
}
return true
})
}
return result, nil
}
func (q *MockFilter) readAllRows() []map[string]interface{} {
q.table.mtx.RLock()
defer q.table.mtx.RUnlock()
var result []map[string]interface{}
for _, row := range q.table.rows {
row.Ascend(func(item btree.Item) bool {
columns := item.(*superColumn).Columns
if q.rowMatch(columns) {
result = append(result, columns)
}
return true
})
}
return result
}
func (q *MockFilter) ReadOne(out interface{}) Op {
return newOp(func(m mockOp) error {
return q.Read(out).Run()
})
}
// mockIterator takes in a slice of maps and implements a Scannable iterator
// which goes row by row within the slice.
type mockIterator struct {
results []map[string]interface{}
fields []string
currRowIndex int
closed bool
err error
}
func newMockIterator(results []map[string]interface{}, fields []string) *mockIterator {
return &mockIterator{
results: results,
fields: fields,
currRowIndex: -1,
closed: false,
}
}
// Next checks to see if there a result to be read
func (iter *mockIterator) Next() bool {
if iter.closed {
return false
}
// Check if reading the next row will get us out of bounds
if iter.currRowIndex+1 >= len(iter.results) {
return false
}
iter.currRowIndex++
return true
}
// Scan mocks a Scanner such as the one you get in gocql.Iter to assign results
func (iter *mockIterator) Scan(dest ...interface{}) error {
if iter.closed {
// We don't explictly assign this error to iter.err so we don't lose the
// original error in the iterator
return fmt.Errorf("called iterator after resources released")
}
if iter.currRowIndex < 0 {
return fmt.Errorf("called Scan without calling Next")
}
if len(dest) != len(iter.fields) {
iter.err = fmt.Errorf("got %d pointers for unmarshalling %d fields", len(dest), len(iter.fields))
return iter.err
}
result := iter.results[iter.currRowIndex]
for i, fieldName := range iter.fields {
if reflect.TypeOf(dest[i]).Kind() != reflect.Ptr {
iter.err = fmt.Errorf("expected pointer but got %T", dest[i])
return iter.err
}
value, ok := result[fieldName]
if !ok {
// See if any fields in result are equal (case insensitive)
for k, v := range result {
if strings.EqualFold(k, fieldName) {
value = v
ok = true
break
}
}
if !ok {
// We could panic here but ultimately this will be the zero value of
// the resulting pointer and is a valid use case so soldier on
continue
}
}
// If it's a field to ignore, then ignore it ;)
rv := reflect.ValueOf(dest[i])
if rv.Elem().Type() == reflect.TypeOf((*IgnoreFieldType)(nil)).Elem() {
continue
}
sv := reflect.ValueOf(value)
if !sv.IsValid() { // Ensure we're not working with the zero value
continue
}
// Maps are stored in the mock as map[<KeyType>]interface{}. The receiving value
// may be of a different map type so we need to accommodate for this.
if sv.Kind() == reflect.Map && sv.Type().Elem() != rv.Elem().Type().Elem() {
targetMap := reflect.MakeMap(rv.Elem().Type())
for _, key := range sv.MapKeys() {
// Need to do a type assertion here to set the underlying rv map value type
switch v := sv.MapIndex(key).Interface().(type) {
case int, int8, int16, int64:
targetMap.SetMapIndex(key, reflect.ValueOf(v))
case float32, float64, bool:
targetMap.SetMapIndex(key, reflect.ValueOf(v))
case byte, []byte, string, []string:
targetMap.SetMapIndex(key, reflect.ValueOf(v))
case interface{}, gocql.Unmarshaler:
targetMap.SetMapIndex(key, reflect.ValueOf(v))
default:
iter.err = fmt.Errorf("mock doesn't support map value type %T", v)
return iter.err
}
}
sv = targetMap
}
// We need to handle the case where we're given a pointer to a type which is
// not the exact type of the value but we can convert over by casting
if sv.Type() != rv.Elem().Type() {
if !sv.Type().ConvertibleTo(rv.Elem().Type()) {
iter.err = fmt.Errorf("could not unmarshal %T into %v", value, rv.Elem().Type())
return iter.err
}
sv = sv.Convert(rv.Elem().Type())
}
rv.Elem().Set(sv)
}
return nil
}
// Err returns the active error in the iterator. Once called, the 'resources'
// should be released and thus this iterator is closed
func (iter *mockIterator) Err() error {
iter.closed = true
return iter.err
}
// Reset resets the result list to the beginning of the slice. This should
// only really be needed for tests
func (iter *mockIterator) Reset() {
iter.closed = false
iter.err = nil
iter.currRowIndex = -1
}
func assignRecords(m map[string]interface{}, record map[string]interface{}) error {
for k, v := range m {
switch v := v.(type) {
case Modifier:
switch v.op {
case ModifierMapSetField:
// Go interfaces are internally represented as a type and a value. The record[k] interface{} value could look like one of these:
// [type, value]
// [type, nil ]
// [nil, nil ]
var targetMap reflect.Value
if record[k] != nil {
// narrowed it down to:
// [type, value]
// [type, nil ]
rv := reflect.ValueOf(record[k])
if rv.Type().Kind() != reflect.Map {
return fmt.Errorf("Can't use MapSetField modifier on field that isn't a map: %T", record[k])
}
if !rv.IsNil() {
// [type, value]
targetMap = rv
}
}
// This modifier's args is a []interface{} with a key at index 0 and a value at index 1
if len(v.args) != 2 {
return fmt.Errorf("Argument for MapSetField is not a slice of 2 elements")
}
key := reflect.ValueOf(v.args[0])
value := reflect.ValueOf(v.args[1])
// If we couldn't initialize the map from the content of record[k], we create it from the values of v.args
if targetMap.Kind() != reflect.Map {
targetMapType := reflect.MapOf(key.Type(), value.Type())
targetMap = reflect.MakeMap(targetMapType)
}
targetMap.SetMapIndex(key, value)
record[k] = targetMap.Interface()
case ModifierMapSetFields:
// Go interfaces are internally represented as a type and a value. The record[k] interface{} value could look like one of these:
// [type, value]
// [type, nil ]
// [nil, nil ]
var targetMap reflect.Value
if record[k] != nil {
// narrowed it down to:
// [type, value]
// [type, nil ]
rv := reflect.ValueOf(record[k])
if rv.Type().Kind() != reflect.Map {
return fmt.Errorf("Can't use MapSetFields modifier on field that isn't a map: %T", record[k])
}
if rv.IsNil() {
// [type, nil ]
targetMap = reflect.MakeMap(rv.Type())
} else {
// [type, value]
targetMap = rv
}
} else {
// [nil, nil ]
// We don't know the type, so we guess. Note that this guess is
// likely wrong but to fix that we need a much larger refactor.
targetMap = reflect.ValueOf(map[string]interface{}{})
}
ma, ok := v.args[0].(map[string]interface{})
if !ok {
return fmt.Errorf("Argument for MapSetFields is not a map")
}
for k, v := range ma {
targetMap.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
}
record[k] = targetMap.Interface()
case ModifierCounterIncrement:
oldV, _ := record[k].(int64)
delta := int64(v.args[0].(int))
record[k] = oldV + delta
default:
return fmt.Errorf("Modifer %v not supported by mock keyspace", v.op)
}
default:
record[k] = v
}
}
return nil
}
type mockContextKey string
var errorInjectorContextKey mockContextKey = "error_injector_context_key"
type ErrorInjector interface {
shouldReturnErr(op Op, opIdx int, opCount int) error
}
func getErrorInjector(ctx context.Context) ErrorInjector {
if ctx != nil {
if strategy := extractErrorInjectorFromContext(ctx); strategy != nil {
return strategy
}
}
return &neverFail{}
}
func extractErrorInjectorFromContext(ctx context.Context) ErrorInjector {
if v := ctx.Value(errorInjectorContextKey); v != nil {
if strategy, ok := v.(ErrorInjector); ok {
return strategy
}
}
return nil
}
// ErrorInjectorContext returns a context which when passed to
// mockMultiOp.RunWithContext(...), will inject an error before one of the
// operations to simulate a partial failure in the query execution.
// The ErrorInjector determines when in the sequence the error is injected
func ErrorInjectorContext(parent context.Context, strategy ErrorInjector) context.Context {
return context.WithValue(parent, errorInjectorContextKey, strategy)
}
type neverFail struct{}
func (n *neverFail) shouldReturnErr(Op, int, int) error { return nil }
// FailOnNthOperation returns an ErrorInjector which injects the provided err on
// the nth operation of a mockMultiOp. n is 0 indexed so an n value of 0 will
// fail on the first operation. If there are fewer than n-1 operations in the
// mockMultiOp, no error will be injected
func FailOnNthOperation(n int, err error) ErrorInjector {
return &failOnNthOperation{n: n, err: err}
}
type failOnNthOperation struct {
n int
err error
}
func (f *failOnNthOperation) shouldReturnErr(op Op, opIdx, opCount int) error {
if opIdx == f.n {
return f.err
}
return nil
}
// FailOnEachOperation returns an ErrorInjector which fails on each operation of
// a mockMultiOp in turn
func FailOnEachOperation(err error) *FailOnEachOperationErrorInjector {
return &FailOnEachOperationErrorInjector{
err: err,
finalOpSucceeded: false,
lastErrorInjectedAtIdx: -1,