Skip to content

Commit

Permalink
format codes, update some test
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 13, 2019
1 parent 4c13a80 commit a066315
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (c *Config) Readonly() {
*************************************************************/

// AddDriver set a decoder and encoder driver for a format.
func AddDriver(driver Driver) {dc.AddDriver(driver) }
func AddDriver(driver Driver) { dc.AddDriver(driver) }

// AddDriver set a decoder and encoder driver for a format.
func (c *Config) AddDriver(driver Driver) {
Expand Down
12 changes: 4 additions & 8 deletions config_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,20 @@ func (c *Config) Exists(key string, findByPath ...bool) (ok bool) {

// check slice index
if err != nil || len(typeData) < i {
ok = false
return
return false
}
case []string: // is array(is from Set)
i, err := strconv.Atoi(k)
if err != nil || len(typeData) < i {
ok = false
return
return false
}
case []interface{}: // is array(load from file)
i, err := strconv.Atoi(k)
if err != nil || len(typeData) < i {
ok = false
return
return false
}
default: // error
ok = false
return
return false
}
}
return true
Expand Down
19 changes: 13 additions & 6 deletions config_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func TestGet(t *testing.T) {
st.True(Exists("age"))
st.True(Exists("map1.key"))
st.True(Exists("arr1.1"))
st.False(Exists("arr1.1", false))
st.False(Exists("not-exist.sub"))
st.False(Exists(""))
st.False(Exists("not-exist"))

// get value
Expand Down Expand Up @@ -93,6 +96,7 @@ func TestGet(t *testing.T) {

// Ints: get int arr
iarr := Ints("name")
st.False(Exists("name.1"))
st.Empty(iarr)

iarr = Ints("notExist")
Expand Down Expand Up @@ -126,7 +130,7 @@ func TestGet(t *testing.T) {
err = Set("intMap0", map[string]int{"a": 1, "b": 2})
st.Nil(err)
imp = IntMap("intMap0")

st.NotEmpty(imp)
st.Equal(1, imp["a"])

Expand Down Expand Up @@ -210,7 +214,7 @@ func TestGet(t *testing.T) {
func TestInt(t *testing.T) {
st := assert.New(t)
ClearAll()
_= LoadStrings(JSON, jsonStr)
_ = LoadStrings(JSON, jsonStr)

st.True(Exists("age"))

Expand All @@ -235,7 +239,7 @@ func TestInt(t *testing.T) {
func TestInt64(t *testing.T) {
st := assert.New(t)
ClearAll()
_= LoadStrings(JSON, jsonStr)
_ = LoadStrings(JSON, jsonStr)

// get int64
iv64 := Int64("age")
Expand All @@ -261,7 +265,7 @@ func TestInt64(t *testing.T) {
func TestFloat(t *testing.T) {
st := assert.New(t)
ClearAll()
_= LoadStrings(JSON, jsonStr)
_ = LoadStrings(JSON, jsonStr)
c := Default()

// get float
Expand All @@ -276,6 +280,9 @@ func TestFloat(t *testing.T) {
flt = c.Float("notExists", 0)
st.Equal(float64(0), flt)

flt = c.Float("notExists", 10)
st.Equal(float64(10), flt)

flt = Float("flVal", 0)
st.Equal(23.45, flt)

Expand All @@ -285,7 +292,7 @@ func TestFloat(t *testing.T) {
func TestString(t *testing.T) {
st := assert.New(t)
ClearAll()
_= LoadStrings(JSON, jsonStr)
_ = LoadStrings(JSON, jsonStr)

// get string
val := String("arr1")
Expand All @@ -309,7 +316,7 @@ func TestString(t *testing.T) {
func TestBool(t *testing.T) {
st := assert.New(t)
ClearAll()
_= LoadStrings(JSON, jsonStr)
_ = LoadStrings(JSON, jsonStr)

// get bool
val := Get("debug")
Expand Down
18 changes: 9 additions & 9 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ func Example() {
// fmt.Printf("config data: \n %#v\n", Data())
fmt.Print("get config example:\n")

name:= String("name")
name := String("name")
fmt.Printf("- get string\n val: %v\n", name)

arr1:= Strings("arr1")
arr1 := Strings("arr1")
fmt.Printf("- get array\n val: %#v\n", arr1)

val0:= String("arr1.0")
val0 := String("arr1.0")
fmt.Printf("- get sub-value by path 'arr.index'\n val: %#v\n", val0)

map1:= StringMap("map1")
map1 := StringMap("map1")
fmt.Printf("- get map\n val: %#v\n", map1)

val0= String("map1.key")
val0 = String("map1.key")
fmt.Printf("- get sub-value by path 'map.key'\n val: %#v\n", val0)

// can parse env name(ParseEnv: true)
Expand All @@ -84,7 +84,7 @@ func Example() {

// set value
_ = Set("name", "new name")
name= String("name")
name = String("name")
fmt.Printf("- set string\n val: %v\n", name)

// if you want export config data
Expand Down Expand Up @@ -425,13 +425,13 @@ func TestOptions(t *testing.T) {
err := c.LoadStrings(JSON, jsonStr)
st.Nil(err)

str:= c.String("name")
str := c.String("name")
st.Equal("app", str)

str= c.String("envKey")
str = c.String("envKey")
st.NotContains(str, "${")

str= c.String("invalidEnvKey")
str = c.String("invalidEnvKey")
st.Contains(str, "${")

str = c.String("envKey1")
Expand Down
2 changes: 1 addition & 1 deletion ini/ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Example() {
fmt.Printf("get env 'envKey1' val: %s\n", config.String("envKey1", ""))

// set value
_= config.Set("name", "new name")
_ = config.Set("name", "new name")
name = config.String("name")
fmt.Printf("set string\n - val: %v\n", name)
}
Expand Down
2 changes: 1 addition & 1 deletion json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Example() {
fmt.Printf("get env 'envKey1' val: %s\n", config.String("envKey1", ""))

// set value
_= config.Set("name", "new name")
_ = config.Set("name", "new name")
name = config.String("name")
fmt.Printf("set string\n - val: %v\n", name)

Expand Down
1 change: 0 additions & 1 deletion json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,3 @@ comments
s,
)
}

2 changes: 1 addition & 1 deletion toml/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Example() {
}

// load from string
_= config.LoadSources(config.Toml, []byte(tomlStr))
_ = config.LoadSources(config.Toml, []byte(tomlStr))

// fmt.Printf("config data: \n %#v\n", Data())
fmt.Print("get config example:\n")
Expand Down

0 comments on commit a066315

Please sign in to comment.