From 8a72be85e4c55ac5b1ff99fc4f1d1f14b8c8ae78 Mon Sep 17 00:00:00 2001 From: "vito.he" <hxmhlt@163.com> Date: Thu, 12 Sep 2019 17:59:59 +0800 Subject: [PATCH] Add:config center service_config & reference_config id --- config/base_config.go | 45 +++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/config/base_config.go b/config/base_config.go index e51d8b955..264eeda3c 100644 --- a/config/base_config.go +++ b/config/base_config.go @@ -109,14 +109,10 @@ func (c *BaseConfig) prepareEnvironment() error { return nil } -func getKeyPrefix(val reflect.Value, id reflect.Value) []string { +func getKeyPrefix(val reflect.Value) []string { var ( prefix string - idStr string ) - if id.Kind() == reflect.String { - idStr = id.Interface().(string) - } if val.CanAddr() { prefix = val.Addr().MethodByName("Prefix").Call(nil)[0].String() @@ -126,11 +122,9 @@ func getKeyPrefix(val reflect.Value, id reflect.Value) []string { var retPrefixs []string for _, pfx := range strings.Split(prefix, "|") { - if idStr != "" { - retPrefixs = append(retPrefixs, pfx+idStr+".") - } else { - retPrefixs = append(retPrefixs, pfx) - } + + retPrefixs = append(retPrefixs, pfx) + } return retPrefixs @@ -150,14 +144,37 @@ func setFieldValue(val reflect.Value, id reflect.Value, config *config.InmemoryC f := val.Field(i) if f.IsValid() { setBaseValue := func(f reflect.Value) { - var ok bool - var value string - prefixs := getKeyPrefix(val, id) + + var ( + ok bool + value string + idStr string + ) + + prefixs := getKeyPrefix(val) + + if id.Kind() == reflect.String { + idStr = id.Interface().(string) + } + for _, pfx := range prefixs { - ok, value = config.GetProperty(pfx + key) + + if len(pfx) > 0 { + if len(idStr) > 0 { + ok, value = config.GetProperty(pfx + idStr + "." + key) + } + if len(value) == 0 || !ok { + ok, value = config.GetProperty(pfx + key) + } + + } else { + ok, value = config.GetProperty(key) + } + if ok { break } + } if ok { switch f.Kind() { -- GitLab