Skip to content

Commit

Permalink
修改死锁bug以及增加WriteTo接口 (#138)
Browse files Browse the repository at this point in the history
1. 修改Set方法的死锁问题
2. 增加WriteTo方法,获取所有配置项
  • Loading branch information
ichiro999 authored Jan 20, 2021
1 parent c7ddc0e commit 4c659b3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
6 changes: 6 additions & 0 deletions archaius.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package archaius
import (
"errors"
"fmt"
"io"

filesource "github.com/go-chassis/go-archaius/source/file"

Expand Down Expand Up @@ -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()
Expand Down
46 changes: 46 additions & 0 deletions archaius_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package archaius_test

import (
"bytes"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 26 additions & 1 deletion source/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ package source
import (
"errors"
"fmt"
"gopkg.in/yaml.v3"
"io"
"reflect"
"regexp"
"sync"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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() == "" {
Expand Down

0 comments on commit 4c659b3

Please sign in to comment.