-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
870 lines (807 loc) · 19.6 KB
/
state.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
// Copyright 2022 Chance Dinkins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// The License can be found in the LICENSE file.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jsonpointer
import (
"encoding"
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
"strconv"
"sync"
)
var (
// valinfoPool sync.Pool
statePool sync.Pool
typeAssigner = reflect.TypeOf((*Assigner)(nil)).Elem()
typeByteSlice = reflect.TypeOf([]byte{})
typeReader = reflect.TypeOf((*io.Reader)(nil)).Elem()
typeWriter = reflect.TypeOf((*io.Writer)(nil)).Elem()
typeTextUnmarshaler = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
typeAny = reflect.TypeOf((*interface{})(nil)).Elem()
typeAnySlice = reflect.TypeOf([]interface{}{})
typeAnyMap = reflect.TypeOf(map[string]interface{}{})
)
func newState(ptr Pointer, op Operation) *state {
var s *state
if v := statePool.Get(); v != nil {
s = v.(*state)
} else {
s = &state{}
}
s.ptr = ptr
s.current = ptr
s.op = op
return s
}
type state struct {
op Operation
ptr Pointer
current Pointer
}
func (s *state) Release() {
statePool.Put(s)
}
func (s state) Operation() Operation {
return s.op
}
func (s *state) resolve(v reflect.Value) (reflect.Value, error) {
var t Token
var err error
var ok bool
for {
cur := s.current
if cur.IsRoot() {
return v, nil
}
typ := v.Type()
if s.current, t, ok = cur.Next(); !ok {
return reflect.Value{}, fmt.Errorf("unexpected end of JSON pointer %v", s.current)
}
if err != nil {
return v, newError(err, *s, typ)
}
v, err = s.resolveNext(v, t)
if err == nil {
switch v.Kind() {
case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
if v.IsNil() && !cur.IsRoot() {
return v, newError(ErrUnreachable, *s, typ)
}
}
}
if err == nil && v.Kind() == reflect.Invalid {
err = newError(ErrNotFound, *s, typ)
}
if err != nil {
s.current = cur.Prepend(t)
updateErrorState(err, *s)
return v, err
}
}
}
func (s *state) assign(dst reflect.Value, val reflect.Value) (reflect.Value, error) {
var t Token
var err error
var ok bool
cur := s.current
if cur.IsRoot() {
_, err := s.assignValue(dst, val)
return dst, err
}
s.current, t, ok = cur.Next()
if !ok {
return reflect.Value{}, fmt.Errorf("unexpected end of JSON pointer %v", cur)
}
var cpy reflect.Value
if isByteSlice(dst.Elem()) {
cpy = dst
if dst.Elem().Len() > 0 {
dst, err = s.unmarshal(dst.Elem())
if err != nil {
return reflect.Value{}, err
}
} else {
_, nt, ok := s.current.Next()
if !ok {
return reflect.Value{}, newError(ErrMalformedToken, *s, dst.Type())
}
if s.current.IsRoot() {
dst = reflect.Zero(val.Type())
} else {
if _, err = nt.Index(0); err == nil {
dst = reflect.MakeSlice(typeAnySlice, 0, 1)
} else {
dst = reflect.MakeMap(typeAnyMap)
}
}
dp := reflect.New(dst.Type())
dp.Elem().Set(dst)
dst = dp
}
}
// new dst
var rn reflect.Value
rn, err = s.resolveNext(dst, t)
if err != nil {
return rn, err
}
switch rn.Kind() {
case reflect.Interface:
if !rn.IsNil() && rn.Type() == typeAny {
rn = rn.Elem()
}
case reflect.Ptr:
if rn.IsNil() {
rn.Set(reflect.New(rn.Type().Elem()))
}
case reflect.Map:
if rn.IsNil() {
rn.Set(reflect.MakeMap(rn.Type()))
}
case reflect.Slice:
if rn.IsNil() {
rn.Set(reflect.MakeSlice(rn.Type(), 0, 1))
}
case reflect.Invalid:
switch dst.Type().Elem().Kind() {
case reflect.Map, reflect.Slice:
rn = reflect.Zero(dst.Type().Elem().Elem())
if rn.Kind() == reflect.Ptr && rn.IsNil() {
rn = reflect.New(rn.Type().Elem())
} else if rn.Type() == typeAny && rn.IsNil() {
nt, ok := s.current.NextToken()
if !ok {
rn = reflect.Zero(val.Type())
} else {
if _, err = nt.Index(0); err == nil {
rn = reflect.MakeSlice(typeAnySlice, 0, 1)
} else {
rn = reflect.MakeMap(typeAnyMap)
}
}
}
case reflect.Interface:
_, nt, ok := s.current.Next()
if !ok {
return reflect.Value{}, newError(ErrMalformedToken, *s, dst.Type())
}
if rn.Type() == typeAny {
if s.current.IsRoot() {
rn = reflect.Zero(val.Type())
} else {
if _, err = nt.Index(0); err == nil {
rn = reflect.MakeSlice(typeAnySlice, 0, 1)
} else {
rn = reflect.MakeMap(typeAnyMap)
}
}
}
default:
return reflect.Value{}, newError(ErrUnreachable, *s, dst.Type())
}
}
if rn.CanAddr() {
rn = rn.Addr()
} else {
pv := reflect.New(rn.Type())
pv.Elem().Set(rn)
rn = pv
}
var nv reflect.Value
nv, err = s.assign(rn, val)
if err != nil {
return dst, err
}
s.current = s.current.Prepend(t)
if cpy.IsValid() {
if assigner, ok := asAssigner(cpy); ok {
err = assigner.AssignByJSONPointer(&cur, nv.Elem().Interface())
if err != nil {
if !errors.Is(err, YieldOperation) {
return dst, newError(err, *s, dst.Elem().Type())
} else {
cur = s.current
}
} else {
return dst, nil
}
// updating state to reflect the new token if it was set by assigner
s.current = cur
}
}
if assigner, ok := asAssigner(dst); ok {
err = assigner.AssignByJSONPointer(&cur, nv.Elem().Interface())
if err != nil {
if !errors.Is(err, YieldOperation) {
return dst, newError(err, *s, dst.Elem().Type())
} else {
cur = s.current
}
} else {
return dst, nil
}
// updating state to reflect the new token if it was set by assigner
s.current = cur
}
rn, err = s.assignValue(rn, nv.Elem())
if err != nil {
return rn, err
}
switch dst.Elem().Kind() {
case reflect.Map:
err = s.setMapIndex(dst.Elem(), t, rn.Elem())
case reflect.Slice:
err = s.setSliceIndex(dst, t, rn.Elem())
}
if err != nil {
return reflect.Value{}, newError(err, *s, dst.Elem().Type())
}
if cpy.IsValid() {
rn, err = s.marshal(dst)
if err != nil {
return reflect.Value{}, err
}
cpy.Elem().SetBytes(rn.Elem().Bytes())
return cpy, nil
}
return dst, nil
}
func (s *state) delete(dst reflect.Value) (reflect.Value, error) {
var t Token
var err error
var ok bool
cur := s.current
if cur.IsRoot() {
err := s.deleteValue(dst)
return dst, err
}
s.current, t, ok = cur.Next()
if !ok {
return reflect.Value{}, fmt.Errorf("unexpected end of JSON pointer %v", cur)
}
var cpy reflect.Value
if isByteSlice(dst.Elem()) {
cpy = dst
if dst.Elem().Len() > 0 {
dst, err = s.unmarshal(dst.Elem())
if err != nil {
return reflect.Value{}, err
}
} else {
_, nt, ok := s.current.Next()
if !ok {
return reflect.Value{}, newError(ErrMalformedToken, *s, dst.Type())
}
if s.current.IsRoot() {
dst.SetBytes([]byte{})
} else {
if _, err = nt.Index(0); err == nil {
dst = reflect.MakeSlice(typeAnySlice, 0, 1)
} else {
dst = reflect.MakeMap(typeAnyMap)
}
}
dp := reflect.New(dst.Type())
dp.Elem().Set(dst)
dst = dp
}
}
if cpy.IsValid() {
if deleter, ok := asDeleter(cpy); ok {
err = deleter.DeleteByJSONPointer(&cur)
if err != nil {
if !errors.Is(err, YieldOperation) {
return dst, newError(err, *s, dst.Elem().Type())
} else {
cur = s.current
}
} else {
return dst, nil
}
s.current = cur
}
}
if deleter, ok := asDeleter(dst); ok {
err = deleter.DeleteByJSONPointer(&cur)
if err != nil {
if !errors.Is(err, YieldOperation) {
return dst, newError(err, *s, dst.Elem().Type())
} else {
cur = s.current
}
} else {
return dst, nil
}
// updating state to reflect the new token if it was set by deleter
s.current = cur
}
// new dst
var rn reflect.Value
rn, err = s.resolveNext(dst, t)
// cur = s.current
if err != nil {
return rn, err
}
if !ok {
return dst, newError(ErrMalformedToken, *s, dst.Type())
}
if rn.IsValid() && rn.CanInterface() && rn.Type() == typeAny && !rn.IsNil() {
rn = rn.Elem()
}
switch rn.Kind() {
case reflect.Interface:
if !rn.IsNil() && rn.Type() == typeAny {
rn = rn.Elem()
}
case reflect.Ptr:
if rn.IsNil() {
s.current = ""
} else if s.current.IsRoot() {
s.current = ""
rn.Set(reflect.ValueOf(nil))
}
case reflect.Map:
switch {
case rn.IsNil():
s.current = ""
case s.current.IsRoot():
s.current = ""
err = s.deleteMapIndex(rn, t)
if err != nil {
return reflect.Value{}, err
}
}
case reflect.Slice:
switch {
case rn.IsNil():
s.current = ""
case s.current.IsRoot():
err = s.deleteSliceIndex(rn, t)
if err != nil {
return reflect.Value{}, err
}
s.current = ""
}
case reflect.Invalid:
s.current = ""
return dst, nil
}
if rn.CanAddr() {
rn = rn.Addr()
} else {
pv := reflect.New(rn.Type())
pv.Elem().Set(rn)
rn = pv
}
var nv reflect.Value
nv, err = s.delete(rn)
if err != nil {
return dst, err
}
cur = s.current
s.current = s.current.Prepend(t)
rn, err = s.assignValue(rn, nv.Elem())
if err != nil {
return rn, err
}
switch dst.Elem().Kind() {
case reflect.Map:
if cur.IsRoot() {
err = s.deleteMapIndex(dst.Elem(), t)
} else {
err = s.setMapIndex(dst.Elem(), t, rn.Elem())
}
case reflect.Slice:
if cur.IsRoot() {
err = s.deleteSliceIndex(dst, t)
} else {
err = s.setSliceIndex(dst, t, rn.Elem())
}
}
if err != nil {
return reflect.Value{}, newError(err, *s, dst.Elem().Type())
}
if cpy.IsValid() {
rn, err = s.marshal(dst)
if err != nil {
return reflect.Value{}, err
}
cpy.Elem().SetBytes(rn.Elem().Bytes())
return cpy, nil
}
return dst, nil
}
func (s *state) setValue(dst reflect.Value, v reflect.Value) error {
switch dst.Kind() {
case reflect.Interface:
if !dst.CanInterface() {
return newError(ErrNotAssignable, *s, dst.Type())
}
return s.setValue(dst.Elem(), v)
case reflect.Ptr:
if v.Type().AssignableTo(dst.Type().Elem()) {
dst.Elem().Set(v)
return nil
}
// none of the above is true
return newValueError(ErrNotAssignable, *s, dst.Type(), v.Type())
default:
// this should never be reached
panic("can not assign to non-pointer")
}
}
func (s state) marshal(v reflect.Value) (reflect.Value, error) {
b, err := json.Marshal(v.Interface())
if err != nil {
return reflect.Value{}, newError(err, s, v.Type())
}
bv := reflect.New(typeByteSlice)
bv.Elem().SetBytes(b)
return bv, nil
}
func (s state) unmarshal(v reflect.Value) (reflect.Value, error) {
var i interface{}
if len(v.Bytes()) == 0 {
return reflect.Value{}, nil
}
err := json.Unmarshal(v.Bytes(), &i)
if err != nil {
return v, newError(err, s, reflect.TypeOf(v))
}
iv := reflect.ValueOf(i)
ptr := reflect.New(iv.Type())
ptr.Elem().Set(iv)
return ptr, nil
}
func (s *state) resolveNext(v reflect.Value, t Token) (reflect.Value, error) {
var err error
switch {
case v.Type().NumMethod() > 0 && v.CanInterface():
if resolver, ok := v.Interface().(Resolver); ok {
rv, err := s.resolveResolver(resolver, v, t)
if err != nil {
if !errors.Is(err, YieldOperation) {
return rv, err
}
} else {
return rv, nil
}
}
case isByteSlice(v):
v, err = s.unmarshal(v)
if err != nil {
return v, err
}
case v.Kind() == reflect.Ptr && isByteSlice(v.Elem()):
v, err = s.unmarshal(v.Elem())
if err != nil {
return v, err
}
}
switch v.Kind() {
case reflect.Interface, reflect.Ptr:
if v.IsNil() {
return v, nil
}
return s.resolveNext(v.Elem(), t)
case reflect.Map:
return s.resolveMapIndex(v, t)
case reflect.Array:
return s.resolveArrayIndex(v, t)
case reflect.Slice:
return s.resolveSlice(v, t)
case reflect.Struct:
return s.resolveStructField(v, t)
default:
return v, newError(ErrUnreachable, *s, v.Type())
}
}
func (s *state) resolveResolver(r Resolver, rv reflect.Value, t Token) (reflect.Value, error) {
// storing the current pointer in the event the Resolver mutates it and
// there is an error
prev := s.current
cur := prev.Prepend(t)
s.current = cur
res, err := r.ResolveJSONPointer(&s.current, s.op)
if err != nil {
// if the Resolver returns an error, it can either be a YieldOperation
// which continues the flow or an actual error.
if !errors.Is(err, YieldOperation) {
return rv, newError(err, *s, reflect.TypeOf(rv))
}
s.current = prev
return rv, YieldOperation
}
if s.current == cur {
// if the Resolver didn't mutate the pointer, then we
// restore it to the original value
s.current = prev
}
return reflect.ValueOf(res), nil
}
func (s state) resolveMapIndex(v reflect.Value, t Token) (reflect.Value, error) {
kv, err := s.mapKey(v, t)
if err != nil {
return kv, err
}
return v.MapIndex(kv), nil
}
func (s state) resolveArrayIndex(v reflect.Value, t Token) (reflect.Value, error) {
i, err := s.arrayIndex(v, t)
if err != nil {
return reflect.Value{}, err
}
return v.Index(i), nil
}
func (s state) resolveStructField(v reflect.Value, t Token) (reflect.Value, error) {
var fields structFields
if v.Type().Kind() == reflect.Ptr {
fields = cachedTypeFields(v.Type().Elem())
} else {
fields = cachedTypeFields(v.Type())
}
var f *field
if i, ok := fields.nameIndex[t.String()]; ok {
f = &fields.list[i]
} else {
for i := range fields.list {
tf := &fields.list[i]
if tf.equalFold(tf.nameBytes, t.Bytes()) {
f = tf
break
}
}
}
if f == nil {
fv, ok := v.Type().FieldByName(t.String())
if ok && !fv.IsExported() {
return reflect.Value{}, newError(ErrUnexportedField, s, v.Type())
}
return reflect.Value{}, newError(ErrNotFound, s, v.Type())
}
return v.FieldByIndex(f.index), nil
}
func (s state) resolveSlice(v reflect.Value, t Token) (reflect.Value, error) {
i, err := s.sliceIndex(v, t)
if err != nil {
if errors.Is(err, strconv.ErrSyntax) {
return reflect.Value{}, newError(ErrMalformedIndex, s, v.Type())
}
return reflect.Value{}, newError(err, s, v.Type())
}
if s.op == Resolving && i >= v.Len() {
return reflect.Value{}, newError(&indexError{
err: ErrOutOfRange,
maxIndex: v.Len() - 1,
index: i,
}, s, v.Type())
} else if i >= v.Len() {
return reflect.Value{}, nil
}
return v.Index(i), nil
}
func (s state) deleteMapIndex(v reflect.Value, t Token) error {
return s.setMapIndex(v, t, reflect.Value{})
}
func (s state) JSONPointer() Pointer {
return s.ptr
}
// CurrentJSONPointer returns the JSONPointer at the time of the error.
func (s state) CurrentJSONPointer() Pointer {
return s.current
}
func (s *state) assignValue(dst reflect.Value, val reflect.Value) (reflect.Value, error) {
if !dst.Elem().CanSet() {
return dst, newValueError(ErrNotAssignable, *s, dst.Type(), val.Type())
}
switch {
case val.Type().AssignableTo(dst.Elem().Type()):
dst.Elem().Set(val)
return dst, nil
case isByteSlice(val.Elem()):
err := json.Unmarshal(val.Elem().Bytes(), dst.Interface())
if err != nil {
return dst, newValueError(ErrNotAssignable, *s, dst.Type(), val.Type())
}
return dst, nil
case isByteSlice(dst.Elem()):
b, err := json.Marshal(val.Interface())
if err != nil {
return dst, newValueError(ErrNotAssignable, *s, dst.Type(), val.Type())
}
dst.Elem().SetBytes(b)
}
return val, newValueError(ErrNotAssignable, *s, dst.Elem().Type(), val.Type())
}
func (s *state) deleteValue(dst reflect.Value) error {
if !dst.Elem().CanSet() {
return newError(ErrNotAssignable, *s, dst.Type())
}
z := reflect.Zero(dst.Elem().Type())
dst.Elem().Set(z)
return nil
}
func (s state) sliceIndex(src reflect.Value, t Token) (int, error) {
i, err := t.Index(src.Len())
return i, err
}
func (s state) arrayIndex(src reflect.Value, t Token) (int, error) {
// if t == "-" then we attempt to get the last non-zero index
z := -1
if t == "-" {
for i := src.Len() - 1; i >= 0; i-- {
if !src.Index(i).IsZero() {
break
}
z = i
}
if z < 0 {
return -1, newError(&indexError{
err: ErrOutOfRange,
maxIndex: src.Len() - 1,
index: -1,
}, s, src.Type())
}
return z, nil
}
z, err := t.Index(src.Type().Len() - 1)
if err != nil {
return z, newError(err, s, src.Type())
}
return z, nil
}
func (s *state) mapKey(src reflect.Value, t Token) (reflect.Value, error) {
kt := src.Type().Key()
var kv reflect.Value
// checks to see if the map's key implements encoding.TextUnmarshaler
// if so, we use that to unmarshal the key
if reflect.PtrTo(kt).Implements(typeTextUnmarshaler) {
kv = reflect.New(kt)
if err := kv.Interface().(encoding.TextUnmarshaler).UnmarshalText(t.Bytes()); err != nil {
return kv, &keyError{
ptrError: ptrError{
err: err,
typ: src.Type(),
state: *s,
},
keyType: kt,
keyValue: kv,
}
}
kv = kv.Elem()
// otherwise the map's key must be either a string or an integer kind
} else {
switch kt.Kind() {
case reflect.String:
kv = reflect.ValueOf(t.String())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i, err := t.Int64()
if err != nil {
return kv, &keyError{
ptrError: ptrError{
err: err,
typ: src.Type(),
state: *s,
},
keyType: kt,
keyValue: kv,
}
}
kv = reflect.ValueOf(i).Convert(src.Type().Key())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
u, err := t.Uint64()
if err != nil {
return kv, &keyError{
ptrError: ptrError{
err: err,
typ: src.Type(),
state: *s,
},
keyType: kt,
keyValue: kv,
}
}
kv = reflect.ValueOf(u).Convert(src.Type().Key())
}
}
if !kv.Type().AssignableTo(src.Type().Key()) {
return kv, &keyError{
ptrError: ptrError{
err: ErrInvalidKeyType,
typ: src.Type(),
state: *s,
},
keyType: kt,
keyValue: kv,
}
}
return kv, nil
}
func (s *state) deleteSliceIndex(l reflect.Value, token Token) error {
e := l.Elem()
i, err := s.sliceIndex(e, token)
if err != nil {
return err
}
if i >= l.Elem().Len() {
return nil
}
reflect.Copy(e.Slice(i, e.Len()), e.Slice(i+1, e.Len()))
e.Index(e.Len() - 1).Set(reflect.Zero(e.Type()))
e.SetLen(e.Len() - 1)
l.Elem().Set(e)
return nil
}
func (s *state) setSliceIndex(l reflect.Value, token Token, v reflect.Value) error {
i, err := s.sliceIndex(l.Elem(), token)
if err != nil {
return err
}
if i >= l.Elem().Len() {
l.Elem().Set(reflect.Append(l.Elem(), v))
return nil
} else {
nl := l.Elem()
nl.Index(i).Set(v)
l.Set(nl)
}
return nil
}
func (s *state) setArrayIndex(src reflect.Value, token Token, v reflect.Value) error {
i, err := s.arrayIndex(src, token)
if err != nil {
return err
}
src.Index(i).Set(v)
return nil
}
func (s *state) setMapIndex(m reflect.Value, token Token, v reflect.Value) error {
kv, err := s.mapKey(m, token)
if err != nil {
return err
}
m.SetMapIndex(kv, v)
return nil
}
func isByteSlice(v reflect.Value) bool {
if v.IsValid() && v.Kind() == reflect.Interface && v.Elem().IsValid() && v.Elem().Type().AssignableTo(typeByteSlice) {
return true
}
return v.Type().AssignableTo(typeByteSlice)
}
func asAssigner(v reflect.Value) (Assigner, bool) {
if v.Type().NumMethod() > 0 && v.CanInterface() {
as, ok := v.Interface().(Assigner)
if ok {
return as, true
}
}
if v.Kind() == reflect.Ptr {
return asAssigner(v.Elem())
}
return nil, false
}
func asDeleter(v reflect.Value) (Deleter, bool) {
if v.Type().NumMethod() > 0 && v.CanInterface() {
del, ok := v.Interface().(Deleter)
if ok {
return del, true
}
}
if v.Kind() == reflect.Ptr {
return asDeleter(v.Elem())
}
return nil, false
}