Skip to content

Commit

Permalink
feat(project): added default container
Browse files Browse the repository at this point in the history
  • Loading branch information
Axel Etcheverry committed Oct 16, 2018
1 parent 298809e commit 994fb17
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 4 deletions.
5 changes: 1 addition & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions container.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
4 changes: 4 additions & 0 deletions container_test.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
37 changes: 37 additions & 0 deletions default.go
Original file line number Diff line number Diff line change
@@ -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)
}
81 changes: 81 additions & 0 deletions default_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit 994fb17

Please sign in to comment.