Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
change constructor to provider
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Oct 30, 2019
1 parent 9779124 commit e81b7e5
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 63 deletions.
15 changes: 13 additions & 2 deletions EXPERIMENTAL/typicmd/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ func (a application) Run(ctx *cli.Context) (err error) {
gracefulStop := make(chan os.Signal)
signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
if err = a.Construct(di); err != nil {
return
for _, constructor := range a.Provide() {
if err = di.Provide(constructor); err != nil {
return
}
}
// TODO: create prepare function
// for _, initiation := range a.Initiations {
Expand All @@ -39,3 +41,12 @@ func (a application) Run(ctx *cli.Context) (err error) {
runner := a.Application.(typiobj.Runner)
return runner.Run(di)
}

func (a application) Provide() (constructors []interface{}) {
constructors = append(constructors, a.Constructors...)
constructors = append(constructors, a.Modules.Provide()...)
if provider, ok := a.Application.(typiobj.Provider); ok {
constructors = append(constructors, provider.Provide()...)
}
return
}
15 changes: 0 additions & 15 deletions EXPERIMENTAL/typictx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,6 @@ func (c *Context) Configurations() (cfgs []typiobj.Configuration) {
// return
// }

// Construct dependencies
func (c *Context) Construct(container *dig.Container) (err error) {
for _, constructor := range c.Constructors {
if err = container.Provide(constructor); err != nil {
return err
}
}
if constructor, ok := c.Application.(typiobj.Constructor); ok {
if err = constructor.Construct(container); err != nil {
return
}
}
return c.Modules.Construct(container)
}

// Destruct dependencies
func (c *Context) Destruct(container *dig.Container) (err error) {
if destructor, ok := c.Application.(typiobj.Destructor); ok {
Expand Down
8 changes: 5 additions & 3 deletions EXPERIMENTAL/typiobj/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ func CliAction(p interface{}, fn interface{}) func(ctx *cli.Context) error {
destructor.Destruct(c)
}
}()
if constructor, ok := p.(Constructor); ok {
if err = constructor.Construct(c); err != nil {
return
if provider, ok := p.(Provider); ok {
for _, constructor := range provider.Provide() {
if err = c.Provide(constructor); err != nil {
return
}
}
}
return c.Invoke(fn)
Expand Down
10 changes: 4 additions & 6 deletions EXPERIMENTAL/typiobj/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ func (m Modules) Commands() (cmds []cli.Command) {
return
}

// Construct dependency
func (m Modules) Construct(c *dig.Container) (err error) {
// Provide dependency
func (m Modules) Provide() (constructors []interface{}) {
for _, module := range m {
if constructor, ok := module.(Constructor); ok {
if err = constructor.Construct(c); err != nil {
return
}
if provider, ok := module.(Provider); ok {
constructors = append(constructors, provider.Provide()...)
}
}
return
Expand Down
6 changes: 3 additions & 3 deletions EXPERIMENTAL/typiobj/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func Description(obj interface{}) string {
return "<description>"
}

// IsConstructor return true if object implementation of constructor
func IsConstructor(obj interface{}) (ok bool) {
_, ok = obj.(Constructor)
// IsProvider return true if object implementation of provider
func IsProvider(obj interface{}) (ok bool) {
_, ok = obj.(Provider)
return
}

Expand Down
6 changes: 3 additions & 3 deletions EXPERIMENTAL/typiobj/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ type Runner interface {
Run(c *dig.Container) error
}

// Constructor responsible to construct dependency
type Constructor interface {
Construct(c *dig.Container) error
// Provider responsible to provide dependency
type Provider interface {
Provide() []interface{}
}

// Destructor responsible to destruct dependency
Expand Down
6 changes: 4 additions & 2 deletions app/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ func (m applicationModule) Run(c *dig.Container) (err error) {
return c.Invoke(Start)
}

func (m applicationModule) Construct(c *dig.Container) (err error) {
return c.Provide(m.loadConfig)
func (m applicationModule) Provide() []interface{} {
return []interface{}{
m.loadConfig,
}
}

func (m applicationModule) loadConfig() (cfg *config.Config, err error) {
Expand Down
10 changes: 6 additions & 4 deletions pkg/typpostgres/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ func (p postgresModule) CommandLine() cli.Command {
}
}

// Construct dependencies
func (p postgresModule) Construct(c *dig.Container) (err error) {
c.Provide(p.loadConfig)
return c.Provide(p.openConnection)
// Provide dependencies
func (p postgresModule) Provide() []interface{} {
return []interface{}{
p.loadConfig,
p.openConnection,
}
}

// Destruct dependencies
Expand Down
12 changes: 6 additions & 6 deletions pkg/typpostgres/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typictx"
"github.com/typical-go/typical-rest-server/pkg/module/typpostgres"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj"
"github.com/typical-go/typical-rest-server/pkg/typpostgres"
)

func TestModule(t *testing.T) {
m := typpostgres.Module()
require.True(t, typictx.IsConstructor(m))
require.True(t, typictx.IsDestructor(m))
require.True(t, typictx.IsCommandLiner(m))
require.True(t, typictx.IsConfigurer(m))
require.True(t, typiobj.IsProvider(m))
require.True(t, typiobj.IsDestructor(m))
require.True(t, typiobj.IsCommandLiner(m))
require.True(t, typiobj.IsConfigurer(m))
}
10 changes: 6 additions & 4 deletions pkg/typredis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ type redisModule struct {
Name string
}

// Construct dependencies
func (r redisModule) Construct(c *dig.Container) (err error) {
c.Provide(r.loadConfig)
return c.Provide(r.connect)
// Provide dependencies
func (r redisModule) Provide() []interface{} {
return []interface{}{
r.loadConfig,
r.connect,
}
}

// Destruct dependencies
Expand Down
13 changes: 6 additions & 7 deletions pkg/typredis/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typictx"

"github.com/typical-go/typical-rest-server/pkg/module/typredis"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj"
"github.com/typical-go/typical-rest-server/pkg/typredis"
)

func TestModule(t *testing.T) {
m := typredis.Module()
require.True(t, typictx.IsConstructor(m))
require.True(t, typictx.IsDestructor(m))
require.True(t, typictx.IsCommandLiner(m))
require.True(t, typictx.IsConfigurer(m))
require.True(t, typiobj.IsProvider(m))
require.True(t, typiobj.IsDestructor(m))
require.True(t, typiobj.IsCommandLiner(m))
require.True(t, typiobj.IsConfigurer(m))
}
8 changes: 5 additions & 3 deletions pkg/typserver/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ type serverModule struct {
Name string
}

func (s serverModule) Construct(c *dig.Container) (err error) {
c.Provide(s.loadConfig)
return c.Provide(s.Create)
func (s serverModule) Provide() []interface{} {
return []interface{}{
s.loadConfig,
s.Create,
}
}

func (s serverModule) Destruct(c *dig.Container) (err error) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/typserver/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typictx"
"github.com/typical-go/typical-rest-server/pkg/module/typserver"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj"
"github.com/typical-go/typical-rest-server/pkg/typserver"
)

func TestModule(t *testing.T) {
m := typserver.Module()
require.True(t, typictx.IsConstructor(m))
require.True(t, typictx.IsDestructor(m))
require.True(t, typictx.IsConfigurer(m))
require.True(t, typiobj.IsProvider(m))
require.True(t, typiobj.IsDestructor(m))
require.True(t, typiobj.IsConfigurer(m))
}

0 comments on commit e81b7e5

Please sign in to comment.