From 943285313bf60fddc1e3f9de81c28ddb48c15b67 Mon Sep 17 00:00:00 2001 From: James Batt Date: Sat, 16 Apr 2022 17:30:58 +1000 Subject: [PATCH] nil handling for Call() (#40) * nil handling for Call() * added a testcase --- container.go | 3 +++ container_test.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/container.go b/container.go index c3537a0..6f6ac44 100644 --- a/container.go +++ b/container.go @@ -150,6 +150,9 @@ func (c Container) Call(function interface{}) error { if len(result) == 0 { return nil } else if len(result) == 1 && result[0].CanInterface() { + if result[0].IsNil() { + return nil + } if err, ok := result[0].Interface().(error); ok { return err } diff --git a/container_test.go b/container_test.go index 8d968ca..448f1da 100644 --- a/container_test.go +++ b/container_test.go @@ -223,6 +223,20 @@ func TestContainer_Call_With_A_Returning_Error(t *testing.T) { assert.EqualError(t, err, "app: some context error") } +func TestContainer_Call_With_A_Returning_Nil_Error(t *testing.T) { + instance.Reset() + + err := instance.Singleton(func() Shape { + return &Circle{} + }) + assert.NoError(t, err) + + err = instance.Call(func(s Shape) error { + return nil + }) + assert.Nil(t, err) +} + func TestContainer_Call_With_Invalid_Signature(t *testing.T) { instance.Reset()