From 102092009e7f57499107033bbbe2eac4d2096715 Mon Sep 17 00:00:00 2001 From: baozhecheng Date: Wed, 10 Aug 2022 13:43:36 +0800 Subject: [PATCH] fix --- contrib/config/apollo/apollo.go | 13 ++++++++++--- contrib/config/apollo/watcher.go | 7 ++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/contrib/config/apollo/apollo.go b/contrib/config/apollo/apollo.go index 2901e5af2..fef411d69 100644 --- a/contrib/config/apollo/apollo.go +++ b/contrib/config/apollo/apollo.go @@ -16,6 +16,13 @@ type apollo struct { opt *options } +const ( + yaml = "yaml" + json = "json" + yml = "yml" + properties = "properties" +) + // Option is apollo option type Option func(*options) @@ -109,7 +116,7 @@ func NewSource(opts ...Option) config.Source { func format(ns string) string { arr := strings.Split(ns, ".") - if len(arr) <= 1 || arr[len(arr)-1] == "properties" { + if len(arr) <= 1 || arr[len(arr)-1] == { return "json" } return arr[len(arr)-1] @@ -120,8 +127,8 @@ func (e *apollo) load() []*config.KeyValue { namespaces := strings.Split(e.opt.namespace, ",") for _, ns := range namespaces { - if strings.Contains(ns, ".") && !strings.Contains(ns, "properties") && - (format(ns) == "yaml" || format(ns) == "yaml" || format(ns) == "json") { + if strings.Contains(ns, ".") && !strings.Contains(ns, properties) && + (format(ns) == yaml || format(ns) == yml || format(ns) == json) { value, err := e.client.GetConfigCache(ns).Get("content") 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 b31dec7a4..670139a26 100644 --- a/contrib/config/apollo/watcher.go +++ b/contrib/config/apollo/watcher.go @@ -2,9 +2,10 @@ package apollo import ( "context" - "github.com/go-kratos/kratos/v2/encoding" "strings" + "github.com/go-kratos/kratos/v2/encoding" + "github.com/go-kratos/kratos/v2/config" "github.com/go-kratos/kratos/v2/log" @@ -25,8 +26,8 @@ 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.Contains(namespace, "properties") && - (format(namespace) == "yaml" || format(namespace) == "yaml" || format(namespace) == "json") { + if strings.Contains(namespace, ".") && !strings.Contains(namespace, properties) && + (format(namespace) == yaml || format(namespace) == yml || format(namespace) == json) { value, err := c.apollo.client.GetConfigCache(namespace).Get("content") if err != nil { log.Warnw("apollo get config failed", "err", err)