You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by la...@apache.org on 2021/09/30 10:30:35 UTC
[dubbo-go] branch 3.0 updated: add properties resolver (#1496)
This is an automated email from the ASF dual-hosted git repository.
laurence pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git
The following commit(s) were added to refs/heads/3.0 by this push:
new a9e9f9a add properties resolver (#1496)
a9e9f9a is described below
commit a9e9f9ac62b719e025e4b733b21b0e3b20a67032
Author: zhaoyunxing <zh...@apache.org>
AuthorDate: Thu Sep 30 18:30:26 2021 +0800
add properties resolver (#1496)
* add properties resolver
* fmt
---
config/config_loader.go | 32 +---------------------
config/config_loader_options.go | 31 +++++++++++++++------
config/config_loader_options_test.go | 29 ++------------------
config/config_resolver.go | 20 ++++++++++----
.../config/properties/application.properties | 13 +++++++++
5 files changed, 55 insertions(+), 70 deletions(-)
diff --git a/config/config_loader.go b/config/config_loader.go
index 6a3f411..2495751 100644
--- a/config/config_loader.go
+++ b/config/config_loader.go
@@ -19,18 +19,12 @@ package config
import (
"errors"
- "fmt"
"reflect"
"strconv"
)
import (
"github.com/knadh/koanf"
- "github.com/knadh/koanf/parsers/json"
- "github.com/knadh/koanf/parsers/toml"
- yaml "github.com/knadh/koanf/parsers/yaml"
- "github.com/knadh/koanf/providers/file"
-
perrors "github.com/pkg/errors"
)
@@ -49,7 +43,7 @@ var (
func Load(opts ...LoaderConfOption) error {
// conf
conf := NewLoaderConf(opts...)
- koan := getKoanf(conf)
+ koan := GetConfigResolver(conf)
if err := koan.UnmarshalWithConf(rootConfig.Prefix(),
rootConfig, koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
return err
@@ -68,30 +62,6 @@ func check() error {
return nil
}
-func getKoanf(conf *loaderConf) *koanf.Koanf {
- var (
- k *koanf.Koanf
- err error
- )
- k = koanf.New(conf.delim)
-
- switch conf.genre {
- case "yaml", "yml":
- err = k.Load(file.Provider(conf.path), yaml.Parser())
- case "json":
- err = k.Load(file.Provider(conf.path), json.Parser())
- case "toml":
- err = k.Load(file.Provider(conf.path), toml.Parser())
- default:
- err = errors.New(fmt.Sprintf("Unsupported %s file type", conf.genre))
- }
-
- if err != nil {
- panic(err)
- }
- return k
-}
-
// registerServiceInstance register service instance
func registerServiceInstance() {
url := selectMetadataServiceExportedURL()
diff --git a/config/config_loader_options.go b/config/config_loader_options.go
index 4de1a66..f98c8a5 100644
--- a/config/config_loader_options.go
+++ b/config/config_loader_options.go
@@ -14,10 +14,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package config
import (
"fmt"
+ "io/ioutil"
"os"
"path/filepath"
"runtime"
@@ -40,8 +42,8 @@ type loaderConf struct {
path string
// loaderConf file delim default .
delim string
- // config
- b []byte
+ // config bytes
+ bytes []byte
}
func NewLoaderConf(opts ...LoaderConfOption) *loaderConf {
@@ -49,16 +51,22 @@ func NewLoaderConf(opts ...LoaderConfOption) *loaderConf {
if configFilePathFromEnv := os.Getenv(constant.CONFIG_FILE_ENV_KEY); configFilePathFromEnv != "" {
configFilePath = configFilePathFromEnv
}
-
+ genre := strings.Split(configFilePath, ".")
conf := &loaderConf{
- genre: "yaml",
- path: configFilePath,
+ genre: genre[len(genre)-1],
+ path: absolutePath(configFilePath),
delim: ".",
}
-
for _, opt := range opts {
opt.apply(conf)
}
+ if len(conf.bytes) <= 0 {
+ bytes, err := ioutil.ReadFile(conf.path)
+ if err != nil {
+ panic(err)
+ }
+ conf.bytes = bytes
+ }
return conf
}
@@ -72,7 +80,7 @@ func (fn loaderConfigFunc) apply(vc *loaderConf) {
fn(vc)
}
-// WithGenre set loaderConf Genre
+// WithGenre set load config genre
func WithGenre(genre string) LoaderConfOption {
return loaderConfigFunc(func(conf *loaderConf) {
g := strings.ToLower(genre)
@@ -83,10 +91,17 @@ func WithGenre(genre string) LoaderConfOption {
})
}
-// WithPath set loaderConf path
+// WithPath set load config path
func WithPath(path string) LoaderConfOption {
return loaderConfigFunc(func(conf *loaderConf) {
conf.path = absolutePath(path)
+ bytes, err := ioutil.ReadFile(path)
+ if err != nil {
+ panic(err)
+ }
+ conf.bytes = bytes
+ genre := strings.Split(path, ".")
+ conf.genre = genre[len(genre)-1]
})
}
diff --git a/config/config_loader_options_test.go b/config/config_loader_options_test.go
index 5f4d771..7704d52 100644
--- a/config/config_loader_options_test.go
+++ b/config/config_loader_options_test.go
@@ -34,30 +34,7 @@ func TestCheckGenre(t *testing.T) {
assert.Nil(t, err)
}
-func TestNewLoaderConf(t *testing.T) {
- conf := NewLoaderConf()
- assert.Equal(t, ".", conf.delim)
- assert.Equal(t, "yaml", conf.genre)
- assert.Equal(t, "../conf/dubbogo.yaml", conf.path)
-}
-
-func TestWithDelim(t *testing.T) {
- conf := NewLoaderConf(WithDelim(":"))
- assert.Equal(t, ":", conf.delim)
- assert.Equal(t, "yaml", conf.genre)
- assert.Equal(t, "../conf/dubbogo.yaml", conf.path)
-}
-
-func TestWithPath(t *testing.T) {
- conf := NewLoaderConf(WithPath("./conf/app.yaml"))
- assert.Equal(t, ".", conf.delim)
- assert.Equal(t, "yaml", conf.genre)
- assert.Equal(t, absolutePath("./conf/app.yaml"), conf.path)
-}
-
-func TestWithGenre(t *testing.T) {
- conf := NewLoaderConf(WithGenre("json"))
- assert.Equal(t, ".", conf.delim)
- assert.Equal(t, "json", conf.genre)
- assert.Equal(t, "../conf/dubbogo.yaml", conf.path)
+func TestFileGenre(t *testing.T) {
+ conf := NewLoaderConf(WithPath("../config/testdata/config/properties/application.properties"))
+ assert.Equal(t, conf.genre, "properties")
}
diff --git a/config/config_resolver.go b/config/config_resolver.go
index 39deffd..a875756 100644
--- a/config/config_resolver.go
+++ b/config/config_resolver.go
@@ -22,7 +22,7 @@ import (
"github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/parsers/yaml"
- "github.com/knadh/koanf/providers/file"
+ "github.com/knadh/koanf/providers/rawbytes"
"github.com/pkg/errors"
)
@@ -36,17 +36,27 @@ func GetConfigResolver(conf *loaderConf) *koanf.Koanf {
k *koanf.Koanf
err error
)
+ if len(conf.genre) <= 0 {
+ conf.genre = "yaml"
+ }
+ if len(conf.delim) <= 0 {
+ conf.delim = "."
+ }
+ bytes := conf.bytes
+ if len(bytes) <= 0 {
+ panic(errors.New("bytes is nil,please set bytes or file path"))
+ }
k = koanf.New(conf.delim)
switch conf.genre {
case "yaml", "yml":
- err = k.Load(file.Provider(conf.path), yaml.Parser())
+ err = k.Load(rawbytes.Provider(bytes), yaml.Parser())
case "json":
- err = k.Load(file.Provider(conf.path), json.Parser())
+ err = k.Load(rawbytes.Provider(bytes), json.Parser())
case "toml":
- err = k.Load(file.Provider(conf.path), toml.Parser())
+ err = k.Load(rawbytes.Provider(bytes), toml.Parser())
case "properties":
- err = k.Load(file.Provider(conf.path), properties.Parser())
+ err = k.Load(rawbytes.Provider(bytes), properties.Parser())
default:
err = errors.Errorf("no support %s file type", conf.genre)
}
diff --git a/config/testdata/config/properties/application.properties b/config/testdata/config/properties/application.properties
new file mode 100644
index 0000000..273dbdd
--- /dev/null
+++ b/config/testdata/config/properties/application.properties
@@ -0,0 +1,13 @@
+dubbo.application.name=dubbo-go
+dubbo.application.module=local
+dubbo.application.version=1.0.0
+dubbo.application.owner=zhaoyunxing
+dubbo.registries.nacos.protocol=nacos
+dubbo.registries.nacos.timeout=5s
+dubbo.registries.nacos.address=127.0.0.1:8848
+dubbo.registries.zk.protocol=zookeeper
+dubbo.registries.zk.timeout=5s
+dubbo.registries.zk.group=dev
+dubbo.registries.zk.address=127.0.0.1:2181
+dubbo.services.HelloService.interface=org.dubbo.service.HelloService
+dubbo.services.HelloService.registry=nacos,zk
\ No newline at end of file