| 334 | } |
| 335 | |
| 336 | func (t *parser) listItem(list []any, i, nestedNameLevel int) ([]any, error) { |
| 337 | if i < 0 { |
| 338 | return list, fmt.Errorf("negative %d index not allowed", i) |
| 339 | } |
| 340 | stop := runeSet([]rune{'[', '.', '='}) |
| 341 | switch k, last, err := runesUntil(t.sc, stop); { |
| 342 | case len(k) > 0: |
| 343 | return list, fmt.Errorf("unexpected data at end of array index: %q", k) |
| 344 | case err != nil: |
| 345 | return list, err |
| 346 | case last == '=': |
| 347 | if t.isjsonval { |
| 348 | empval, err := t.emptyVal() |
| 349 | if err != nil { |
| 350 | return list, err |
| 351 | } |
| 352 | if empval { |
| 353 | return setIndex(list, i, nil) |
| 354 | } |
| 355 | // parse jsonvals by using Go’s JSON standard library |
| 356 | // Decode is preferred to Unmarshal in order to parse just the json parts of the list key1=jsonval1,key2=jsonval2,... |
| 357 | // Since Decode has its own buffer that consumes more characters (from underlying t.sc) than the ones actually decoded, |
| 358 | // we invoke Decode on a separate reader built with a copy of what is left in t.sc. After Decode is executed, we |
| 359 | // discard in t.sc the chars of the decoded json value (the number of those characters is returned by InputOffset). |
| 360 | var jsonval any |
| 361 | dec := json.NewDecoder(strings.NewReader(t.sc.String())) |
| 362 | if err = dec.Decode(&jsonval); err != nil { |
| 363 | return list, err |
| 364 | } |
| 365 | if list, err = setIndex(list, i, jsonval); err != nil { |
| 366 | return list, err |
| 367 | } |
| 368 | if _, err = io.CopyN(io.Discard, t.sc, dec.InputOffset()); err != nil { |
| 369 | return list, err |
| 370 | } |
| 371 | // skip possible blanks and comma |
| 372 | _, err = t.emptyVal() |
| 373 | return list, err |
| 374 | } |
| 375 | vl, e := t.valList() |
| 376 | switch e { |
| 377 | case nil: |
| 378 | return setIndex(list, i, vl) |
| 379 | case io.EOF: |
| 380 | return setIndex(list, i, "") |
| 381 | case ErrNotList: |
| 382 | rs, e := t.val() |
| 383 | if e != nil && e != io.EOF { |
| 384 | return list, e |
| 385 | } |
| 386 | v, e := t.reader(rs) |
| 387 | if e != nil { |
| 388 | return list, e |
| 389 | } |
| 390 | return setIndex(list, i, v) |
| 391 | default: |
| 392 | return list, e |
| 393 | } |