Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added generics and must methods (#39)
- Loading branch information
Showing
4 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//go:build go1.18 | ||
|
||
package container | ||
|
||
func ResolveT[T any](c Container) (T, error) { | ||
var defaultvalue T | ||
|
||
if err := c.Resolve(&defaultvalue); err != nil { | ||
return defaultvalue, err | ||
} | ||
|
||
return defaultvalue, nil | ||
} | ||
|
||
func MustResolveT[T any](c Container) T { | ||
return must(ResolveT[T](c)) | ||
} | ||
|
||
func must[T any](value T, err error) T { | ||
if err != nil { | ||
panic(err) | ||
} | ||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
module github.com/golobby/container/v3 | ||
|
||
go 1.11 | ||
go 1.18 | ||
|
||
require github.com/stretchr/testify v1.7.0 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.0 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package container | ||
|
||
func MustSingleton(c Container, resolver interface{}) { | ||
if err := c.Singleton(resolver); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func MustNamedSingleton(c Container, name string, resolver interface{}) { | ||
if err := c.NamedSingleton(name, resolver); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func MustTransient(c Container, resolver interface{}) { | ||
if err := c.Transient(resolver); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func MustNamedTransient(c Container, name string, resolver interface{}) { | ||
if err := c.NamedTransient(name, resolver); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func MustCall(c Container, receiver interface{}) { | ||
if err := c.Call(receiver); err != nil { | ||
panic(err) | ||
} | ||
} |