MCPcopy Create free account
hub / github.com/google/gapid / ArrayIndex

Function ArrayIndex

gapis/resolve/resolve.go:153–183  ·  view source on GitHub ↗

ArrayIndex resolves and returns the array or slice element from the path p.

(ctx context.Context, p *path.ArrayIndex, r *path.ResolveConfig)

Source from the content-addressed store, hash-verified

151
152// ArrayIndex resolves and returns the array or slice element from the path p.
153func ArrayIndex(ctx context.Context, p *path.ArrayIndex, r *path.ResolveConfig) (interface{}, error) {
154 obj, err := ResolveInternal(ctx, p.Parent(), r)
155 if err != nil {
156 return nil, err
157 }
158
159 a := reflect.ValueOf(obj)
160 switch {
161 case box.IsMemorySlice(a.Type()):
162 slice := box.AsMemorySlice(a)
163 if count := slice.Count(); p.Index >= count {
164 return nil, errPathOOB(p.Index, "Index", 0, count-1, p)
165 }
166 return slice.ISlice(p.Index, p.Index+1), nil
167
168 default:
169 switch a.Kind() {
170 case reflect.Array, reflect.Slice, reflect.String:
171 if count := uint64(a.Len()); p.Index >= count {
172 return nil, errPathOOB(p.Index, "Index", 0, count-1, p)
173 }
174 return a.Index(int(p.Index)).Interface(), nil
175
176 default:
177 return nil, &service.ErrInvalidPath{
178 Reason: messages.ErrTypeNotArrayIndexable(typename(a.Type())),
179 Path: p.Path(),
180 }
181 }
182 }
183}
184
185// Slice resolves and returns the subslice from the path p.
186func Slice(ctx context.Context, p *path.Slice, r *path.ResolveConfig) (interface{}, error) {

Callers 1

ResolveInternalFunction · 0.70

Calls 12

ResolveInternalFunction · 0.85
errPathOOBFunction · 0.85
InterfaceMethod · 0.80
typenameFunction · 0.70
ParentMethod · 0.65
TypeMethod · 0.65
CountMethod · 0.65
ISliceMethod · 0.65
KindMethod · 0.65
LenMethod · 0.65
IndexMethod · 0.65
PathMethod · 0.65

Tested by

no test coverage detected