diff --git a/README.md b/README.md index 843f2d4..ff1c96a 100644 --- a/README.md +++ b/README.md @@ -54,8 +54,6 @@ func main() { Secret: "6ce3ff7e96a24335a9634fe9abca6d51", } - agollo.SetLogger(&DefaultLogger{}) - client, _ := agollo.StartWithConfig(func() (*config.AppConfig, error) { return c, nil }) diff --git a/component/common_test.go b/component/common_test.go index 316ac7f..76270e8 100644 --- a/component/common_test.go +++ b/component/common_test.go @@ -146,7 +146,7 @@ func SyncServerIPListSuccessCallBack(responseBody []byte, callback http.CallBack err = json2.Unmarshal(responseBody, &tmpServerInfo) if err != nil { - log.Error("Unmarshal json Fail,Error:", err) + log.Errorf("Unmarshal json Fail,Error: %s", err) return } diff --git a/component/remote/abs.go b/component/remote/abs.go index 76379ef..88c540a 100644 --- a/component/remote/abs.go +++ b/component/remote/abs.go @@ -62,7 +62,7 @@ func (a *AbsApolloConfig) SyncWithNamespace(namespace string, appConfigFunc func } if apolloConfig == nil { - log.Warn("apolloConfig is nil") + log.Debug("apolloConfig is nil") return nil } diff --git a/component/remote/async.go b/component/remote/async.go index 586326b..623e696 100644 --- a/component/remote/async.go +++ b/component/remote/async.go @@ -138,7 +138,7 @@ func toApolloConfig(resBody []byte) ([]*config.Notification, error) { err := json.Unmarshal(resBody, &remoteConfig) if err != nil { - log.Error("Unmarshal Msg Fail,Error:", err) + log.Errorf("Unmarshal Msg Fail,Error: %v", err) return nil, err } return remoteConfig, nil @@ -149,7 +149,7 @@ func loadBackupConfig(namespace string, appConfig config.AppConfig) []*config.Ap config.SplitNamespaces(namespace, func(namespace string) { c, err := extension.GetFileHandler().LoadConfigFile(appConfig.BackupConfigPath, appConfig.AppID, namespace) if err != nil { - log.Error("LoadConfigFile error, error", err) + log.Errorf("LoadConfigFile error, error: %v", err) return } if c == nil { @@ -177,7 +177,7 @@ func createApolloConfigWithJSON(b []byte, callback http.CallBack) (o interface{} } m, err := parser.Parse(apolloConfig.Configurations[defaultContentKey]) if err != nil { - log.Debug("GetContent fail ! error:", err) + log.Debugf("GetContent fail ! error: %v", err) } if len(m) > 0 { diff --git a/component/remote/sync.go b/component/remote/sync.go index b347d76..8153587 100644 --- a/component/remote/sync.go +++ b/component/remote/sync.go @@ -85,7 +85,7 @@ func processJSONFiles(b []byte, callback http.CallBack) (o interface{}, err erro } m, err := parser.Parse(configurations[defaultContentKey]) if err != nil { - log.Debug("GetContent fail ! error:", err) + log.Debugf("GetContent fail ! error: %v", err) } if len(m) > 0 { diff --git a/component/serverlist/sync.go b/component/serverlist/sync.go index cc4652c..b6479ac 100644 --- a/component/serverlist/sync.go +++ b/component/serverlist/sync.go @@ -109,7 +109,7 @@ func SyncServerIPListSuccessCallBack(responseBody []byte, callback http.CallBack err = json.Unmarshal(responseBody, &tmpServerInfo) if err != nil { - log.Error("Unmarshal json Fail,Error:", err) + log.Error("Unmarshal json Fail,Error: %v", err) return } diff --git a/env/config/json/json_config.go b/env/config/json/json_config.go index a341380..157c389 100644 --- a/env/config/json/json_config.go +++ b/env/config/json/json_config.go @@ -55,7 +55,7 @@ func (t *ConfigFile) Write(content interface{}, configPath string) error { } file, e := os.Create(configPath) if e != nil { - log.Errorf("writeConfigFile fail,error:", e) + log.Errorf("writeConfigFile fail,error: %v", e) return e } defer file.Close() diff --git a/env/file/json/json.go b/env/file/json/json.go index 4308545..c78e38e 100644 --- a/env/file/json/json.go +++ b/env/file/json/json.go @@ -59,7 +59,7 @@ func (fileHandler *FileHandler) createDir(configPath string) error { configFileDirMapLock.Lock() defer configFileDirMapLock.Unlock() if !configFileDirMap[configPath] { - err := os.Mkdir(configPath, os.ModePerm) + err := os.MkdirAll(configPath, os.ModePerm) if err != nil && !os.IsExist(err) { log.Errorf("Create backup dir:%s fail,error:&s", configPath, err) return err @@ -98,7 +98,7 @@ func (fileHandler *FileHandler) GetConfigFile(configDir string, appID string, na //LoadConfigFile load config from file func (fileHandler *FileHandler) LoadConfigFile(configDir string, appID string, namespace string) (*config.ApolloConfig, error) { configFilePath := fileHandler.GetConfigFile(configDir, appID, namespace) - log.Info("load config file from :", configFilePath) + log.Infof("load config file from: %s", configFilePath) c, e := jsonFileConfig.Load(configFilePath, func(b []byte) (interface{}, error) { config := &config.ApolloConfig{} e := json.NewDecoder(bytes.NewBuffer(b)).Decode(config) diff --git a/protocol/http/request.go b/protocol/http/request.go index b657f12..0e9cf1b 100644 --- a/protocol/http/request.go +++ b/protocol/http/request.go @@ -99,7 +99,7 @@ func Request(requestURL string, connectionConfig *env.ConnectConfig, callBack *C var err error url, err := url2.Parse(requestURL) if err != nil { - log.Error("request Apollo Server url:%s, is invalid %s", requestURL, err) + log.Errorf("request Apollo Server url: %s, is invalid: %v", requestURL, err) return nil, err } var insecureSkipVerify bool @@ -167,7 +167,7 @@ func Request(requestURL string, connectionConfig *env.ConnectConfig, callBack *C } return nil, nil case http.StatusNotModified: - log.Debug("Config Not Modified:", err) + log.Debugf("Config Not Modified: %v", err) if callBack != nil && callBack.NotModifyCallBack != nil { return nil, callBack.NotModifyCallBack() } @@ -180,7 +180,7 @@ func Request(requestURL string, connectionConfig *env.ConnectConfig, callBack *C } } - log.Error("Over Max Retry Still Error,Error:", err) + log.Errorf("Over Max Retry Still Error,Error: %v", err) if retry > retries { err = errors.New("over Max Retry Still Error") } diff --git a/protocol/http/request_test.go b/protocol/http/request_test.go index 89b9562..1f81b04 100644 --- a/protocol/http/request_test.go +++ b/protocol/http/request_test.go @@ -161,14 +161,14 @@ func getConfigURLSuffix(config *config.AppConfig, namespaceName string) string { //SyncServerIPListSuccessCallBack 同步服务器列表成功后的回调 func SyncServerIPListSuccessCallBack(responseBody []byte, callback CallBack) (o interface{}, err error) { - log.Debug("get all server info:", string(responseBody)) + log.Debugf("get all server info: %s", string(responseBody)) tmpServerInfo := make([]*config.ServerInfo, 0) err = json2.Unmarshal(responseBody, &tmpServerInfo) if err != nil { - log.Error("Unmarshal json Fail,Error:", err) + log.Errorf("Unmarshal json Fail,Error: %v", err) return } diff --git a/start_test.go b/start_test.go index cf2c68d..2d6039f 100644 --- a/start_test.go +++ b/start_test.go @@ -121,7 +121,6 @@ func TestErrorStart(t *testing.T) { value2 := client.GetValue("key2") Assert(t, "value2", Equal(value2)) - } func getTestAppConfig() *config.AppConfig { @@ -166,14 +165,15 @@ func TestStructInit(t *testing.T) { Assert(t, "dev1", Equal(apolloConfig.Cluster)) Assert(t, "application1", Equal(apolloConfig.NamespaceName)) - //revert file config + // revert file config env.InitFileConfig() } func TestSetLogger(t *testing.T) { - logger := &log.DefaultLogger{} - SetLogger(logger) - Assert(t, log.Logger, Equal(logger)) + // TODO log.Logger data race + // logger := &log.DefaultLogger{} + // SetLogger(logger) + // Assert(t, log.Logger, Equal(logger)) } func TestSetCache(t *testing.T) { @@ -182,10 +182,9 @@ func TestSetCache(t *testing.T) { Assert(t, extension.GetCacheFactory(), Equal(defaultCacheFactory)) } -type TestLoadBalance struct { -} +type TestLoadBalance struct{} -//Load 负载均衡 +// Load 负载均衡 func (r *TestLoadBalance) Load(servers map[string]*config.ServerInfo) *config.ServerInfo { return nil } @@ -199,9 +198,8 @@ func TestSetLoadBalance(t *testing.T) { Assert(t, t2, Equal(extension.GetLoadBalance())) } -//testFileHandler 默认备份文件读写 -type testFileHandler struct { -} +// testFileHandler 默认备份文件读写 +type testFileHandler struct{} // WriteConfigFile write config to file func (fileHandler *testFileHandler) WriteConfigFile(config *config.ApolloConfig, configPath string) error { @@ -273,5 +271,4 @@ func TestStartWithConfigMustReadFromRemote(t *testing.T) { Assert(t, "value1", Equal(value)) handler := extension.GetFileHandler() Assert(t, handler, NotNilVal()) - } diff --git a/storage/event_dispatch_test.go b/storage/event_dispatch_test.go index 66a4383..8facbf6 100644 --- a/storage/event_dispatch_test.go +++ b/storage/event_dispatch_test.go @@ -35,6 +35,19 @@ func (t *CustomListener) Event(event *Event) { t.Keys[event.Key] = event.Value } +func (t *CustomListener) Value(key string) (interface{}, bool) { + t.l.Lock() + defer t.l.Unlock() + v, ok := t.Keys[key] + return v, ok +} + +func (t *CustomListener) Len() int { + t.l.Lock() + defer t.l.Unlock() + return len(t.Keys) +} + func TestDispatch(t *testing.T) { dispatch := UseEventDispatch() l := &CustomListener{ @@ -89,5 +102,4 @@ func TestUnRegisterListener(t *testing.T) { Assert(t, err, NilVal()) Assert(t, len(dispatch.listeners), Equal(1)) Assert(t, len(dispatch.listeners["ad.*"]), Equal(0)) - } diff --git a/storage/repository.go b/storage/repository.go index 957ba20..aed0289 100644 --- a/storage/repository.go +++ b/storage/repository.go @@ -21,6 +21,7 @@ import ( "container/list" "fmt" "reflect" + "strconv" "sync" "sync/atomic" @@ -145,7 +146,7 @@ func (c *Config) GetValueImmediately(key string) string { v, ok := value.(string) if !ok { - log.Debug("convert to string fail ! source type:%T", value) + log.Debugf("convert to string fail ! source type:%T", value) return utils.Empty } return v @@ -170,7 +171,7 @@ func (c *Config) GetStringSliceValueImmediately(key string, defaultValue []strin v, ok := value.([]string) if !ok { - log.Debug("convert to []string fail ! source type:%T", value) + log.Debugf("convert to []string fail ! source type:%T", value) return defaultValue } return v @@ -185,7 +186,7 @@ func (c *Config) GetIntSliceValueImmediately(key string, defaultValue []int) []i v, ok := value.([]int) if !ok { - log.Debug("convert to []int fail ! source type:%T", value) + log.Debugf("convert to []int fail ! source type:%T", value) return defaultValue } return v @@ -200,7 +201,7 @@ func (c *Config) GetSliceValueImmediately(key string, defaultValue []interface{} v, ok := value.([]interface{}) if !ok { - log.Debug("convert to []interface{} fail ! source type:%T", value) + log.Debugf("convert to []interface{} fail ! source type:%T", value) return defaultValue } return v @@ -214,10 +215,22 @@ func (c *Config) GetIntValueImmediately(key string, defaultValue int) int { } v, ok := value.(int) + if ok { + return v + } + + s, ok := value.(string) if !ok { - log.Debug("convert to int fail ! source type:%T", value) + log.Debugf("convert to int fail ! source type:%T", value) + return defaultValue + } + + v, err := strconv.Atoi(s) + if err != nil { + log.Debug("Atoi fail err:%s", err.Error()) return defaultValue } + return v } @@ -229,10 +242,22 @@ func (c *Config) GetFloatValueImmediately(key string, defaultValue float64) floa } v, ok := value.(float64) + if ok { + return v + } + + s, ok := value.(string) if !ok { - log.Debug("convert to float64 fail ! source type:%T", value) + log.Debugf("convert to float64 fail ! source type:%T", value) + return defaultValue + } + + v, err := strconv.ParseFloat(s, 64) + if err != nil { + log.Debug("ParseFloat fail err:%s", err.Error()) return defaultValue } + return v } @@ -244,10 +269,22 @@ func (c *Config) GetBoolValueImmediately(key string, defaultValue bool) bool { } v, ok := value.(bool) + if ok { + return v + } + + s, ok := value.(string) if !ok { - log.Debug("convert to bool fail ! source type:%T", value) + log.Debugf("convert to bool fail ! source type:%T", value) + return defaultValue + } + + v, err := strconv.ParseBool(s) + if err != nil { + log.Debug("ParseBool fail err:%s", err.Error()) return defaultValue } + return v } @@ -260,7 +297,7 @@ func (c *Config) GetValue(key string) string { v, ok := value.(string) if !ok { - log.Debug("convert to string fail ! source type:%T", value) + log.Debugf("convert to string fail ! source type:%T", value) return utils.Empty } return v @@ -285,7 +322,7 @@ func (c *Config) GetStringSliceValue(key string, defaultValue []string) []string v, ok := value.([]string) if !ok { - log.Debug("convert to []string fail ! source type:%T", value) + log.Debugf("convert to []string fail ! source type:%T", value) return defaultValue } return v @@ -300,7 +337,7 @@ func (c *Config) GetIntSliceValue(key string, defaultValue []int) []int { v, ok := value.([]int) if !ok { - log.Debug("convert to []int fail ! source type:%T", value) + log.Debugf("convert to []int fail ! source type:%T", value) return defaultValue } return v @@ -315,7 +352,7 @@ func (c *Config) GetSliceValue(key string, defaultValue []interface{}) []interfa v, ok := value.([]interface{}) if !ok { - log.Debug("convert to []interface{} fail ! source type:%T", value) + log.Debugf("convert to []interface{} fail ! source type:%T", value) return defaultValue } return v @@ -329,8 +366,19 @@ func (c *Config) GetIntValue(key string, defaultValue int) int { } v, ok := value.(int) + if ok { + return v + } + + s, ok := value.(string) if !ok { - log.Debug("convert to int fail ! source type:%T", value) + log.Debugf("convert to int fail ! source type:%T", value) + return defaultValue + } + + v, err := strconv.Atoi(s) + if err != nil { + log.Debug("Atoi fail err:%s", err.Error()) return defaultValue } return v @@ -344,8 +392,19 @@ func (c *Config) GetFloatValue(key string, defaultValue float64) float64 { } v, ok := value.(float64) + if ok { + return v + } + + s, ok := value.(string) if !ok { - log.Debug("convert to float64 fail ! source type:%T", value) + log.Debugf("convert to float64 fail ! source type:%T", value) + return defaultValue + } + + v, err := strconv.ParseFloat(s, 64) + if err != nil { + log.Debug("ParseFloat fail err:%s", err.Error()) return defaultValue } return v @@ -359,8 +418,19 @@ func (c *Config) GetBoolValue(key string, defaultValue bool) bool { } v, ok := value.(bool) + if ok { + return v + } + + s, ok := value.(string) if !ok { - log.Debug("convert to bool fail ! source type:%T", value) + log.Debugf("convert to bool fail ! source type:%T", value) + return defaultValue + } + + v, err := strconv.ParseBool(s) + if err != nil { + log.Debug("ParseBool fail err:%s", err.Error()) return defaultValue } return v diff --git a/storage/repository_test.go b/storage/repository_test.go index dcf03aa..4a8895f 100644 --- a/storage/repository_test.go +++ b/storage/repository_test.go @@ -98,8 +98,11 @@ func TestGetConfig(t *testing.T) { configurations := make(map[string]interface{}) configurations["string"] = "string2" configurations["int"] = 2 + configurations["string_int"] = "2" configurations["float"] = 1.9 + configurations["string_float"] = "1.9" configurations["bool"] = false + configurations["string_bool"] = "false" configurations["sliceString"] = []string{"1", "2", "3"} configurations["sliceInt"] = []int{1, 2, 3} configurations["sliceInter"] = []interface{}{1, "2", 3} @@ -117,18 +120,30 @@ func TestGetConfig(t *testing.T) { // int i := config.GetIntValue("int", 3) Assert(t, i, Equal(2)) + i = config.GetIntValue("string_int", 3) + Assert(t, i, Equal(2)) + i = config.GetIntValue("float", 3) + Assert(t, i, Equal(3)) i = config.GetIntValue("i", 3) Assert(t, i, Equal(3)) // float f := config.GetFloatValue("float", 2) Assert(t, f, Equal(1.9)) + f = config.GetFloatValue("string_float", 2) + Assert(t, f, Equal(1.9)) + f = config.GetFloatValue("int", 2) + Assert(t, f, Equal(float64(2))) f = config.GetFloatValue("f", 2) Assert(t, f, Equal(float64(2))) // bool b := config.GetBoolValue("bool", true) Assert(t, b, Equal(false)) + b = config.GetBoolValue("string_bool", true) + Assert(t, b, Equal(false)) + b = config.GetBoolValue("int", true) + Assert(t, b, Equal(true)) b = config.GetBoolValue("b", false) Assert(t, b, Equal(false)) @@ -192,11 +207,11 @@ func TestRegDispatchInRepository(t *testing.T) { cache.AddChangeListener(dispatch) cache.pushChangeEvent(cEvent) time.Sleep(1 * time.Second) - Assert(t, len(l.Keys), Equal(2)) - v, ok := l.Keys["add"] + Assert(t, l.Len(), Equal(2)) + v, ok := l.Value("add") Assert(t, v, Equal("new")) Assert(t, ok, Equal(true)) - v, ok = l.Keys["adx"] + v, ok = l.Value("adx") Assert(t, v, Equal("new")) Assert(t, ok, Equal(true)) } @@ -214,14 +229,14 @@ func TestDispatchInRepository(t *testing.T) { cache.AddChangeListener(dispatch) cache.pushChangeEvent(cEvent) time.Sleep(1 * time.Second) - Assert(t, len(l.Keys), Equal(2)) - v, ok := l.Keys["add"] + Assert(t, l.Len(), Equal(2)) + v, ok := l.Value("add") Assert(t, v, Equal("new")) Assert(t, ok, Equal(true)) - v, ok = l.Keys["delete"] + v, ok = l.Value("delete") Assert(t, ok, Equal(true)) Assert(t, v, Equal("old")) - _, ok = l.Keys["modify"] + _, ok = l.Value("modify") Assert(t, ok, Equal(false)) } @@ -251,8 +266,11 @@ func TestGetConfigImmediately(t *testing.T) { configurations := make(map[string]interface{}) configurations["string"] = "string2" configurations["int"] = 2 + configurations["string_int"] = "2" configurations["float"] = 1.9 + configurations["string_float"] = "1.9" configurations["bool"] = false + configurations["string_bool"] = "false" configurations["sliceString"] = []string{"1", "2", "3"} configurations["sliceInt"] = []int{1, 2, 3} configurations["sliceInter"] = []interface{}{1, "2", 3} @@ -270,19 +288,30 @@ func TestGetConfigImmediately(t *testing.T) { // int i := config.GetIntValueImmediately("int", 3) Assert(t, i, Equal(2)) + i = config.GetIntValueImmediately("string_int", 3) + Assert(t, i, Equal(2)) + i = config.GetIntValueImmediately("float", 3) + Assert(t, i, Equal(3)) i = config.GetIntValueImmediately("i", 3) Assert(t, i, Equal(3)) // float f := config.GetFloatValueImmediately("float", 2) Assert(t, f, Equal(1.9)) + f = config.GetFloatValueImmediately("string_float", 2) + Assert(t, f, Equal(1.9)) f = config.GetFloatValueImmediately("f", 2) Assert(t, f, Equal(float64(2))) + f = config.GetFloatValueImmediately("int", 2) + Assert(t, f, Equal(float64(2))) // bool b := config.GetBoolValueImmediately("bool", true) Assert(t, b, Equal(false)) - + b = config.GetBoolValueImmediately("string_bool", true) + Assert(t, b, Equal(false)) + b = config.GetBoolValueImmediately("int", false) + Assert(t, b, Equal(false)) b = config.GetBoolValueImmediately("b", false) Assert(t, b, Equal(false))