Skip to content

Commit

Permalink
fix: #1558: Set root config to global ptr in Init() function. (#1564)
Browse files Browse the repository at this point in the history
* fix: #1558

* fix: change the api to start the app by config-api
  • Loading branch information
LaurenceLiZhixin authored Nov 7, 2021
1 parent 27aaaa9 commit ff58c2a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
13 changes: 9 additions & 4 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ var (
func Load(opts ...LoaderConfOption) error {
// conf
conf := NewLoaderConf(opts...)
koan := GetConfigResolver(conf)
if err := koan.UnmarshalWithConf(rootConfig.Prefix(),
rootConfig, koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
return err
if conf.rc == nil {
koan := GetConfigResolver(conf)
if err := koan.UnmarshalWithConf(rootConfig.Prefix(),
rootConfig, koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
return err
}
} else {
rootConfig = conf.rc
}

if err := rootConfig.Init(); err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions config/config_loader_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type loaderConf struct {
delim string
// config bytes
bytes []byte
// user provide rootConfig built by config api
rc *RootConfig
}

func NewLoaderConf(opts ...LoaderConfOption) *loaderConf {
Expand All @@ -60,6 +62,9 @@ func NewLoaderConf(opts ...LoaderConfOption) *loaderConf {
for _, opt := range opts {
opt.apply(conf)
}
if conf.rc != nil {
return conf
}
if len(conf.bytes) <= 0 {
bytes, err := ioutil.ReadFile(conf.path)
if err != nil {
Expand Down Expand Up @@ -105,6 +110,12 @@ func WithPath(path string) LoaderConfOption {
})
}

func WithRootConfig(rc *RootConfig) LoaderConfOption {
return loaderConfigFunc(func(conf *loaderConf) {
conf.rc = rc
})
}

func WithDelim(delim string) LoaderConfOption {
return loaderConfigFunc(func(conf *loaderConf) {
conf.delim = delim
Expand Down
6 changes: 6 additions & 0 deletions config/config_loader_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ func TestFileGenre(t *testing.T) {
conf := NewLoaderConf(WithPath("../config/testdata/config/properties/application.properties"))
assert.Equal(t, conf.genre, "properties")
}

func TestRootConfig(t *testing.T) {
rc := NewRootConfigBuilder().SetApplication(NewApplicationConfigBuilder().SetName("test-app").Build()).Build()
conf := NewLoaderConf(WithRootConfig(rc))
assert.Equal(t, conf.rc.Application.Name, "test-app")
}
2 changes: 2 additions & 0 deletions config/root_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ func registerPOJO() {
hessian.RegisterPOJO(&common.URL{})
}

// Init is to start dubbo-go framework, load local configuration, or read configuration from config-center if necessary.
// It's deprecated for user to call rootConfig.Init() manually, try config.Load(config.WithRootConfig(rootConfig)) instead.
func (rc *RootConfig) Init() error {
registerPOJO()
if err := rc.Logger.Init(); err != nil { // init default logger
Expand Down

0 comments on commit ff58c2a

Please sign in to comment.