From bbfb3db8ed815857674581d5b98bbd2686a4d64a Mon Sep 17 00:00:00 2001 From: xgfone Date: Tue, 19 Sep 2023 23:51:16 +0800 Subject: [PATCH] use defaults.Exit instead of os.Exit --- .github/workflows/go.yml | 4 +-- config.go | 2 -- config_test.go | 2 +- go.mod | 2 +- go.sum | 4 +-- opt_group_test.go | 34 ++++++++++----------- opt_proxy_test.go | 66 ++++++++++++++++++++-------------------- snapshot_test.go | 9 +++--- source_flag.go | 6 ++-- source_test.go | 37 +++++++++++----------- 10 files changed, 85 insertions(+), 81 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 27b5b29..3cdb058 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -4,7 +4,7 @@ env: GO111MODULE: on jobs: build: - runs-on: ubuntu-22.04 + runs-on: ubuntu-latest name: Go ${{ matrix.go }} strategy: matrix: @@ -12,7 +12,7 @@ jobs: - '1.18' - '1.19' - '1.20' - - '1.21.x' + - '1.21' steps: - uses: actions/checkout@v3 - name: Setup Go diff --git a/config.go b/config.go index 3ac13bd..4b6a018 100644 --- a/config.go +++ b/config.go @@ -394,8 +394,6 @@ func (c *Config) flatmap(prefix string, results, maps map[string]interface{}) { results[prefix+key] = value } } - - return } func (c *Config) flatMap(maps map[string]interface{}) map[string]interface{} { diff --git a/config_test.go b/config_test.go index a1c1e27..3d6d662 100644 --- a/config_test.go +++ b/config_test.go @@ -109,7 +109,7 @@ func TestConfig(t *testing.T) { t.Errorf("durations option value expects '%v', but got '%v'", []time.Duration{}, v) } - LoadMap(map[string]interface{}{ + _ = LoadMap(map[string]interface{}{ "bool": true, "string": "abc", "int": 111, diff --git a/go.mod b/go.mod index 197744f..dc80644 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/xgfone/gconf/v6 require ( github.com/fsnotify/fsnotify v1.6.0 github.com/xgfone/go-cast v0.8.1 - github.com/xgfone/go-defaults v0.7.0 + github.com/xgfone/go-defaults v0.13.0 gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index 5cd04c5..288fed4 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4 github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= github.com/xgfone/go-cast v0.8.1 h1:x80Qu+XCUyQoFvCo2j+CFRiKiJydF11jeAJRzRtGY9U= github.com/xgfone/go-cast v0.8.1/go.mod h1:aHO9rXhmN4IZ4d1UG35+6WEVbg5yyISynFQJCVltrsk= -github.com/xgfone/go-defaults v0.7.0 h1:SzpA+1/Z8RxWsLBbwvRhJ/rkkH0GtDAiZHsEVDLnk2M= -github.com/xgfone/go-defaults v0.7.0/go.mod h1:zwcnD8escc6E5QrCNKRLQddb4/hcUnUOM20kUeki18Y= +github.com/xgfone/go-defaults v0.13.0 h1:aJX/RJSI8yN6Xxn1b1NlFQyClwION2DM5X1NDz3KQ0U= +github.com/xgfone/go-defaults v0.13.0/go.mod h1:4qxXP2vvK8n2csVwYmFbhbQAISq5s/2zYZE9CKYj/bw= golang.org/x/sys v0.0.0-20220908164124-27713097b956 h1:XeJjHH1KiLpKGb6lvMiksZ9l0fVUh+AmGcm0nOMEBOY= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/opt_group_test.go b/opt_group_test.go index 39713d2..52031e9 100644 --- a/opt_group_test.go +++ b/opt_group_test.go @@ -84,23 +84,23 @@ func TestOptGroup(t *testing.T) { } } - g1.Set("bool", "true") - g1.Set("string", "abc") - g1.Set("int", 111) - g1.Set("int32", 222) - g1.Set("int64", 333) - g1.Set("uint", "444") - g1.Set("uint32", "555") - g1.Set("uint64", "666") - g1.Set("float64", 777) - g1.Set("duration", "1s") - g1.Set("time", "2021-08-25T21:50:51+08:00") - g1.Set("ints", "1,2,3") - g1.Set("uints", "4,5,6") - g1.Set("float64s", []float64{7, 8, 9}) - g1.Set("strings", []string{"a", "b", "c"}) - g1.Set("durations", []time.Duration{time.Second}) - g2.Set("string", "xyz") + _ = g1.Set("bool", "true") + _ = g1.Set("string", "abc") + _ = g1.Set("int", 111) + _ = g1.Set("int32", 222) + _ = g1.Set("int64", 333) + _ = g1.Set("uint", "444") + _ = g1.Set("uint32", "555") + _ = g1.Set("uint64", "666") + _ = g1.Set("float64", 777) + _ = g1.Set("duration", "1s") + _ = g1.Set("time", "2021-08-25T21:50:51+08:00") + _ = g1.Set("ints", "1,2,3") + _ = g1.Set("uints", "4,5,6") + _ = g1.Set("float64s", []float64{7, 8, 9}) + _ = g1.Set("strings", []string{"a", "b", "c"}) + _ = g1.Set("durations", []time.Duration{time.Second}) + _ = g2.Set("string", "xyz") if v := g1.GetBool("bool"); v != true { t.Errorf("bool option value expect '%v', but got '%v'", true, v) diff --git a/opt_proxy_test.go b/opt_proxy_test.go index 53b50ac..01dda4b 100644 --- a/opt_proxy_test.go +++ b/opt_proxy_test.go @@ -94,22 +94,22 @@ func TestOptProxy(t *testing.T) { t.Errorf("option value expect '%v', but got '%v'", []time.Duration{time.Second}, v) } - boolopt.Set(true) - intopt.Set(11) - int32opt.Set(22) - int64opt.Set(33) - uintopt.Set(44) - uint32opt.Set(55) - uint64opt.Set(66) - float64opt.Set(77) - stringopt.Set("abc") - durationopt.Set(time.Hour) - timeopt.Set(time.Unix(1629878851, 0)) - intsopt.Set("1,2,3") - uintsopt.Set("4,5,6") - float64sopt.Set("7,8,9") - stringsopt.Set("x,y,z") - durationsopt.Set("1s,1h") + _ = boolopt.Set(true) + _ = intopt.Set(11) + _ = int32opt.Set(22) + _ = int64opt.Set(33) + _ = uintopt.Set(44) + _ = uint32opt.Set(55) + _ = uint64opt.Set(66) + _ = float64opt.Set(77) + _ = stringopt.Set("abc") + _ = durationopt.Set(time.Hour) + _ = timeopt.Set(time.Unix(1629878851, 0)) + _ = intsopt.Set("1,2,3") + _ = uintsopt.Set("4,5,6") + _ = float64sopt.Set("7,8,9") + _ = stringsopt.Set("x,y,z") + _ = durationsopt.Set("1s,1h") if v := boolopt.Get(); v != true { t.Errorf("option value expect '%v', but got '%v'", true, v) @@ -183,22 +183,22 @@ func TestOptGroupProxy(t *testing.T) { stringsopt := group.NewStringSlice("strings", []string{"a"}, "strings") durationsopt := group.NewDurationSlice("durations", []time.Duration{time.Second}, "durations") - boolopt.Set(true) - intopt.Set(11) - int32opt.Set(22) - int64opt.Set(33) - uintopt.Set(44) - uint32opt.Set(55) - uint64opt.Set(66) - float64opt.Set(77) - stringopt.Set("abc") - durationopt.Set(time.Hour) - timeopt.Set(time.Unix(1629878851, 0)) - intsopt.Set("1,2,3") - uintsopt.Set("4,5,6") - float64sopt.Set("7,8,9") - stringsopt.Set("x,y,z") - durationsopt.Set("1s,1h") + _ = boolopt.Set(true) + _ = intopt.Set(11) + _ = int32opt.Set(22) + _ = int64opt.Set(33) + _ = uintopt.Set(44) + _ = uint32opt.Set(55) + _ = uint64opt.Set(66) + _ = float64opt.Set(77) + _ = stringopt.Set("abc") + _ = durationopt.Set(time.Hour) + _ = timeopt.Set(time.Unix(1629878851, 0)) + _ = intsopt.Set("1,2,3") + _ = uintsopt.Set("4,5,6") + _ = float64sopt.Set("7,8,9") + _ = stringsopt.Set("x,y,z") + _ = durationsopt.Set("1s,1h") if v := config.Get(intopt.Name()); v != 11 { t.Errorf("option value expect '%v', but got '%v'", 11, v) @@ -288,7 +288,7 @@ func TestOptProxyOnUpdate(t *testing.T) { fmt.Fprintf(buf, "%s: %v -> %v", opt.Name(), old, new) }) - opt.Set("xyz") + _ = opt.Set("xyz") expect := `opt: abc -> xyz` if s := buf.String(); s != expect { t.Errorf("expect '%s', but got '%s'", expect, s) diff --git a/snapshot_test.go b/snapshot_test.go index c6ee00d..d9a6f00 100644 --- a/snapshot_test.go +++ b/snapshot_test.go @@ -23,8 +23,8 @@ func TestConfig_Snapshot(t *testing.T) { IntOpt("opt2", ""), ) - config.Set("opt1", "a") - config.Set("opt2", 1) + _ = config.Set("opt1", "a") + _ = config.Set("opt2", 1) gen, snaps := config.Snapshot() if gen != 2 { t.Errorf("expect %d generation, but got %d", 2, gen) @@ -46,8 +46,9 @@ func TestConfig_Snapshot(t *testing.T) { } } } - config.Set("opt1", "b") - config.Set("opt2", 2) + + _ = config.Set("opt1", "b") + _ = config.Set("opt2", 2) gen, snaps = config.Snapshot() if gen != 4 { t.Errorf("expect %d generation, but got %d", 4, gen) diff --git a/source_flag.go b/source_flag.go index fff56e8..1859cb9 100644 --- a/source_flag.go +++ b/source_flag.go @@ -23,6 +23,8 @@ import ( "strconv" "strings" "time" + + "github.com/xgfone/go-defaults" ) // PrintFlagUsage prints the flag usage instead of the default. @@ -72,7 +74,7 @@ func PrintFlagUsage(flagSet *flag.FlagSet) { // // They are equivalent. func AddOptFlag(c *Config, flagSet ...*flag.FlagSet) { - addAndParseOptFlag(false, c, flagSet...) + _ = addAndParseOptFlag(false, c, flagSet...) } // AddAndParseOptFlag is the same as AddOptFlag, but parses the CLI arguments. @@ -141,7 +143,7 @@ func addAndParseOptFlag(parse bool, c *Config, flagSet ...*flag.FlagSet) error { if flag := flagset.Lookup(vName); flag != nil { if yes, _ := strconv.ParseBool(flag.Value.String()); yes { fmt.Println(value) - os.Exit(0) + defaults.Exit(0) } } } diff --git a/source_test.go b/source_test.go index a4bf514..6acb1ff 100644 --- a/source_test.go +++ b/source_test.go @@ -39,7 +39,7 @@ func TestNewEnvSource(t *testing.T) { conf.Group("group1").RegisterOpts(StrOpt("opt2", "")) conf.Group("group1").Group("group2").RegisterOpts(Float64Opt("opt3", "")) - conf.LoadSource(NewEnvSource(""), true) + _ = conf.LoadSource(NewEnvSource(""), true) if v := conf.GetInt("opt1"); v != 111 { t.Errorf("expect '%d', but got '%d'", 111, v) } else if v := conf.Group("group1").GetString("opt2"); v != "abc" { @@ -48,7 +48,7 @@ func TestNewEnvSource(t *testing.T) { t.Errorf("expect '%d', but got '%f'", 222, v) } - conf.LoadSource(NewEnvSource("test"), true) + _ = conf.LoadSource(NewEnvSource("test"), true) if v := conf.GetInt("opt1"); v != 333 { t.Errorf("expect '%d', but got '%d'", 333, v) } else if v := conf.Group("group1").GetString("opt2"); v != "efg" { @@ -65,7 +65,7 @@ func TestNewFileSource_INI(t *testing.T) { if err != nil { t.Error(err) } else { - file.Write([]byte(` + _, _ = file.Write([]byte(` [DEFAULT] opt1 = 1 opt2 = 0 @@ -88,7 +88,7 @@ func TestNewFileSource_INI(t *testing.T) { conf.RegisterOpts(IntOpt("opt1", "")) conf.Group("group1").RegisterOpts(BoolOpt("opt2", "")) conf.Group("group1.group2").RegisterOpts(Float64Opt("opt3", "")) - conf.LoadSource(NewFileSource(filename)) + _ = conf.LoadSource(NewFileSource(filename)) // Check the config if v := conf.GetInt("opt1"); v != 1 { @@ -107,7 +107,7 @@ func TestNewFileSource_JSON(t *testing.T) { if err != nil { t.Error(err) } else { - file.Write([]byte(`{ + _, _ = file.Write([]byte(`{ "opt1": 1, "opt2": false, "opt3": 1, @@ -131,7 +131,7 @@ func TestNewFileSource_JSON(t *testing.T) { conf.RegisterOpts(IntOpt("opt1", "")) conf.Group("group1").RegisterOpts(BoolOpt("opt2", "")) conf.Group("group1").Group("group2").RegisterOpts(Float64Opt("opt3", "")) - conf.LoadSource(NewFileSource(filename)) + _ = conf.LoadSource(NewFileSource(filename)) // Check the config if v := conf.GetInt("opt1"); v != 1 { @@ -147,21 +147,24 @@ func TestNewURLSource(t *testing.T) { first := true // Start the http server - go http.ListenAndServe("127.0.0.1:12345", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if first { - first = false - w.Header().Set("Content-Type", "application/json; charset=utf-8") - w.Write([]byte(`{"opt": 123}`)) - } else { - w.Header().Set("Content-Type", "application/json; charset=utf-8") - w.Write([]byte(`{"opt": 456}`)) - } - })) + go func() { + _ = http.ListenAndServe("127.0.0.1:12345", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if first { + first = false + w.Header().Set("Content-Type", "application/json; charset=utf-8") + _, _ = w.Write([]byte(`{"opt": 123}`)) + } else { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + _, _ = w.Write([]byte(`{"opt": 456}`)) + } + })) + }() + time.Sleep(time.Millisecond * 50) // Wait that the http server finishes to start. conf := New() conf.RegisterOpts(IntOpt("opt", "")) - conf.LoadAndWatchSource(NewURLSource("http://127.0.0.1:12345/", time.Millisecond*100)) + _ = conf.LoadAndWatchSource(NewURLSource("http://127.0.0.1:12345/", time.Millisecond*100)) defer conf.Stop() if v := conf.GetInt("opt"); v != 123 {