| 308 | |
| 309 | } |
| 310 | func (t *parser) listItem(list []interface{}, i int) ([]interface{}, error) { |
| 311 | if i < 0 { |
| 312 | return list, fmt.Errorf("negative %d index not allowed", i) |
| 313 | } |
| 314 | stop := runeSet([]rune{'[', '.', '='}) |
| 315 | switch k, last, err := runesUntil(t.sc, stop); { |
| 316 | case len(k) > 0: |
| 317 | return list, errors.Errorf("unexpected data at end of array index: %q", k) |
| 318 | case err != nil: |
| 319 | return list, err |
| 320 | case last == '=': |
| 321 | vl, e := t.valList() |
| 322 | switch e { |
| 323 | case nil: |
| 324 | return setIndex(list, i, vl) |
| 325 | case io.EOF: |
| 326 | return setIndex(list, i, "") |
| 327 | case ErrNotList: |
| 328 | rs, e := t.val() |
| 329 | if e != nil && e != io.EOF { |
| 330 | return list, e |
| 331 | } |
| 332 | v, e := t.reader(rs) |
| 333 | if e != nil { |
| 334 | return list, e |
| 335 | } |
| 336 | return setIndex(list, i, v) |
| 337 | default: |
| 338 | return list, e |
| 339 | } |
| 340 | case last == '[': |
| 341 | // now we have a nested list. Read the index and handle. |
| 342 | nextI, err := t.keyIndex() |
| 343 | if err != nil { |
| 344 | return list, errors.Wrap(err, "error parsing index") |
| 345 | } |
| 346 | var crtList []interface{} |
| 347 | if len(list) > i { |
| 348 | // If nested list already exists, take the value of list to next cycle. |
| 349 | existed := list[i] |
| 350 | if existed != nil { |
| 351 | crtList = list[i].([]interface{}) |
| 352 | } |
| 353 | } |
| 354 | // Now we need to get the value after the ]. |
| 355 | list2, err := t.listItem(crtList, nextI) |
| 356 | if err != nil { |
| 357 | return list, err |
| 358 | } |
| 359 | return setIndex(list, i, list2) |
| 360 | case last == '.': |
| 361 | // We have a nested object. Send to t.key |
| 362 | inner := map[string]interface{}{} |
| 363 | if len(list) > i { |
| 364 | var ok bool |
| 365 | inner, ok = list[i].(map[string]interface{}) |
| 366 | if !ok { |
| 367 | // We have indices out of order. Initialize empty value. |