diff --git a/.golangci.yml b/.golangci.yml index ca20d5f..4ad9d54 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -2,10 +2,7 @@ run: concurrency: 4 deadline: 1m issues-exit-code: 1 - tests: true - skip-dirs: - - build - - ghost + tests: false skip-files: - ".*_mock\\.go" - "mock_.*\\.go" diff --git a/container.go b/container.go index 5f408e5..e4e1354 100644 --- a/container.go +++ b/container.go @@ -1,3 +1,7 @@ +// Copyright 2018 Axel Etcheverry. All rights reserved. +// Use of this source code is governed by a MIT +// license that can be found in the LICENSE file. + package service import ( diff --git a/container_test.go b/container_test.go index 344013c..158acf0 100644 --- a/container_test.go +++ b/container_test.go @@ -1,3 +1,7 @@ +// Copyright 2018 Axel Etcheverry. All rights reserved. +// Use of this source code is governed by a MIT +// license that can be found in the LICENSE file. + package service import ( diff --git a/default.go b/default.go new file mode 100644 index 0000000..60c0d1a --- /dev/null +++ b/default.go @@ -0,0 +1,37 @@ +// Copyright 2018 Axel Etcheverry. All rights reserved. +// Use of this source code is governed by a MIT +// license that can be found in the LICENSE file. + +package service + +var defaultContainer = New() + +// Set service +func Set(name string, f ContainerFunc) { + defaultContainer.Set(name, f) +} + +// Has service exists +func Has(name string) bool { + return defaultContainer.Has(name) +} + +// Get service +func Get(name string) interface{} { + return defaultContainer.Get(name) +} + +// GetKeys of all services +func GetKeys() []string { + return defaultContainer.GetKeys() +} + +// Fill dst +func Fill(name string, dst interface{}) { + defaultContainer.Fill(name, dst) +} + +// Extend service +func Extend(name string, f ExtenderFunc) { + defaultContainer.Extend(name, f) +} diff --git a/default_test.go b/default_test.go new file mode 100644 index 0000000..8b0d05b --- /dev/null +++ b/default_test.go @@ -0,0 +1,81 @@ +// Copyright 2018 Axel Etcheverry. All rights reserved. +// Use of this source code is governed by a MIT +// license that can be found in the LICENSE file. + +package service + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDefaultContainer(t *testing.T) { + assert.False(t, Has("test.bad.service.name")) + + assert.Equal(t, []string{}, GetKeys()) + + Set("my.service", func(c Container) interface{} { + return &MyService{} + }) + + Extend("my.service", func(s *MyService) *MyService { + s.Name = "My Service" + + return s + }) + + assert.True(t, Has("my.service")) + + assert.Equal(t, []string{"my.service"}, GetKeys()) + + Set("my.service", func(c Container) interface{} { + return &MyService{} + }) + + myService1 := Get("my.service").(*MyService) + + myService2 := Get("my.service").(*MyService) + + assert.Equal(t, myService1, myService2) + + assert.Equal(t, "My Service", myService1.Name) + + assert.Panics(t, func() { + Set("my.service", func(c Container) interface{} { + return &MyService{} + }) + }) + + assert.Panics(t, func() { + Extend("my.service", func(s *MyService) *MyService { + s.Name = "My Service 2" + + return s + }) + }) + + assert.Panics(t, func() { + Extend("not.exists.service", func(s *MyService) *MyService { + s.Name = "My Service 3" + + return s + }) + }) + + assert.Panics(t, func() { + Get("test.bad.service.name") + }) + + var myService3 *MyService + + Fill("my.service", &myService3) + + assert.Equal(t, myService2, myService3) + + assert.Panics(t, func() { + var bad string + + Fill("my.service", &bad) + }) +}