Skip to content

Commit f35ea09

Browse files
committed
change conf README
1 parent 5f8d783 commit f35ea09

File tree

3 files changed

+68
-44
lines changed

3 files changed

+68
-44
lines changed

pkg/conf/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ Parsing yaml, json, toml configuration files to go struct.
99
```go
1010
import "github.com/zhufuyi/sponge/pkg/conf"
1111

12-
// Way 1: No listening profile
12+
// Way 1: No listening configuration file
1313
config := &App{}
1414
err := conf.Parse("test.yml", config)
1515

16-
// Way 2: Enable listening profile
16+
// Way 2: Enable listening configuration file
1717
config := &App{}
18-
fs := []func(){
18+
reloads := []func(){
1919
func() {
20-
fmt.Println("Listening for updates to the configuration file")
20+
fmt.Println("close and reconnect mysql")
21+
fmt.Println("close and reconnect redis")
2122
},
2223
}
23-
err := conf.Parse("test.yml", config, fs...)
24+
err := conf.Parse("test.yml", config, reloads...)
2425
```

pkg/conf/parse.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
// Parse configuration files to struct, including yaml, toml, json, etc., and turn on listening for configuration file changes if fs is not empty
18-
func Parse(configFile string, obj interface{}, fs ...func()) error {
18+
func Parse(configFile string, obj interface{}, reloads ...func()) error {
1919
confFileAbs, err := filepath.Abs(configFile)
2020
if err != nil {
2121
return err
@@ -38,23 +38,45 @@ func Parse(configFile string, obj interface{}, fs ...func()) error {
3838
return err
3939
}
4040

41-
if len(fs) > 0 {
42-
watchConfig(obj, fs...)
41+
if len(reloads) > 0 {
42+
watchConfig(obj, reloads...)
43+
}
44+
45+
return nil
46+
}
47+
48+
// ParseConfigData parse data to struct
49+
func ParseConfigData(data []byte, format string, obj interface{}, reloads ...func()) error {
50+
viper.SetConfigType(format)
51+
err := viper.ReadConfig(bytes.NewBuffer(data))
52+
if err != nil {
53+
return err
54+
}
55+
56+
err = viper.Unmarshal(obj)
57+
if err != nil {
58+
return err
59+
}
60+
61+
if len(reloads) > 0 {
62+
watchConfig(obj, reloads...)
4363
}
4464

4565
return nil
4666
}
4767

4868
// listening for profile updates
49-
func watchConfig(obj interface{}, fs ...func()) {
69+
func watchConfig(obj interface{}, reloads ...func()) {
5070
viper.WatchConfig()
71+
72+
// Note: OnConfigChange is called twice on Windows
5173
viper.OnConfigChange(func(e fsnotify.Event) {
5274
err := viper.Unmarshal(obj)
5375
if err != nil {
5476
fmt.Println("viper.Unmarshal error: ", err)
5577
} else {
56-
for _, f := range fs {
57-
f()
78+
for _, reload := range reloads {
79+
reload()
5880
}
5981
}
6082
})
@@ -123,13 +145,3 @@ func replaceDSN(str string) string {
123145

124146
return fmt.Sprintf("%s******%s", data[:start+1], data[end:])
125147
}
126-
127-
// ParseConfigData parse data to struct
128-
func ParseConfigData(data []byte, format string, obj interface{}) error {
129-
viper.SetConfigType(format)
130-
err := viper.ReadConfig(bytes.NewBuffer(data))
131-
if err != nil {
132-
return err
133-
}
134-
return viper.Unmarshal(obj)
135-
}

pkg/conf/parse_test.go

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,6 @@ import (
99

1010
var c = make(map[string]interface{})
1111

12-
func init() {
13-
// result error test
14-
_ = Parse("test.yml", nil)
15-
// not found error test
16-
_ = Parse("notfound.yml", &c)
17-
18-
err := Parse("test.yml", &c)
19-
if err != nil {
20-
panic(err)
21-
}
22-
}
23-
2412
func TestShow(t *testing.T) {
2513
t.Log(Show(c))
2614
t.Log(Show(make(chan string)))
@@ -45,21 +33,18 @@ func Test_hideSensitiveFields(t *testing.T) {
4533
fmt.Printf(hideSensitiveFields(str))
4634
}
4735

48-
// test listening for profile updates
49-
func TestWatch(t *testing.T) {
50-
time.Sleep(time.Second)
36+
// test listening for configuration file updates
37+
func TestParse(t *testing.T) {
5138
conf := make(map[string]interface{})
5239

53-
fs := []func(){
40+
reloads := []func(){
5441
func() {
55-
fmt.Println("update field 1")
56-
},
57-
func() {
58-
fmt.Println("update field 2")
42+
fmt.Println("close and reconnect mysql")
43+
fmt.Println("close and reconnect redis")
5944
},
6045
}
6146

62-
err := Parse("test.yml", &conf, fs...)
47+
err := Parse("test.yml", &conf, reloads...)
6348
if err != nil {
6449
t.Error(err)
6550
return
@@ -75,13 +60,39 @@ func TestWatch(t *testing.T) {
7560
time.Sleep(time.Millisecond * 100)
7661
}
7762

63+
func TestParseErr(t *testing.T) {
64+
// result error test
65+
err := Parse("test.yml", nil)
66+
t.Log(err)
67+
68+
// not found error test
69+
err = Parse("notfound.yml", &c)
70+
t.Log(err)
71+
}
72+
7873
func TestParseConfigData(t *testing.T) {
7974
conf := make(map[string]interface{})
75+
76+
reloads := []func(){
77+
func() {
78+
fmt.Println("close and reconnect mysql")
79+
fmt.Println("close and reconnect redis")
80+
},
81+
}
82+
8083
data, _ := os.ReadFile("test.yml")
81-
err := ParseConfigData(data, "yaml", &conf)
84+
err := ParseConfigData(data, "yaml", &conf, reloads...)
8285
if err != nil {
8386
t.Error(err)
8487
return
8588
}
86-
t.Log(conf)
89+
90+
time.Sleep(time.Second)
91+
content, _ := os.ReadFile("test.yml")
92+
contentChange := append(content, byte('#'))
93+
time.Sleep(time.Millisecond * 100)
94+
_ = os.WriteFile("test.yml", contentChange, 0666) // change file
95+
time.Sleep(time.Millisecond * 100)
96+
_ = os.WriteFile("test.yml", content, 0666) // recovery documents
97+
time.Sleep(time.Millisecond * 100)
8798
}

0 commit comments

Comments
 (0)