From 8af34ddf183218337f0c494d26ad092420f0edab Mon Sep 17 00:00:00 2001 From: Sam Parton Date: Sat, 24 Sep 2022 18:21:50 +0100 Subject: [PATCH] Minor changes... for some reason, reflect.Type isn't a "Comparable" type anymore, don't understand why. Removed maps.x/slices.x which caused this error --- container_main.go | 22 +++++++++++++++------- container_tagged.go | 30 +++++++++++++++++++----------- go.mod | 7 ++++--- go.sum | 2 +- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/container_main.go b/container_main.go index 24ce76f..ea5b3db 100644 --- a/container_main.go +++ b/container_main.go @@ -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 @@ -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 } diff --git a/container_tagged.go b/container_tagged.go index 432f635..6c0a6aa 100644 --- a/container_tagged.go +++ b/container_tagged.go @@ -2,8 +2,6 @@ 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 @@ -11,16 +9,15 @@ import ( // // 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 @@ -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 diff --git a/go.mod b/go.mod index 1c80b1a..4d07b14 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 5d9e0c6..b05f139 100644 --- a/go.sum +++ b/go.sum @@ -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=