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

Function newTypeEncoder

internal/encoding/json/encode.go:416–469  ·  view source on GitHub ↗

newTypeEncoder constructs an encoderFunc for a type. The returned encoder only checks CanAddr when allowAddr is true.

(t reflect.Type, allowAddr bool)

Source from the content-addressed store, hash-verified

414// newTypeEncoder constructs an encoderFunc for a type.
415// The returned encoder only checks CanAddr when allowAddr is true.
416func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc {
417 // EDIT(begin): add custom time encoder
418 if t == timeType {
419 return newTimeEncoder()
420 }
421 // EDIT(end)
422
423 // If we have a non-pointer value whose type implements
424 // Marshaler with a value receiver, then we're better off taking
425 // the address of the value - otherwise we end up with an
426 // allocation as we cast the value to an interface.
427 if t.Kind() != reflect.Pointer && allowAddr && reflect.PointerTo(t).Implements(marshalerType) {
428 return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false))
429 }
430
431 if t.Implements(marshalerType) {
432 return marshalerEncoder
433 }
434 if t.Kind() != reflect.Pointer && allowAddr && reflect.PointerTo(t).Implements(textMarshalerType) {
435 return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false))
436 }
437 if t.Implements(textMarshalerType) {
438 return textMarshalerEncoder
439 }
440
441 switch t.Kind() {
442 case reflect.Bool:
443 return boolEncoder
444 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
445 return intEncoder
446 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
447 return uintEncoder
448 case reflect.Float32:
449 return float32Encoder
450 case reflect.Float64:
451 return float64Encoder
452 case reflect.String:
453 return stringEncoder
454 case reflect.Interface:
455 return interfaceEncoder
456 case reflect.Struct:
457 return newStructEncoder(t)
458 case reflect.Map:
459 return newMapEncoder(t)
460 case reflect.Slice:
461 return newSliceEncoder(t)
462 case reflect.Array:
463 return newArrayEncoder(t)
464 case reflect.Pointer:
465 return newPtrEncoder(t)
466 default:
467 return unsupportedTypeEncoder
468 }
469}
470
471func invalidValueEncoder(e *encodeState, v reflect.Value, _ encOpts) {
472 e.WriteString("null")

Callers 1

typeEncoderFunction · 0.85

Calls 7

newTimeEncoderFunction · 0.85
newCondAddrEncoderFunction · 0.85
newStructEncoderFunction · 0.85
newMapEncoderFunction · 0.85
newSliceEncoderFunction · 0.85
newArrayEncoderFunction · 0.85
newPtrEncoderFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…