Skip to content

Commit

Permalink
use defaults.Exit instead of os.Exit
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Sep 19, 2023
1 parent b72278d commit bbfb3db
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 81 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ env:
GO111MODULE: on
jobs:
build:
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
name: Go ${{ matrix.go }}
strategy:
matrix:
go:
- '1.18'
- '1.19'
- '1.20'
- '1.21.x'
- '1.21'
steps:
- uses: actions/checkout@v3
- name: Setup Go
Expand Down
2 changes: 0 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
34 changes: 17 additions & 17 deletions opt_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
66 changes: 33 additions & 33 deletions opt_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions source_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"strconv"
"strings"
"time"

"github.com/xgfone/go-defaults"
)

// PrintFlagUsage prints the flag usage instead of the default.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
37 changes: 20 additions & 17 deletions source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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" {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit bbfb3db

Please sign in to comment.