MCPcopy
hub / github.com/openai/openai-go / array

Method array

internal/encoding/json/decode.go:509–599  ·  view source on GitHub ↗

array consumes an array from d.data[d.off-1:], decoding into v. The first byte of the array ('[') has been read already.

(v reflect.Value)

Source from the content-addressed store, hash-verified

507// array consumes an array from d.data[d.off-1:], decoding into v.
508// The first byte of the array ('[') has been read already.
509func (d *decodeState) array(v reflect.Value) error {
510 // Check for unmarshaler.
511 u, ut, pv := indirect(v, false)
512 if u != nil {
513 start := d.readIndex()
514 d.skip()
515 return u.UnmarshalJSON(d.data[start:d.off])
516 }
517 if ut != nil {
518 d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)})
519 d.skip()
520 return nil
521 }
522 v = pv
523
524 // Check type of target.
525 switch v.Kind() {
526 case reflect.Interface:
527 if v.NumMethod() == 0 {
528 // Decoding into nil interface? Switch to non-reflect code.
529 ai := d.arrayInterface()
530 v.Set(reflect.ValueOf(ai))
531 return nil
532 }
533 // Otherwise it's invalid.
534 fallthrough
535 default:
536 d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)})
537 d.skip()
538 return nil
539 case reflect.Array, reflect.Slice:
540 break
541 }
542
543 i := 0
544 for {
545 // Look ahead for ] - can only happen on first iteration.
546 d.scanWhile(scanSkipSpace)
547 if d.opcode == scanEndArray {
548 break
549 }
550
551 // Expand slice length, growing the slice if necessary.
552 if v.Kind() == reflect.Slice {
553 if i >= v.Cap() {
554 v.Grow(1)
555 }
556 if i >= v.Len() {
557 v.SetLen(i + 1)
558 }
559 }
560
561 if i < v.Len() {
562 // Decode into element.
563 if err := d.value(v.Index(i)); err != nil {
564 return err
565 }
566 } else {

Callers 1

valueMethod · 0.95

Calls 9

readIndexMethod · 0.95
skipMethod · 0.95
saveErrorMethod · 0.95
arrayInterfaceMethod · 0.95
scanWhileMethod · 0.95
valueMethod · 0.95
indirectFunction · 0.85
UnmarshalJSONMethod · 0.65
IndexMethod · 0.45

Tested by

no test coverage detected