From 4c659b3ede9a0ef30d3ea18b17e63eacf819cebe Mon Sep 17 00:00:00 2001 From: fanhui Date: Wed, 20 Jan 2021 08:33:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=AD=BB=E9=94=81bug?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E5=A2=9E=E5=8A=A0WriteTo=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=20(#138)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修改Set方法的死锁问题 2. 增加WriteTo方法,获取所有配置项 --- archaius.go | 6 ++++++ archaius_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ source/manager.go | 27 ++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/archaius.go b/archaius.go index a0c22c8..df509cb 100755 --- a/archaius.go +++ b/archaius.go @@ -5,6 +5,7 @@ package archaius import ( "errors" "fmt" + "io" filesource "github.com/go-chassis/go-archaius/source/file" @@ -183,6 +184,11 @@ func UnmarshalConfig(obj interface{}) error { return manager.Unmarshal(obj) } +// WriteTo write the config to writer by yaml +func WriteTo(w io.Writer) error { + return manager.Marshal(w) +} + // GetBool is gives the key value in the form of bool func GetBool(key string, defaultValue bool) bool { b, err := GetValue(key).ToBool() diff --git a/archaius_test.go b/archaius_test.go index 531cc88..0053b9c 100755 --- a/archaius_test.go +++ b/archaius_test.go @@ -1,6 +1,7 @@ package archaius_test import ( + "bytes" "fmt" "io" "os" @@ -227,6 +228,51 @@ infos_ptr: assert.Equal(t, "123", p.MD["name"]) }) } + +func TestMarshalConfig(t *testing.T) { + b := []byte(` +info: + address: a +metadata_str: + key01: "value01" +metadata_int: + key01: 1 +metadata_struct: + key01: {address: "addr03",number: 1230} +metadata_ptr: + key01: {address: "addr05",number: 1232} +str_arr: + - "list01" +int_arr: + - 1 +infos: + - address: "addr01" + users: + - name: "yourname" +infos_ptr: + - number: 123 + users: + - name: "yourname1" + age: 22 +`) + d, _ := os.Getwd() + filename1 := filepath.Join(d, "f4.yaml") + f1, err := os.Create(filename1) + assert.NoError(t, err) + err = archaius.Init(archaius.WithMemorySource()) + assert.NoError(t, err) + defer f1.Close() + defer os.Remove(filename1) + _, err = io.WriteString(f1, string(b)) + assert.NoError(t, err) + err = archaius.AddFile(filename1) + assert.NoError(t, err) + buf := bytes.NewBuffer(nil) + err = archaius.WriteTo(buf) + assert.NoError(t, err) + t.Logf("%s", buf.String()) +} + func TestInitConfigCenter(t *testing.T) { err := archaius.EnableRemoteSource("fake", nil) assert.Error(t, err) diff --git a/source/manager.go b/source/manager.go index 1e5f07c..99b21e8 100755 --- a/source/manager.go +++ b/source/manager.go @@ -24,6 +24,8 @@ package source import ( "errors" "fmt" + "gopkg.in/yaml.v3" + "io" "reflect" "regexp" "sync" @@ -36,6 +38,7 @@ import ( var ( ErrKeyNotExist = errors.New("key does not exist") ErrIgnoreChange = errors.New("ignore key changed") + ErrWriterInvalid = errors.New("writer is invalid") ) //const @@ -82,7 +85,7 @@ func (m *Manager) Cleanup() error { //Set call set of all sources func (m *Manager) Set(k string, v interface{}) error { m.sourceMapMux.RLock() - defer m.sourceMapMux.RLock() + defer m.sourceMapMux.RUnlock() var err error for _, s := range m.Sources { err = s.Set(k, v) @@ -128,6 +131,28 @@ func (m *Manager) Unmarshal(obj interface{}) error { return m.unmarshal(rv, doNotConsiderTag) } +// Marshal function is used to write all configuration by yaml +func (m *Manager) Marshal(w io.Writer) error { + if w == nil { + openlog.Error("invalid writer") + return ErrWriterInvalid + } + allConfig := make(map[string]map[string]interface{}) + for name, source := range m.Sources { + config, err := source.GetConfigurations() + if err != nil { + openlog.Error("get source " + name + " error " + err.Error()) + continue + } + if len(config) == 0 { + continue + } + allConfig[name] = config + } + encode := yaml.NewEncoder(w) + return encode.Encode(allConfig) +} + // AddSource adds a source to configurationManager func (m *Manager) AddSource(source ConfigSource) error { if source == nil || source.GetSourceName() == "" {