ResolveType retrieves an instance of the specified type.
(ctx context.Context, typ reflect.Type)
| 346 | |
| 347 | // ResolveType retrieves an instance of the specified type. |
| 348 | func (d *DefaultContainer) ResolveType(ctx context.Context, typ reflect.Type) (any, error) { |
| 349 | if typ == nil { |
| 350 | return nil, errors.New("nil type") |
| 351 | } |
| 352 | |
| 353 | ctx = withCreationState(ctx) |
| 354 | |
| 355 | resolvableCandidates := d.findResolvableCandidates(typ) |
| 356 | if len(resolvableCandidates) > 1 { |
| 357 | return nil, errors.New("multiple instance found") |
| 358 | } else if len(resolvableCandidates) == 1 { |
| 359 | return resolvableCandidates[0], nil |
| 360 | } |
| 361 | |
| 362 | singletons := d.resolveSingletons(typ) |
| 363 | if len(singletons) > 1 { |
| 364 | return nil, errors.New("multiple singletons found") |
| 365 | } else if len(singletons) == 1 { |
| 366 | return singletons[0], nil |
| 367 | } |
| 368 | |
| 369 | definitions := d.DefinitionsOf(typ) |
| 370 | if len(definitions) > 1 { |
| 371 | return nil, errors.New("multiple definitions found") |
| 372 | } else if len(definitions) == 1 { |
| 373 | return d.Resolve(ctx, definitions[0].Name()) |
| 374 | } |
| 375 | |
| 376 | return nil, ErrInstanceNotFound |
| 377 | } |
| 378 | |
| 379 | // ResolveAs retrieves a component by both name and expected type. |
| 380 | func (d *DefaultContainer) ResolveAs(ctx context.Context, name string, typ reflect.Type) (any, error) { |