Skip to content

Commit

Permalink
Minor changes... for some reason, reflect.Type isn't a "Comparable" t…
Browse files Browse the repository at this point in the history
…ype anymore, don't understand why.

Removed maps.x/slices.x which caused this error
  • Loading branch information
iDevelopThings committed Sep 24, 2022
1 parent 47b731d commit 8af34dd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
22 changes: 15 additions & 7 deletions container_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package container
import (
"reflect"
"unsafe"

"golang.org/x/exp/maps"
)

// ContainerConfig - Holds configuration values... soon I will add some more, make them work fully
Expand Down Expand Up @@ -80,16 +78,26 @@ func (container *ContainerInstance) CreateChildContainer() *ContainerInstance {
// ClearInstances - This will just remove any singleton instances from the container
// When they are next resolved via Make/MakeTo, they will be instantiated again
func (container *ContainerInstance) ClearInstances() {
maps.Clear(container.resolved)
for k := range container.resolved {
delete(container.resolved, k)
}
}

// Reset - Reset will empty all bindings in this container, you will have to register
// any bindings again before you can resolve them.
func (container *ContainerInstance) Reset() {
maps.Clear(container.resolved)
maps.Clear(container.bindings)
maps.Clear(container.concretes)
maps.Clear(container.tagged)
for k := range container.resolved {
delete(container.resolved, k)
}
for k := range container.bindings {
delete(container.bindings, k)
}
for k := range container.concretes {
delete(container.concretes, k)
}
for k := range container.tagged {
delete(container.tagged, k)
}
container.parent = nil
}

Expand Down
30 changes: 19 additions & 11 deletions container_tagged.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@ package container

import (
"reflect"

"golang.org/x/exp/slices"
)

// Tag - When we've bound to the container, we can then tag the abstracts with a string
// This is useful when we want to obtain a "category" of implementations
//
// For example; Imagine we have a few different "statistic gathering" services
//
// // Bind our individual services
// Container.Bind(new(NewUserPostViewsStatService), func () {})
// Container.Bind(new(NewPageViewsStatService), func () {})
//
// // Add the services to the "StatServices" "Category"
// Container.Tag("StatServices", new(NewUserPostViewsStatService), new(NewPageViewsStatService))
// // Bind our individual services
// Container.Bind(new(NewUserPostViewsStatService), func () {})
// Container.Bind(new(NewPageViewsStatService), func () {})
//
// // Now we can obtain them all
// Container.Tagged("StatServices")
// // Add the services to the "StatServices" "Category"
// Container.Tag("StatServices", new(NewUserPostViewsStatService), new(NewPageViewsStatService))
//
// // Now we can obtain them all
// Container.Tagged("StatServices")
func (container *ContainerInstance) Tag(tag string, bindings ...any) bool {
if len(bindings) == 0 {
return false
Expand Down Expand Up @@ -50,9 +47,20 @@ func (container *ContainerInstance) Tag(tag string, bindings ...any) bool {

// We have types tagged with this tag already, so we need to merge, but make sure they're unique
for _, taggedType := range taggedTypes {
if !slices.Contains(container.tagged[tag], taggedType) {
// check if container.tagged[tag] contains taggedType in the slice
var found bool = false
for _, r := range container.tagged[tag] {
if r == taggedType {
found = true
break
}
}
if !found {
container.tagged[tag] = append(container.tagged[tag], taggedType)
}
// if !slices.Contains(container.tagged[tag], taggedType) {
// container.tagged[tag] = append(container.tagged[tag], taggedType)
// }
}

return len(container.tagged[tag]) > 0
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ go 1.18

require golang.org/x/exp v0.0.0-20220218215828-6cf2b201936e

require github.com/modern-go/reflect2 v1.0.2
require (
github.com/modern-go/reflect2 v1.0.2
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
github.com/stretchr/objx v0.1.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
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/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/exp v0.0.0-20220218215828-6cf2b201936e h1:iWVPgObh6F4UDtjBLK51zsy5UHTPLQwCmsNjCsbKhQ0=
golang.org/x/exp v0.0.0-20220218215828-6cf2b201936e/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 8af34dd

Please sign in to comment.