Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] 修复当调用 repository.go 中 GetXXXValue,类型不一致时的报错 #216

Merged
merged 2 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 7 additions & 61 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ package agollo
import (
"container/list"
"errors"
"strconv"

"github.com/apolloconfig/agollo/v4/agcache"
"github.com/apolloconfig/agollo/v4/agcache/memory"
"github.com/apolloconfig/agollo/v4/cluster/roundrobin"
Expand Down Expand Up @@ -195,89 +193,37 @@ func (c *internalClient) GetApolloConfigCache() agcache.CacheInterface {

//GetValue 获取配置
func (c *internalClient) GetValue(key string) string {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method is get default namespace for compatible old version, below as also.

value := c.getConfigValue(key)
if value == nil {
return utils.Empty
}

return value.(string)
return c.GetConfig(storage.GetDefaultNamespace()).GetValue(key)
}

//GetStringValue 获取string配置值
func (c *internalClient) GetStringValue(key string, defaultValue string) string {
value := c.GetValue(key)
if value == utils.Empty {
return defaultValue
}

return value
return c.GetConfig(storage.GetDefaultNamespace()).GetStringValue(key, defaultValue)
}

//GetIntValue 获取int配置值
func (c *internalClient) GetIntValue(key string, defaultValue int) int {
value := c.GetValue(key)

i, err := strconv.Atoi(value)
if err != nil {
log.Debug("convert to int fail!error:", err)
return defaultValue
}

return i
return c.GetConfig(storage.GetDefaultNamespace()).GetIntValue(key, defaultValue)
}

//GetFloatValue 获取float配置值
func (c *internalClient) GetFloatValue(key string, defaultValue float64) float64 {
value := c.GetValue(key)

i, err := strconv.ParseFloat(value, 64)
if err != nil {
log.Debug("convert to float fail!error:", err)
return defaultValue
}

return i
return c.GetConfig(storage.GetDefaultNamespace()).GetFloatValue(key, defaultValue)
}

//GetBoolValue 获取bool 配置值
func (c *internalClient) GetBoolValue(key string, defaultValue bool) bool {
value := c.GetValue(key)

b, err := strconv.ParseBool(value)
if err != nil {
log.Debug("convert to bool fail!error:", err)
return defaultValue
}

return b
return c.GetConfig(storage.GetDefaultNamespace()).GetBoolValue(key, defaultValue)
}

//GetStringSliceValue 获取[]string 配置值
func (c *internalClient) GetStringSliceValue(key string, defaultValue []string) []string {
value := c.getConfigValue(key)

if value == nil {
return defaultValue
}
s, ok := value.([]string)
if !ok {
return defaultValue
}
return s
return c.GetConfig(storage.GetDefaultNamespace()).GetStringSliceValue(key, defaultValue)
}

//GetIntSliceValue 获取[]int 配置值
func (c *internalClient) GetIntSliceValue(key string, defaultValue []int) []int {
value := c.getConfigValue(key)

if value == nil {
return defaultValue
}
s, ok := value.([]int)
if !ok {
return defaultValue
}
return s
return c.GetConfig(storage.GetDefaultNamespace()).GetIntSliceValue(key, defaultValue)
}

func (c *internalClient) getConfigValue(key string) interface{} {
Expand Down
10 changes: 5 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ func createMockApolloConfig(expireTime int) *internalClient {
//string
configs["string"] = "value"
//int
configs["int"] = "1"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong case ,so change it

configs["int"] = 1
//float
configs["float"] = "190.3"
configs["float"] = 190.3
//bool
configs["bool"] = "true"
configs["bool"] = true
//string slice
configs["stringSlice"] = []string{"1", "2"}

Expand Down Expand Up @@ -159,7 +159,7 @@ func TestGetFloatValue(t *testing.T) {
//error type
v = client.GetFloatValue("int", defaultValue)

Assert(t, float64(1), Equal(v))
Assert(t, defaultValue, Equal(v))
}

func TestGetBoolValue(t *testing.T) {
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestConfig_GetFloatValue(t *testing.T) {
//error type
v = config.GetFloatValue("int", defaultValue)

Assert(t, float64(1), Equal(v))
Assert(t, defaultValue, Equal(v))
}

func TestConfig_GetIntValue(t *testing.T) {
Expand Down
78 changes: 51 additions & 27 deletions storage/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"container/list"
"fmt"
"reflect"
"strconv"
"sync"
"sync/atomic"

Expand Down Expand Up @@ -140,7 +139,12 @@ func (c *Config) GetValue(key string) string {
return utils.Empty
}

return value.(string)
v, ok := value.(string)
if !ok {
log.Debug("convert to string fail ! source type:%T", value)
return utils.Empty
}
return v
}

//GetStringValue 获取配置值(string),获取不到则取默认值
Expand All @@ -154,68 +158,88 @@ func (c *Config) GetStringValue(key string, defaultValue string) string {
}

//GetStringSliceValue 获取配置值([]string)
func (c *Config) GetStringSliceValue(key string) []string {
func (c *Config) GetStringSliceValue(key string, defaultValue []string) []string {
value := c.getConfigValue(key)
if value == nil {
return []string{}
return defaultValue
}
return value.([]string)
v, ok := value.([]string)
if !ok {
log.Debug("convert to []string fail ! source type:%T", value)
return defaultValue
}
return v
}

//GetIntSliceValue 获取配置值([]int)
func (c *Config) GetIntSliceValue(key string) []int {
func (c *Config) GetIntSliceValue(key string, defaultValue []int) []int {
value := c.getConfigValue(key)
if value == nil {
return []int{}
return defaultValue
}
return value.([]int)
v, ok := value.([]int)
if !ok {
log.Debug("convert to []int fail ! source type:%T", value)
return defaultValue
}
return v
}

//GetSliceValue 获取配置值([]interface)
func (c *Config) GetSliceValue(key string) []interface{} {
func (c *Config) GetSliceValue(key string, defaultValue []interface{}) []interface{} {
value := c.getConfigValue(key)
if value == nil {
return []interface{}{}
return defaultValue
}
v, ok := value.([]interface{})
if !ok {
log.Debug("convert to []interface{} fail ! source type:%T", value)
return defaultValue
}
return value.([]interface{})
return v
}

//GetIntValue 获取配置值(int),获取不到则取默认值
func (c *Config) GetIntValue(key string, defaultValue int) int {
value := c.GetValue(key)
value := c.getConfigValue(key)

i, err := strconv.Atoi(value)
if err != nil {
log.Debug("convert to int fail!error:", err)
if value == nil {
return defaultValue
}
v, ok := value.(int)
if !ok {
log.Debug("convert to int fail ! source type:%T", value)
return defaultValue
}
return i
return v
}

//GetFloatValue 获取配置值(float),获取不到则取默认值
func (c *Config) GetFloatValue(key string, defaultValue float64) float64 {
value := c.GetValue(key)
value := c.getConfigValue(key)

i, err := strconv.ParseFloat(value, 64)
if err != nil {
log.Debug("convert to float fail!error:", err)
if value == nil {
return defaultValue
}

return i
v, ok := value.(float64)
if !ok {
log.Debug("convert to float64 fail ! source type:%T", value)
return defaultValue
}
return v
}

//GetBoolValue 获取配置值(bool),获取不到则取默认值
func (c *Config) GetBoolValue(key string, defaultValue bool) bool {
value := c.GetValue(key)
value := c.getConfigValue(key)

b, err := strconv.ParseBool(value)
if err != nil {
log.Debug("convert to bool fail!error:", err)
v, ok := value.(bool)
if !ok {
log.Debug("convert to bool fail ! source type:%T", value)
return defaultValue
}

return b
return v
}

//UpdateApolloConfig 根据config server返回的内容更新内存
Expand Down
20 changes: 10 additions & 10 deletions storage/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func TestUpdateApolloConfigNull(t *testing.T) {

configurations := make(map[string]interface{})
configurations["string"] = "string"
configurations["int"] = "1"
configurations["float"] = "1"
configurations["bool"] = "true"
configurations["int"] = 1
configurations["float"] = 1.9
configurations["bool"] = true
configurations["slice"] = []int{1, 2}

apolloConfig := &config.ApolloConfig{}
Expand Down Expand Up @@ -98,9 +98,9 @@ func TestGetDefaultNamespace(t *testing.T) {
func TestGetConfig(t *testing.T) {
configurations := make(map[string]interface{})
configurations["string"] = "string2"
configurations["int"] = "2"
configurations["float"] = "1"
configurations["bool"] = "false"
configurations["int"] = 2
configurations["float"] = 1.9
configurations["bool"] = false
configurations["sliceString"] = []string{"1", "2", "3"}
configurations["sliceInt"] = []int{1, 2, 3}
configurations["sliceInter"] = []interface{}{1, "2", 3}
Expand All @@ -123,7 +123,7 @@ func TestGetConfig(t *testing.T) {

//float
f := config.GetFloatValue("float", 2)
Assert(t, f, Equal(float64(1)))
Assert(t, f, Equal(1.9))
f = config.GetFloatValue("f", 2)
Assert(t, f, Equal(float64(2)))

Expand All @@ -134,13 +134,13 @@ func TestGetConfig(t *testing.T) {
b = config.GetBoolValue("b", false)
Assert(t, b, Equal(false))

slice := config.GetStringSliceValue("sliceString")
slice := config.GetStringSliceValue("sliceString", []string{})
Assert(t, slice, Equal([]string{"1", "2", "3"}))

sliceInt := config.GetIntSliceValue("sliceInt")
sliceInt := config.GetIntSliceValue("sliceInt", []int{})
Assert(t, sliceInt, Equal([]int{1, 2, 3}))

sliceInter := config.GetSliceValue("sliceInter")
sliceInter := config.GetSliceValue("sliceInter", []interface{}{})
Assert(t, sliceInter, Equal([]interface{}{1, "2", 3}))

//content
Expand Down