-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc61fa2
commit ca63a11
Showing
5 changed files
with
31 additions
and
43 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 |
---|---|---|
@@ -1,4 +1,14 @@ | ||
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/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= | ||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= | ||
github.com/lpernett/godotenv v0.0.0-20230527005122-0de1d4c5ef5e h1:6b4YTtccT1y/3eSsDCVhB6boPPCh5bQwP1Pa863yH28= | ||
github.com/lpernett/godotenv v0.0.0-20230527005122-0de1d4c5ef5e/go.mod h1:K+inF/XYdmRn4sSP3IU4EM3KcOdGVJUJqZPmrQSxjGo= | ||
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.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
go.uber.org/dig v1.18.0 h1:imUL1UiY0Mg4bqbFfsRQO5G4CGRBec/ZujWTvSVp3pw= | ||
go.uber.org/dig v1.18.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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,50 +1,28 @@ | ||
package di | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"sync" | ||
"go.uber.org/dig" | ||
) | ||
|
||
type Container struct { | ||
services map[string]interface{} | ||
mu sync.RWMutex | ||
container *dig.Container | ||
} | ||
|
||
func NewContainer() *Container { | ||
return &Container{ | ||
services: make(map[string]interface{}), | ||
container: dig.New(), | ||
} | ||
} | ||
|
||
func (c *Container) Register(name string, service interface{}) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
c.services[name] = service | ||
func (c *Container) Register(constructor interface{}) error { | ||
return c.container.Provide(constructor) | ||
} | ||
|
||
func (c *Container) Get(name string) (interface{}, error) { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
service, exists := c.services[name] | ||
if !exists { | ||
return nil, errors.New("service not found") | ||
} | ||
return service, nil | ||
// Provide is an alias for Register. It registers a constructor or provider in the container | ||
func (c *Container) Provide(constructor interface{}) error { | ||
return c.Register(constructor) | ||
} | ||
|
||
func (c *Container) Inject(target interface{}) error { | ||
val := reflect.ValueOf(target).Elem() | ||
for i := 0; i < val.NumField(); i++ { | ||
field := val.Field(i) | ||
if field.CanSet() && field.Kind() == reflect.Interface { | ||
serviceName := field.Type().String() | ||
service, err := c.Get(serviceName) | ||
if err != nil { | ||
return err | ||
} | ||
field.Set(reflect.ValueOf(service)) | ||
} | ||
} | ||
return nil | ||
func (c *Container) Invoke(function interface{}) error { | ||
return c.container.Invoke(function) | ||
} |
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