Skip to content

Singleton resolvers that return error during resolution should not cache the concrete instance #57

Description

@wbreza

Description

Singleton resolvers that return error during resolution should not cache the concrete instance

Steps to Reproduce

Given the following Go struct

type Foo struct {
    Value string
}

func NewFoo(value string) *Foo {
  return &Foo{ Value: value }
}

When using the container in this package if I register a resolver for a singleton where it could conditionally fail based on some expectation:

container.Singleton(func() (*Foo, error) {
  if expectationsAreMet {
    return NewFoo("test"), nil
  } else {
    return nil, errors.New("cannot create this type right now")
  }
})

Then when the caller attempts to re-resolve the type the cached nil value is returned instead of the resolver trying again.

var instance *Foo
err := container.Resolve(&instance) // Resolution can fail here if external expectation is not met
if err != nil {
  // Perform some bootstrapping to meeting required expectation
  external.MeetExpectation()
  
  // Try to resolve the type again after expectations are met
  err = container.Resolve(&instance) // Resolver should try again now that expectation has been met.
}

Expected

The registered singleton resolver should run again since the original resolution failed and return an error

Actual

The nil value is stored as the concrete value for the singleton and is returned on subsequent Resolve(...) calls.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions