(t reflect.Type)
| 354 | } |
| 355 | |
| 356 | func typeEncoder(t reflect.Type) encoderFunc { |
| 357 | if fi, ok := encoderCache.Load(t); ok { |
| 358 | return fi.(encoderFunc) |
| 359 | } |
| 360 | |
| 361 | // To deal with recursive types, populate the map with an |
| 362 | // indirect func before we build it. This type waits on the |
| 363 | // real func (f) to be ready and then calls it. This indirect |
| 364 | // func is only used for recursive types. |
| 365 | var ( |
| 366 | wg sync.WaitGroup |
| 367 | f encoderFunc |
| 368 | ) |
| 369 | wg.Add(1) |
| 370 | fi, loaded := encoderCache.LoadOrStore(t, encoderFunc(func(e *encodeState, v reflect.Value, opts encOpts) { |
| 371 | wg.Wait() |
| 372 | f(e, v, opts) |
| 373 | })) |
| 374 | if loaded { |
| 375 | return fi.(encoderFunc) |
| 376 | } |
| 377 | |
| 378 | // Compute the real encoder and replace the indirect func with it. |
| 379 | f = newTypeEncoder(t, true) |
| 380 | wg.Done() |
| 381 | encoderCache.Store(t, f) |
| 382 | return f |
| 383 | } |
| 384 | |
| 385 | var ( |
| 386 | marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem() |
no test coverage detected