Skip to content

Commit

Permalink
add:support file extension (#1626)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyunxing92 authored Dec 2, 2021
1 parent a8fba70 commit f7f1581
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
15 changes: 6 additions & 9 deletions config/config_center_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
"github.com/creasty/defaults"

"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/rawbytes"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -61,6 +59,9 @@ type CenterConfig struct {
AppID string `default:"dubbo" yaml:"app-id" json:"app-id,omitempty"`
Timeout string `default:"10s" yaml:"timeout" json:"timeout,omitempty"`
Params map[string]string `yaml:"params" json:"parameters,omitempty"`

//FileExtension the suffix of config dataId, also the file extension of config content
FileExtension string `default:"yaml" yaml:"file-extension" json:"file-extension" `
}

// Prefix dubbo.config-center
Expand Down Expand Up @@ -146,15 +147,11 @@ func startConfigCenter(rc *RootConfig) error {
"Please check if your config-center config is correct.", cc)
return nil
}
koan := koanf.New(".")
if err = koan.Load(rawbytes.Provider([]byte(strConf)), yaml.Parser()); err != nil {
return err
}
if err = koan.UnmarshalWithConf(rc.Prefix(),
rc, koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
config := NewLoaderConf(WithDelim("."), WithGenre(cc.FileExtension), WithBytes([]byte(strConf)))
koan := GetConfigResolver(config)
if err = koan.UnmarshalWithConf(rc.Prefix(), rc, koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
return err
}

return nil
}

Expand Down
12 changes: 9 additions & 3 deletions config/config_loader_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package config

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -122,6 +121,13 @@ func WithDelim(delim string) LoaderConfOption {
})
}

// WithBytes set load config bytes
func WithBytes(bytes []byte) LoaderConfOption {
return loaderConfigFunc(func(conf *loaderConf) {
conf.bytes = bytes
})
}

// absolutePath get absolut path
func absolutePath(inPath string) string {

Expand Down Expand Up @@ -155,11 +161,11 @@ func userHomeDir() string {

// checkGenre check Genre
func checkGenre(genre string) error {
genres := []string{"json", "toml", "yaml", "yml"}
genres := []string{"json", "toml", "yaml", "yml", "properties"}
sort.Strings(genres)
idx := sort.SearchStrings(genres, genre)
if genres[idx] != genre {
return errors.New(fmt.Sprintf("no support %s", genre))
return errors.Errorf("no support file extension: %s", genre)
}
return nil
}
11 changes: 11 additions & 0 deletions config/config_loader_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ func TestRootConfig(t *testing.T) {
conf := NewLoaderConf(WithRootConfig(rc))
assert.Equal(t, conf.rc.Application.Name, "test-app")
}

func TestNewLoaderConf_WithBytes(t *testing.T) {
str := `dubbo.application.name=dubbo-go
dubbo.application.module=local
dubbo.services.HelloService.registry=nacos,zk`

conf := NewLoaderConf(WithBytes([]byte(str)), WithGenre("properties"))

assert.NotNil(t, conf)
assert.NotNil(t, conf.bytes)
}

0 comments on commit f7f1581

Please sign in to comment.