Skip to content

Commit

Permalink
fix YML bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Jun 13, 2020
1 parent 461d21e commit e58babf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
11 changes: 10 additions & 1 deletion 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 Down Expand Up @@ -44,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 e58babf

Please sign in to comment.