ResolveAs retrieves a component by both name and expected type.
(ctx context.Context, name string, typ reflect.Type)
| 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) { |
| 381 | if name == "" { |
| 382 | return nil, errors.New("empty name") |
| 383 | } |
| 384 | |
| 385 | if typ == nil { |
| 386 | return nil, errors.New("nil type") |
| 387 | } |
| 388 | |
| 389 | ctx = withCreationState(ctx) |
| 390 | |
| 391 | instance, err := d.Resolve(ctx, name) |
| 392 | if err != nil { |
| 393 | return nil, err |
| 394 | } |
| 395 | |
| 396 | instanceType := reflect.TypeOf(instance) |
| 397 | if !convertibleTo(instanceType, typ) { |
| 398 | return nil, fmt.Errorf("component %q is not assignable to %s", name, typ) |
| 399 | } |
| 400 | |
| 401 | return instance, nil |
| 402 | } |
| 403 | |
| 404 | // ResolveAll retrieves all instances assignable to the specified type. |
| 405 | func (d *DefaultContainer) ResolveAll(ctx context.Context, typ reflect.Type) ([]any, error) { |