From a52b17c2686c2aeb12ce318693c450bf46ba13a6 Mon Sep 17 00:00:00 2001 From: youn <279844216@qq.com> Date: Wed, 23 Feb 2022 14:30:21 +0800 Subject: [PATCH] fix: add yaml / yml parser (#1808) Co-authored-by: yang.liu14 --- contrib/config/apollo/go.mod | 1 + contrib/config/apollo/go.sum | 2 -- contrib/config/apollo/json_parser.go | 25 ----------------- contrib/config/apollo/parser.go | 40 ++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 27 deletions(-) delete mode 100644 contrib/config/apollo/json_parser.go create mode 100644 contrib/config/apollo/parser.go diff --git a/contrib/config/apollo/go.mod b/contrib/config/apollo/go.mod index e96d8f572..3631b634c 100644 --- a/contrib/config/apollo/go.mod +++ b/contrib/config/apollo/go.mod @@ -12,6 +12,7 @@ require ( github.com/spf13/viper v1.10.1 // indirect golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect gopkg.in/ini.v1 v1.66.3 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) replace github.com/go-kratos/kratos/v2 => ../../../ diff --git a/contrib/config/apollo/go.sum b/contrib/config/apollo/go.sum index 185a878ae..df612a3d9 100644 --- a/contrib/config/apollo/go.sum +++ b/contrib/config/apollo/go.sum @@ -59,8 +59,6 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= -github.com/apolloconfig/agollo/v4 v4.0.8 h1:SY23bGjLJX58OMVnussD9MKwg/XE9zZgzrTg3WrjFdU= -github.com/apolloconfig/agollo/v4 v4.0.8/go.mod h1:SuvTjtg0p4UlSzSbik+ibLRr6oR1xRsfy65QzP3GEAs= github.com/apolloconfig/agollo/v4 v4.1.0 h1:aZmrnSr/l0/CD5+txdgF1bIiKP1I5m0EH0WkHqAVhHk= github.com/apolloconfig/agollo/v4 v4.1.0/go.mod h1:SuvTjtg0p4UlSzSbik+ibLRr6oR1xRsfy65QzP3GEAs= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= diff --git a/contrib/config/apollo/json_parser.go b/contrib/config/apollo/json_parser.go deleted file mode 100644 index 827c7b29b..000000000 --- a/contrib/config/apollo/json_parser.go +++ /dev/null @@ -1,25 +0,0 @@ -package apollo - -import ( - "encoding/json" - - "github.com/apolloconfig/agollo/v4/constant" - "github.com/apolloconfig/agollo/v4/extension" -) - -type jsonExtParser struct{} - -func (j jsonExtParser) Parse(configContent interface{}) (map[string]interface{}, error) { - v, ok := configContent.(string) - if !ok { - return nil, nil - } - out := make(map[string]interface{}, 4) - err := json.Unmarshal([]byte(v), &out) - return out, err -} - -func init() { - // add json format - extension.AddFormatParser(constant.JSON, &jsonExtParser{}) -} diff --git a/contrib/config/apollo/parser.go b/contrib/config/apollo/parser.go new file mode 100644 index 000000000..ab4cb434e --- /dev/null +++ b/contrib/config/apollo/parser.go @@ -0,0 +1,40 @@ +package apollo + +import ( + "encoding/json" + "gopkg.in/yaml.v3" + + "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) { + v, ok := configContent.(string) + if !ok { + return nil, nil + } + out := make(map[string]interface{}, 4) + err := json.Unmarshal([]byte(v), &out) + return out, err +} + +type yamlExtParser struct{} + +func (parser yamlExtParser) Parse(configContent interface{}) (out map[string]interface{}, err error) { + v, ok := configContent.(string) + if !ok { + return nil, nil + } + out = make(map[string]interface{}, 4) + err = yaml.Unmarshal([]byte(v), &out) + return +} + +func init() { + // add json/yaml/yml format + extension.AddFormatParser(constant.JSON, &jsonExtParser{}) + extension.AddFormatParser(constant.YAML, &yamlExtParser{}) + extension.AddFormatParser(constant.YML, &yamlExtParser{}) +}