Skip to content

Commit

Permalink
Allow service override (#16)
Browse files Browse the repository at this point in the history
You can now replace a service that already exists in the Builder.
  • Loading branch information
sarulabs authored Aug 13, 2019
1 parent 0217505 commit bc5c1d2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
16 changes: 2 additions & 14 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (b *Builder) Add(defs ...Def) error {
}

func (b *Builder) add(def Def) error {
if err := b.checkName(def.Name); err != nil {
return err
if def.Name == "" {
return errors.New("name can not be empty")
}

// note that an empty scope is allowed
Expand All @@ -102,18 +102,6 @@ func (b *Builder) add(def Def) error {
return nil
}

func (b *Builder) checkName(name string) error {
if name == "" {
return errors.New("name can not be empty")
}

if b.IsDefined(name) {
return fmt.Errorf("name `%s` is already defined", name)
}

return nil
}

// Build creates a Container in the most generic scope
// with all the definitions registered in the Builder.
func (b *Builder) Build() Container {
Expand Down
27 changes: 24 additions & 3 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ func TestIsDefined(t *testing.T) {
require.False(t, b.IsDefined("undefined"))
}

func TestServiceOverride(t *testing.T) {
b, _ := NewBuilder()

var err error

err = b.Add(Def{
Name: "name",
Build: func(ctn Container) (interface{}, error) {
return "first", nil
},
})
require.Nil(t, err)

err = b.Add(Def{
Name: "name",
Build: func(ctn Container) (interface{}, error) {
return "second", nil
},
})
require.Nil(t, err)

require.Equal(t, "second", b.Build().Get("name").(string))
}

func TestAddErrors(t *testing.T) {
b, _ := NewBuilder()

Expand All @@ -72,9 +96,6 @@ func TestAddErrors(t *testing.T) {
err = b.Add(Def{Name: "object", Scope: "undefined", Build: buildFunc})
require.NotNil(t, err, "should not be able to add a Def in an undefined scope")

err = b.Add(Def{Name: "name", Scope: App, Build: buildFunc})
require.NotNil(t, err, "should not be able to add a Def if the name is already used")

err = b.Add(Def{Name: "", Scope: App, Build: buildFunc})
require.NotNil(t, err, "should not be able to add a Def if the name is empty")

Expand Down
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
module github.com/sarulabs/di/v2

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
)
require github.com/stretchr/testify v1.3.0
9 changes: 5 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

0 comments on commit bc5c1d2

Please sign in to comment.