diff --git a/contrib/config/apollo/apollo.go b/contrib/config/apollo/apollo.go index 3d312b1b6..9a3a67819 100644 --- a/contrib/config/apollo/apollo.go +++ b/contrib/config/apollo/apollo.go @@ -150,6 +150,11 @@ func format(ns string) string { return suffix } +func isOriginConfig(ns string) bool { + f := format(ns) + return f == yaml || f == yml || f == json +} + func (e *apollo) load() []*config.KeyValue { var ( kvs = make([]*config.KeyValue, 0) @@ -166,8 +171,7 @@ func (e *apollo) load() []*config.KeyValue { kvs = append(kvs, kv) continue } - if strings.Contains(ns, ".") && !strings.HasSuffix(ns, "."+properties) && - (format(ns) == yaml || format(ns) == yml || format(ns) == json) { + if strings.Contains(ns, ".") && !strings.HasSuffix(ns, "."+properties) && isOriginConfig(ns) { kv, err := e.getOriginConfig(ns) if err != nil { log.Errorf("apollo get config failed,err:%v", err) diff --git a/contrib/config/apollo/watcher.go b/contrib/config/apollo/watcher.go index ad8424209..bbed38523 100644 --- a/contrib/config/apollo/watcher.go +++ b/contrib/config/apollo/watcher.go @@ -24,21 +24,19 @@ type customChangeListener struct { } func (c *customChangeListener) onChange(namespace string, changes map[string]*storage.ConfigChange) []*config.KeyValue { - kv := make([]*config.KeyValue, 0, 2) - if strings.Contains(namespace, ".") && !strings.HasSuffix(namespace, "."+properties) && - (format(namespace) == yaml || format(namespace) == yml || format(namespace) == json) { + if strings.Contains(namespace, ".") && !strings.HasSuffix(namespace, "."+properties) && isOriginConfig(namespace) { value, err := c.apollo.client.GetConfigCache(namespace).Get("content") if err != nil { log.Warnw("apollo get config failed", "err", err) return nil } - kv = append(kv, &config.KeyValue{ - Key: namespace, - Value: []byte(value.(string)), - Format: format(namespace), - }) - - return kv + return []*config.KeyValue{ + { + Key: namespace, + Value: []byte(value.(string)), + Format: format(namespace), + }, + } } next := make(map[string]interface{}) @@ -48,19 +46,18 @@ func (c *customChangeListener) onChange(namespace string, changes map[string]*st } f := format(namespace) - codec := encoding.GetCodec(f) - val, err := codec.Marshal(next) + val, err := encoding.GetCodec(f).Marshal(next) if err != nil { log.Warnf("apollo could not handle namespace %s: %v", namespace, err) return nil } - kv = append(kv, &config.KeyValue{ - Key: namespace, - Value: val, - Format: f, - }) - - return kv + return []*config.KeyValue{ + { + Key: namespace, + Value: val, + Format: f, + }, + } } func (c *customChangeListener) OnChange(changeEvent *storage.ChangeEvent) {