fix: add yaml / yml parser (#1808)
Co-authored-by: yang.liu14 <yang.liu14@weimob.com>pull/1833/head
parent
4dadafff90
commit
a52b17c268
@ -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{}) |
||||
} |
@ -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{}) |
||||
} |
Loading…
Reference in new issue