Skip to content

Commit

Permalink
Remove Must* funcs to greatly reduce API surface area. (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grayson Koonce authored May 3, 2017
1 parent 188fe23 commit 043c731
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 101 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

## v0.4 (unreleased)

- Remove `Must*` funcs to greatly reduce API surface area.

## v0.3 (2 May 2017)

- Rename `RegisterAll` and `MustRegisterAll` to `ProvideAll` and
Expand Down
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,6 @@ err := c.Provide(&Type1{Name: "I am an thing"})
// to other constructors that require it.
```

## Error Handling and Alternatives

`Provide` and `Resolve` (and their `ProvideAll` and `ResolveAll` counterparts)
return errors, and usages of `dig` should fully utilize the error checking, since
container creation is done through reflection meaning a lot of the errors surface
at runtime.

There are, however, `Must*` alternatives of all the methods available. They
are drawn from some of the patterns in the Go standard library and are there
to simplify usage in critical scenarios: where not being able to resolve an
object is not an option and panic is preferred.

[doc]: https://godoc.org/go.uber.org/dig
[doc-img]: https://godoc.org/go.uber.org/dig?status.svg
[cov]: https://coveralls.io/github/uber-go/dig?branch=master
Expand Down
28 changes: 0 additions & 28 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,6 @@ func (c *Container) Provide(t interface{}) error {
}
}

// MustRegister will attempt to Provide the object and panic if error is encountered
func (c *Container) MustRegister(t interface{}) {
if err := c.Provide(t); err != nil {
panic(err)
}
}

// Resolve all of the dependencies of the provided class
//
// Provided object must be a pointer
Expand Down Expand Up @@ -152,13 +145,6 @@ func (c *Container) Resolve(obj interface{}) (err error) {
return nil
}

// MustResolve calls Resolve and panics if an error is encountered
func (c *Container) MustResolve(obj interface{}) {
if err := c.Resolve(obj); err != nil {
panic(err)
}
}

// ResolveAll the dependencies of each provided object
// Returns the first error encountered
func (c *Container) ResolveAll(objs ...interface{}) error {
Expand All @@ -170,13 +156,6 @@ func (c *Container) ResolveAll(objs ...interface{}) error {
return nil
}

// MustResolveAll calls ResolveAll and panics if an error is encountered
func (c *Container) MustResolveAll(objs ...interface{}) {
if err := c.ResolveAll(objs...); err != nil {
panic(err)
}
}

// ProvideAll registers all the provided args in the Container
func (c *Container) ProvideAll(types ...interface{}) error {
for _, t := range types {
Expand All @@ -186,10 +165,3 @@ func (c *Container) ProvideAll(types ...interface{}) error {
}
return nil
}

// MustProvideAll calls ProvideAll and panics is an error is encountered
func (c *Container) MustProvideAll(types ...interface{}) {
if err := c.ProvideAll(types...); err != nil {
panic(err)
}
}
61 changes: 0 additions & 61 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,67 +445,6 @@ func TestPanicConstructor(t *testing.T) {
require.Contains(t, err.Error(), "RUH ROH")
}

func TestMustFunctions(t *testing.T) {
t.Parallel()
tts := []struct {
name string
f func(c *Container)
panicExpected bool
}{
{
"wrong Provide type",
func(c *Container) { c.MustRegister(2) },
true,
},
{
"wrong Provide all types",
func(c *Container) { c.MustProvideAll("2", "3") },
true,
},
{
"unregistered type",
func(c *Container) {
var v *Type1
c.MustResolve(&v)
},
true,
},
{
"correct Provide",
func(c *Container) { c.MustRegister(NewChild1) },
false,
},
{
"correct Provide all",
func(c *Container) { c.MustProvideAll(NewChild1, NewChild2) },
false,
},
{
"unregistered types",
func(c *Container) {
var v *Type1
var v2 *Type2
c.MustResolveAll(&v, &v2)
},
true,
},
}

for _, tc := range tts {
t.Run(tc.name, func(t *testing.T) {
c := New()
f := func() {
tc.f(c)
}
if tc.panicExpected {
require.Panics(t, f)
} else {
require.NotPanics(t, f)
}
})
}
}

func TestMultiObjectRegisterResolve(t *testing.T) {
t.Parallel()
c := New()
Expand Down

0 comments on commit 043c731

Please sign in to comment.