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

Mod: fixed some issues in config folder that reported by sonar-qube #634

Merged
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
100 changes: 50 additions & 50 deletions config/base_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,20 @@ func getKeyPrefix(val reflect.Value) []string {
var (
prefix string
)

configPrefixMethod := "Prefix"
if val.CanAddr() {
prefix = val.Addr().MethodByName("Prefix").Call(nil)[0].String()
prefix = val.Addr().MethodByName(configPrefixMethod).Call(nil)[0].String()
} else {
prefix = val.MethodByName("Prefix").Call(nil)[0].String()
prefix = val.MethodByName(configPrefixMethod).Call(nil)[0].String()
}
var retPrefixs []string
var retPrefixes []string

for _, pfx := range strings.Split(prefix, "|") {

retPrefixs = append(retPrefixs, pfx)
retPrefixes = append(retPrefixes, pfx)

}
return retPrefixs
return retPrefixes

}

Expand All @@ -164,13 +164,13 @@ func setFieldValue(val reflect.Value, id reflect.Value, config *config.InmemoryC
idStr string
)

prefixs := getKeyPrefix(val)
prefixes := getKeyPrefix(val)

if id.Kind() == reflect.String {
idStr = id.Interface().(string)
}

for _, pfx := range prefixs {
for _, pfx := range prefixes {

if len(pfx) > 0 {
if len(idStr) > 0 {
Expand All @@ -190,40 +190,39 @@ func setFieldValue(val reflect.Value, id reflect.Value, config *config.InmemoryC

}
if ok {
errMsg := func(structName string, fieldName string, errorDetails error) {
logger.Errorf("Dynamic change the configuration in struct {%v} field {%v} error ,error message is {%v}",
structName, fieldName, errorDetails)
}
switch f.Kind() {
case reflect.Int64:
x, err := strconv.Atoi(value)
if err != nil {
logger.Errorf("Dynamic change the configuration in struct {%v} field {%v} error ,error message is {%v}",
val.Type().Name(), val.Type().Field(i).Name, err)
errMsg(val.Type().Name(), val.Type().Field(i).Name, err)
} else {
if !f.OverflowInt(int64(x)) {
f.SetInt(int64(x))
} else {
logger.Errorf("Dynamic change the configuration in struct {%v} field {%v} error ,error message is {%v}",
val.Type().Name(), val.Type().Field(i).Name, perrors.Errorf("the int64 value {%v} from config center is overflow", int64(x)))
errMsg(val.Type().Name(), val.Type().Field(i).Name, perrors.Errorf("the int64 value {%v} from config center is overflow", int64(x)))
}
}
case reflect.String:
f.SetString(value)
case reflect.Bool:
x, err := strconv.ParseBool(value)
if err != nil {
logger.Errorf("Dynamic change the configuration in struct {%v} field {%v} error ,error message is {%v}",
val.Type().Name(), val.Type().Field(i).Name, err)
errMsg(val.Type().Name(), val.Type().Field(i).Name, err)
}
f.SetBool(x)
case reflect.Float64:
x, err := strconv.ParseFloat(value, 64)
if err != nil {
logger.Errorf("Dynamic change the configuration in struct {%v} field {%v} error ,error message is {%v}",
val.Type().Name(), val.Type().Field(i).Name, err)
errMsg(val.Type().Name(), val.Type().Field(i).Name, err)
} else {
if !f.OverflowFloat(x) {
f.SetFloat(x)
} else {
logger.Errorf("Dynamic change the configuration in struct {%v} field {%v} error ,error message is {%v}",
val.Type().Name(), val.Type().Field(i).Name, perrors.Errorf("the float64 value {%v} from config center is overflow", x))
errMsg(val.Type().Name(), val.Type().Field(i).Name, perrors.Errorf("the float64 value {%v} from config center is overflow", x))
}
}
default:
Expand Down Expand Up @@ -328,39 +327,40 @@ func (c *BaseConfig) SetFatherConfig(fatherConfig interface{}) {
}

func initializeStruct(t reflect.Type, v reflect.Value) {
if v.Kind() == reflect.Struct {
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
ft := t.Field(i)

if ft.Tag.Get("property") != "" {
switch ft.Type.Kind() {
case reflect.Map:
if f.IsNil() {
f.Set(reflect.MakeMap(ft.Type))
}
case reflect.Slice:
if f.IsNil() {
f.Set(reflect.MakeSlice(ft.Type, 0, 0))
}
case reflect.Chan:
if f.IsNil() {
f.Set(reflect.MakeChan(ft.Type, 0))
}
case reflect.Struct:
if f.IsNil() {
initializeStruct(ft.Type, f)
}
case reflect.Ptr:
if f.IsNil() {
fv := reflect.New(ft.Type.Elem())
initializeStruct(ft.Type.Elem(), fv.Elem())
f.Set(fv)
}
default:
}
}
if v.Kind() != reflect.Struct {
return
}
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
ft := t.Field(i)

if ft.Tag.Get("property") == "" {
continue
}
switch ft.Type.Kind() {
case reflect.Map:
if f.IsNil() {
f.Set(reflect.MakeMap(ft.Type))
}
case reflect.Slice:
if f.IsNil() {
f.Set(reflect.MakeSlice(ft.Type, 0, 0))
}
case reflect.Chan:
if f.IsNil() {
f.Set(reflect.MakeChan(ft.Type, 0))
}
case reflect.Struct:
if f.IsNil() {
initializeStruct(ft.Type, f)
}
case reflect.Ptr:
if f.IsNil() {
fv := reflect.New(ft.Type.Elem())
initializeStruct(ft.Type.Elem(), fv.Elem())
f.Set(fv)
}
default:
}
}
}
Loading