ArrayIndex resolves and returns the array or slice element from the path p.
(ctx context.Context, p *path.ArrayIndex, r *path.ResolveConfig)
| 151 | |
| 152 | // ArrayIndex resolves and returns the array or slice element from the path p. |
| 153 | func 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. |
| 186 | func Slice(ctx context.Context, p *path.Slice, r *path.ResolveConfig) (interface{}, error) { |
no test coverage detected