diff --git a/go.mod b/go.mod index 03609c3..01c64bd 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.22.0 require ( github.com/stretchr/testify v1.9.0 - github.com/tinh-tinh/tinhtinh/v2 v2.0.4 + github.com/tinh-tinh/tinhtinh/v2 v2.1.1 ) require ( diff --git a/go.sum b/go.sum index 98e7383..7b75b41 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/tinh-tinh/tinhtinh/v2 v2.0.0 h1:+AUbzU4EHS2HuXA7pk+bswRSURd9duF4vBLRT github.com/tinh-tinh/tinhtinh/v2 v2.0.0/go.mod h1:4nppE7KAIswZKutI9ElMqAD9kyash7aea0Ewowsqj5g= github.com/tinh-tinh/tinhtinh/v2 v2.0.4 h1:Db8oeam9uec5/ZUONhUf7qXwrxJisn1423No76jh9yo= github.com/tinh-tinh/tinhtinh/v2 v2.0.4/go.mod h1:4nppE7KAIswZKutI9ElMqAD9kyash7aea0Ewowsqj5g= +github.com/tinh-tinh/tinhtinh/v2 v2.1.1 h1:+B7U+wkHGAaB52QmRBXk57QBADPjQgL3pqk13cgKs9E= +github.com/tinh-tinh/tinhtinh/v2 v2.1.1/go.mod h1:4nppE7KAIswZKutI9ElMqAD9kyash7aea0Ewowsqj5g= 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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/module.go b/module.go index 4dcbb0c..b1113f4 100644 --- a/module.go +++ b/module.go @@ -30,3 +30,20 @@ func Inject(module core.Module) *Fetch { } return fetch } + +type ConfigFactory func(ref core.RefProvider) *Config + +func RegisterFactory(fnc ConfigFactory) core.Modules { + return func(module core.Module) core.Module { + config := fnc(module) + fetchModule := module.New(core.NewModuleOptions{}) + + fetchModule.NewProvider(core.ProviderOptions{ + Name: FETCH, + Value: Create(config), + }) + fetchModule.Export(FETCH) + + return fetchModule + } +} diff --git a/module_test.go b/module_test.go index b7810fd..b998ba4 100644 --- a/module_test.go +++ b/module_test.go @@ -67,3 +67,21 @@ func Test_AppModule(t *testing.T) { require.Nil(t, err) fmt.Println(string(data)) } + +func Test_Nil(t *testing.T) { + appModule := core.NewModule(core.NewModuleOptions{}) + fetchModule := fetch.Inject(appModule) + require.Nil(t, fetchModule) +} + +func Test_ModuleFactory(t *testing.T) { + appModule := core.NewModule(core.NewModuleOptions{ + Imports: []core.Modules{ + fetch.RegisterFactory(func(ref core.RefProvider) *fetch.Config { + return &fetch.Config{} + }), + }, + }) + fetchModule := fetch.Inject(appModule) + require.NotNil(t, fetchModule) +}