MCPcopy Create free account
hub / github.com/codnect/procyon / ResolveAs

Method ResolveAs

component/container.go:380–402  ·  view source on GitHub ↗

ResolveAs retrieves a component by both name and expected type.

(ctx context.Context, name string, typ reflect.Type)

Source from the content-addressed store, hash-verified

378
379// ResolveAs retrieves a component by both name and expected type.
380func (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.
405func (d *DefaultContainer) ResolveAll(ctx context.Context, typ reflect.Type) ([]any, error) {

Callers 1

Calls 3

ResolveMethod · 0.95
withCreationStateFunction · 0.85
convertibleToFunction · 0.85

Tested by 1