MCPcopy Create free account
hub / github.com/golobby/container / NamedResolve

Method NamedResolve

container.go:228–250  ·  view source on GitHub ↗

NamedResolve takes abstraction and its name and fills it with the related concrete.

(abstraction interface{}, name string)

Source from the content-addressed store, hash-verified

226
227// NamedResolve takes abstraction and its name and fills it with the related concrete.
228func (c Container) NamedResolve(abstraction interface{}, name string) error {
229 receiverType := reflect.TypeOf(abstraction)
230 if receiverType == nil {
231 return errors.New("container: invalid abstraction")
232 }
233
234 if receiverType.Kind() == reflect.Ptr {
235 elem := receiverType.Elem()
236
237 if concrete, exist := c[elem][name]; exist {
238 if instance, err := concrete.make(c); err == nil {
239 reflect.ValueOf(abstraction).Elem().Set(reflect.ValueOf(instance))
240 return nil
241 } else {
242 return fmt.Errorf("container: encountered error while making concrete for: %s. Error encountered: %w", elem.String(), err)
243 }
244 }
245
246 return errors.New("container: no concrete found for: " + elem.String())
247 }
248
249 return errors.New("container: invalid abstraction")
250}
251
252// Fill takes a struct and resolves the fields with the tag `container:"inject"`
253func (c Container) Fill(structure interface{}) error {

Callers 9

ResolveMethod · 0.95
NamedResolveFunction · 0.80
TestNamedResolveFunction · 0.80
MustNamedResolveFunction · 0.80

Calls 1

makeMethod · 0.80