remove config API's context parameter

This commit is contained in:
Ming Deng
2020-10-13 22:32:41 +08:00
parent 568626cd57
commit 2572094a8d
22 changed files with 457 additions and 497 deletions

View File

@@ -15,7 +15,6 @@
package json
import (
"context"
"fmt"
"os"
"testing"
@@ -52,7 +51,7 @@ func TestJsonStartsWithArray(t *testing.T) {
if err != nil {
t.Fatal(err)
}
rootArray, err := jsonconf.DIY(nil, "rootArray")
rootArray, err := jsonconf.DIY("rootArray")
if err != nil {
t.Error("array does not exist as element")
}
@@ -158,19 +157,19 @@ func TestJson(t *testing.T) {
var value interface{}
switch v.(type) {
case int:
value, err = jsonconf.Int(nil, k)
value, err = jsonconf.Int(k)
case int64:
value, err = jsonconf.Int64(nil, k)
value, err = jsonconf.Int64(k)
case float64:
value, err = jsonconf.Float(nil, k)
value, err = jsonconf.Float(k)
case bool:
value, err = jsonconf.Bool(nil, k)
value, err = jsonconf.Bool(k)
case []string:
value, err = jsonconf.Strings(nil, k)
value, err = jsonconf.Strings(k)
case string:
value, err = jsonconf.String(nil, k)
value, err = jsonconf.String(k)
default:
value, err = jsonconf.DIY(nil, k)
value, err = jsonconf.DIY(k)
}
if err != nil {
t.Fatalf("get key %q value fatal,%v err %s", k, v, err)
@@ -179,16 +178,16 @@ func TestJson(t *testing.T) {
}
}
if err = jsonconf.Set(nil, "name", "astaxie"); err != nil {
if err = jsonconf.Set("name", "astaxie"); err != nil {
t.Fatal(err)
}
res, _ := jsonconf.String(nil, "name")
res, _ := jsonconf.String("name")
if res != "astaxie" {
t.Fatal("get name error")
}
if db, err := jsonconf.DIY(nil, "database"); err != nil {
if db, err := jsonconf.DIY("database"); err != nil {
t.Fatal(err)
} else if m, ok := db.(map[string]interface{}); !ok {
t.Log(db)
@@ -199,46 +198,46 @@ func TestJson(t *testing.T) {
}
}
if _, err := jsonconf.Int(nil, "unknown"); err == nil {
if _, err := jsonconf.Int("unknown"); err == nil {
t.Error("unknown keys should return an error when expecting an Int")
}
if _, err := jsonconf.Int64(nil, "unknown"); err == nil {
if _, err := jsonconf.Int64("unknown"); err == nil {
t.Error("unknown keys should return an error when expecting an Int64")
}
if _, err := jsonconf.Float(nil, "unknown"); err == nil {
if _, err := jsonconf.Float("unknown"); err == nil {
t.Error("unknown keys should return an error when expecting a Float")
}
if _, err := jsonconf.DIY(nil, "unknown"); err == nil {
if _, err := jsonconf.DIY("unknown"); err == nil {
t.Error("unknown keys should return an error when expecting an interface{}")
}
if val, _ := jsonconf.String(nil, "unknown"); val != "" {
if val, _ := jsonconf.String("unknown"); val != "" {
t.Error("unknown keys should return an empty string when expecting a String")
}
if _, err := jsonconf.Bool(nil, "unknown"); err == nil {
if _, err := jsonconf.Bool("unknown"); err == nil {
t.Error("unknown keys should return an error when expecting a Bool")
}
if !jsonconf.DefaultBool(nil, "unknown", true) {
if !jsonconf.DefaultBool("unknown", true) {
t.Error("unknown keys with default value wrong")
}
sub, err := jsonconf.Sub(context.Background(), "database")
sub, err := jsonconf.Sub("database")
assert.Nil(t, err)
assert.NotNil(t, sub)
sub, err = sub.Sub(context.Background(), "conns")
sub, err = sub.Sub("conns")
assert.Nil(t, err)
maxCon, _ := sub.Int(context.Background(), "maxconnection")
maxCon, _ := sub.Int("maxconnection")
assert.Equal(t, 12, maxCon)
dbCfg := &DatabaseConfig{}
err = sub.Unmarshaler(context.Background(), "", dbCfg)
err = sub.Unmarshaler("", dbCfg)
assert.Nil(t, err)
assert.Equal(t, 12, dbCfg.MaxConnection)
assert.True(t, dbCfg.Autoconnect)