Skip to content

Commit

Permalink
Merge e58babf into e3c2021
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Jun 13, 2020
2 parents e3c2021 + e58babf commit fb5b033
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
15 changes: 13 additions & 2 deletions utils/parse/yml/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package yml

import (
"bytes"
"encoding/json"

"github.com/spf13/cast"
"github.com/spf13/viper"
"github.com/zouyx/agollo/v3/constant"
"github.com/zouyx/agollo/v3/extension"
Expand All @@ -11,7 +14,9 @@ import (
var vp = viper.New()

func init() {
extension.AddFormatParser(constant.YML, &Parser{})
parser := &Parser{}
extension.AddFormatParser(constant.YML, parser)
extension.AddFormatParser(constant.YAML, parser)
vp.SetConfigType("yml")
}

Expand Down Expand Up @@ -42,7 +47,13 @@ func convertToMap(vp *viper.Viper) map[string]string {

m := make(map[string]string)
for _, key := range vp.AllKeys() {
m[key] = vp.GetString(key)
v := vp.Get(key)
s, err := cast.ToStringE(v)
if err != nil {
b, _ := json.Marshal(v)
s = string(b)
}
m[key] = s
}
return m
}
7 changes: 5 additions & 2 deletions utils/parse/yml/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ c:
c1: c1
d:
d1: d1
e:
e1: e1`)
e:
e1:
- xxx
- xxx`)
Assert(t, err, NilVal())

Assert(t, s["a.a1"], Equal("a1"))

Assert(t, s["b.b1"], Equal("b1"))

Assert(t, s["c.c1"], Equal("c1"))
Assert(t, s["e.e1"], Equal(`["xxx","xxx"]`))
}

0 comments on commit fb5b033

Please sign in to comment.