NamedResolve takes abstraction and its name and fills it with the related concrete.
(abstraction interface{}, name string)
| 226 | |
| 227 | // NamedResolve takes abstraction and its name and fills it with the related concrete. |
| 228 | func (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"` |
| 253 | func (c Container) Fill(structure interface{}) error { |