Skip to content

Commit

Permalink
add errors to API
Browse files Browse the repository at this point in the history
this change adds proper errors to NewInjector, Child, GetInstance, GetAnnotatedInstance and
RequestInjection.
also the error handling has been revisited, to be more precise and clear
in error situations.
panic() only happens in case of Bind(nil) and if providers don't accept
errors.
Providers can now be of type somethingProvider() Something or
somethingProvider() (Something, error), later case doesn't panic, but
returns an error if resolving/providing fails.
  • Loading branch information
bastianccm committed Dec 16, 2019
1 parent 5712389 commit a9d3c82
Show file tree
Hide file tree
Showing 16 changed files with 461 additions and 176 deletions.
13 changes: 8 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ language: go

go:
- 1.x
- 1.11.x
- 1.12.x

env:
- GO111MODULE=on

install: true

script:
- go get -u golang.org/x/lint/golint
- golint -set_exit_status $(go list ./...)
- go test -v -vet=all ./...
- go test -v -race ./...
- set -e
- fmt=$(gofmt -l .)
- test -z $fmt || (echo "please run gofmt" ; echo $fmt ; exit 1)
- go run golang.org/x/lint/golint -set_exit_status $(go list ./...)
- go test -v ./...
- go test -v -race -vet=all ./...
- git diff --quiet || (echo 'generated go files are not up to date, check go generate, go.sum and go.mod' ; git diff ; exit 1)
10 changes: 6 additions & 4 deletions binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ func (b *Binding) equal(to *Binding) bool {
}

// Create creates a new instance by the provider and requests injection, all provider arguments are automatically filled
func (p *Provider) Create(injector *Injector) reflect.Value {
func (p *Provider) Create(injector *Injector) (reflect.Value, error) {
in := make([]reflect.Value, p.fnc.Type().NumIn())
var err error
for i := 0; i < p.fnc.Type().NumIn(); i++ {
in[i] = injector.getInstance(p.fnc.Type().In(i), "", traceCircular)
if in[i], err = injector.getInstance(p.fnc.Type().In(i), "", traceCircular); err != nil {
return reflect.Value{}, err
}
}
res := p.fnc.Call(in)[0]
injector.requestInjection(res, traceCircular)
return res
return res, injector.requestInjection(res, traceCircular)
}
28 changes: 17 additions & 11 deletions circular_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,39 @@ type (
)

func TestDingoCircula(t *testing.T) {
traceCircular = make([]circularTraceEntry, 0)
EnableCircularTracing()
defer func() {
traceCircular = nil
}()

injector := NewInjector()
injector, err := NewInjector()
assert.NoError(t, err)

assert.Panics(t, func() {
_, ok := injector.GetInstance(new(circA)).(*circA)
i, err := injector.GetInstance(new(circA))
assert.NoError(t, err)
_, ok := i.(*circA)
if !ok {
t.Fail()
}
})

injector.Bind(new(circCInterface)).To(circC{})
assert.NotPanics(t, func() {
c, ok := injector.GetInstance(new(circC)).(*circC)
if !ok {
t.Fail()
}

assert.NotNil(t, c.C())
})
i, err := injector.GetInstance(new(circC))
assert.NoError(t, err)
c, ok := i.(*circC)
if !ok {
t.Fail()
}
assert.NotNil(t, c.C())

var d *circD
assert.NotPanics(t, func() {
var ok bool
d, ok = injector.GetInstance(new(circD)).(*circD)
i, err := injector.GetInstance(new(circD))
assert.NoError(t, err)
d, ok = i.(*circD)
if !ok {
t.Fail()
}
Expand Down
Loading

0 comments on commit a9d3c82

Please sign in to comment.