From 7342800caabea559f2f0dc17c2006d70b09e2c51 Mon Sep 17 00:00:00 2001 From: baozhecheng Date: Wed, 10 Aug 2022 19:45:14 +0800 Subject: [PATCH] fix --- contrib/config/apollo/apollo.go | 95 +++++++++++++++++++++++---------- contrib/config/apollo/parser.go | 12 ----- 2 files changed, 67 insertions(+), 40 deletions(-) diff --git a/contrib/config/apollo/apollo.go b/contrib/config/apollo/apollo.go index f5a674a67..5f868bab9 100644 --- a/contrib/config/apollo/apollo.go +++ b/contrib/config/apollo/apollo.go @@ -4,11 +4,13 @@ import ( "strings" "github.com/go-kratos/kratos/v2/config" - "github.com/go-kratos/kratos/v2/encoding" "github.com/go-kratos/kratos/v2/log" "github.com/apolloconfig/agollo/v4" + "github.com/apolloconfig/agollo/v4/constant" apolloConfig "github.com/apolloconfig/agollo/v4/env/config" + "github.com/apolloconfig/agollo/v4/extension" + "github.com/go-kratos/kratos/v2/encoding" ) type apollo struct { @@ -34,6 +36,7 @@ type options struct { namespace string isBackupConfig bool backupPath string + originConfig bool } // WithAppID with apollo config app id @@ -92,6 +95,16 @@ func WithBackupPath(backupPath string) Option { } } +// WithOriginalConfig use the original configuration file without parse processing +func WithOriginalConfig() Option { + return func(o *options) { + extension.AddFormatParser(constant.JSON, &jsonExtParser{}) + extension.AddFormatParser(constant.YAML, &yamlExtParser{}) + extension.AddFormatParser(constant.YML, &yamlExtParser{}) + o.originConfig = true + } +} + func NewSource(opts ...Option) config.Source { op := options{} for _, o := range opts { @@ -123,45 +136,71 @@ func format(ns string) string { } func (e *apollo) load() []*config.KeyValue { - kv := make([]*config.KeyValue, 0) + kvs := make([]*config.KeyValue, 0) namespaces := strings.Split(e.opt.namespace, ",") for _, ns := range namespaces { - 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 !e.opt.originConfig { + kv, err := e.getConfig(ns) if err != nil { log.Errorf("apollo get config failed,err:%v", err) continue } - // serialize the namespace content KeyValue into bytes. - kv = append(kv, &config.KeyValue{ - Key: ns, - Value: []byte(value.(string)), - Format: format(ns), - }) + kvs = append(kvs, kv) continue } - next := map[string]interface{}{} - e.client.GetConfigCache(ns).Range(func(key, value interface{}) bool { - // all values are out properties format - resolve(genKey(ns, key.(string)), value, next) - return true - }) - f := format(ns) - codec := encoding.GetCodec(f) - val, err := codec.Marshal(next) - if err != nil { - log.Warnf("apollo could not handle namespace %s: %v", ns, err) + if strings.Contains(ns, ".") && !strings.Contains(ns, properties) && + (format(ns) == yaml || format(ns) == yml || format(ns) == json) { + kv, err := e.getOriginConfig(ns) + if err != nil { + log.Errorf("apollo get config failed,err:%v", err) + continue + } + kvs = append(kvs, kv) continue + } else { + kv, err := e.getConfig(ns) + if err != nil { + log.Errorf("apollo get config failed,err:%v", err) + continue + } + kvs = append(kvs, kv) } - kv = append(kv, &config.KeyValue{ - Key: ns, - Value: val, - Format: f, - }) } - return kv + return kvs +} + +func (e *apollo) getConfig(ns string) (*config.KeyValue, error) { + next := map[string]interface{}{} + e.client.GetConfigCache(ns).Range(func(key, value interface{}) bool { + // all values are out properties format + resolve(genKey(ns, key.(string)), value, next) + return true + }) + f := format(ns) + codec := encoding.GetCodec(f) + val, err := codec.Marshal(next) + if err != nil { + return nil, err + } + return &config.KeyValue{ + Key: ns, + Value: val, + Format: f, + }, nil +} + +func (e apollo) getOriginConfig(ns string) (*config.KeyValue, error) { + value, err := e.client.GetConfigCache(ns).Get("content") + if err != nil { + return nil, err + } + // serialize the namespace content KeyValue into bytes. + return &config.KeyValue{ + Key: ns, + Value: []byte(value.(string)), + Format: format(ns), + }, nil } func (e *apollo) Load() (kv []*config.KeyValue, err error) { diff --git a/contrib/config/apollo/parser.go b/contrib/config/apollo/parser.go index eaee845e9..2b1abb991 100644 --- a/contrib/config/apollo/parser.go +++ b/contrib/config/apollo/parser.go @@ -1,10 +1,5 @@ package apollo -import ( - "github.com/apolloconfig/agollo/v4/constant" - "github.com/apolloconfig/agollo/v4/extension" -) - type jsonExtParser struct{} func (parser jsonExtParser) Parse(configContent interface{}) (map[string]interface{}, error) { @@ -16,10 +11,3 @@ type yamlExtParser struct{} func (parser yamlExtParser) Parse(configContent interface{}) (map[string]interface{}, error) { return map[string]interface{}{"content": configContent}, nil } - -func init() { - // add json/yaml/yml format - extension.AddFormatParser(constant.JSON, &jsonExtParser{}) - extension.AddFormatParser(constant.YAML, &yamlExtParser{}) - extension.AddFormatParser(constant.YML, &yamlExtParser{}) -}