-
Notifications
You must be signed in to change notification settings - Fork 1
/
array_access.go
175 lines (155 loc) · 4.47 KB
/
array_access.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
package protopath
import (
"context"
"fmt"
"reflect"
)
// ArrayAccessMethod responsible to execute any method to access array field
type ArrayAccessMethod interface {
Get(ctx context.Context, obj any) (any, error)
}
// ArrayAccessOperation is interface to access array fields
type ArrayAccessOperation struct {
method ArrayAccessMethod
}
// Lookup find the desired value based on defined operation
func (a *ArrayAccessOperation) Lookup(ctx context.Context, obj, _ any) (any, error) {
return a.method.Get(ctx, obj)
}
// getByIndices, array access method by specified indices
// e.g $.arr[1] or $.arr[1,2,5]
type getByIndices struct {
indices []int
}
// Get find the desired value based on the defined method
func (gbi *getByIndices) Get(ctx context.Context, val any) (any, error) {
reflectVal := reflect.ValueOf(val)
if reflectVal.Kind() != reflect.Slice {
return nil, fmt.Errorf("only work for slice")
}
result := make([]any, 0)
lenOfArray := reflectVal.Len()
isNestedArray := reflect.TypeOf(reflectVal.Index(0).Interface()).Kind() == reflect.Slice
if len(gbi.indices) == 1 && !isNestedArray {
idx := gbi.indices[0]
if idx >= lenOfArray {
return nil, fmt.Errorf("index out of bound")
}
return reflectVal.Index(idx).Interface(), nil
}
if isNestedArray {
for i := 0; i < lenOfArray; i++ {
currVal := reflectVal.Index(i).Interface()
currValReflection := reflect.ValueOf(currVal)
for _, idxVal := range gbi.indices {
if idxVal >= currValReflection.Len() || idxVal < 0 {
return nil, fmt.Errorf("index out of bound")
}
result = append(result, currValReflection.Index(idxVal).Interface())
}
}
return result, nil
}
for _, idxVal := range gbi.indices {
if idxVal >= lenOfArray || idxVal < 0 {
return nil, fmt.Errorf("index out of bound")
}
result = append(result, reflectVal.Index(idxVal).Interface())
}
return result, nil
}
// getByRange, array access method by specify the range
// e.g
// 1. $.arr[1:5]
// 2. $.arr[1:]
// 3. $.arr[:3]
// 4. $.arr[:]
type getByRange struct {
startIdx *int
endIdx *int
}
// Get find the desired value based on the defined method
func (i *getByRange) Get(ctx context.Context, val any) (any, error) {
reflectVal := reflect.ValueOf(val)
if reflectVal.Kind() != reflect.Slice {
return nil, fmt.Errorf("only work for slice")
}
lenOfArray := reflectVal.Len()
startIdx := 0
if i.startIdx != nil {
startIdx = *i.startIdx
}
if startIdx < 0 {
startIdx = startIdx + lenOfArray
}
endIdx := lenOfArray
if i.endIdx != nil {
endIdx = *i.endIdx
}
if endIdx < 0 {
endIdx = endIdx + lenOfArray
}
if startIdx > endIdx {
return nil, fmt.Errorf("end index should be greater than start index")
}
if endIdx > lenOfArray {
return nil, fmt.Errorf("end index should be less or equal length of rows")
}
result := make([]any, 0)
if lenOfArray == 0 {
return result, nil
}
isNestedArray := reflect.TypeOf(reflectVal.Index(0).Interface()).Kind() == reflect.Slice
for i := 0; i < lenOfArray; i++ {
currVal := reflectVal.Index(i).Interface()
if isNestedArray {
currValReflection := reflect.ValueOf(currVal)
for j := 0; j < currValReflection.Len(); j++ {
if j < startIdx || j >= endIdx {
continue
}
result = append(result, currValReflection.Index(j).Interface())
}
continue
}
if i < startIdx || i >= endIdx {
continue
}
result = append(result, currVal)
}
return result, nil
}
// getByBackwardIndex, array access method to find the object in the specific index counted from the end of index
// e.g $.arr[@.length-4]
type getByBackwardIndex struct {
index int
}
// Get find the desired value based on the defined method
func (i *getByBackwardIndex) Get(ctx context.Context, val any) (any, error) {
reflectVal := reflect.ValueOf(val)
if reflectVal.Kind() != reflect.Slice {
return nil, fmt.Errorf("only work for slice")
}
lenOfArray := reflectVal.Len()
idx := lenOfArray - i.index
if idx < 0 {
return nil, fmt.Errorf("index should be greater equal 0")
}
if lenOfArray == 0 {
return nil, nil
}
isNestedArray := reflect.TypeOf(reflectVal.Index(0).Interface()).Kind() == reflect.Slice
if !isNestedArray {
return reflectVal.Index(idx).Interface(), nil
}
result := make([]any, 0)
for i := 0; i < lenOfArray; i++ {
currVal := reflectVal.Index(i).Interface()
currValReflection := reflect.ValueOf(currVal)
if idx < currValReflection.Len() {
return nil, fmt.Errorf("index out of bound")
}
result = append(result, currValReflection.Index(idx).Interface())
}
return result, nil
}