add buildin functions xml/ini/yaml/toml for package gview

This commit is contained in:
John Guo
2022-01-17 15:55:45 +08:00
parent d4b502f14e
commit c72a9f2e1e
8 changed files with 144 additions and 13 deletions

View File

@ -81,20 +81,32 @@ func Decode(data []byte) (res map[string]interface{}, err error) {
// Encode converts map to INI format.
func Encode(data map[string]interface{}) (res []byte, err error) {
var (
n int
w = new(bytes.Buffer)
n int
w = new(bytes.Buffer)
m map[string]interface{}
ok bool
)
for k, v := range data {
n, err = w.WriteString(fmt.Sprintf("[%s]\n", k))
if err != nil || n == 0 {
return nil, gerror.Wrapf(err, "w.WriteString failed")
}
for kk, vv := range v.(map[string]interface{}) {
n, err = w.WriteString(fmt.Sprintf("%s=%s\n", kk, vv.(string)))
for section, item := range data {
// Section key-value pairs.
if m, ok = item.(map[string]interface{}); ok {
n, err = w.WriteString(fmt.Sprintf("[%s]\n", section))
if err != nil || n == 0 {
return nil, gerror.Wrapf(err, "w.WriteString failed")
}
for k, v := range m {
if n, err = w.WriteString(fmt.Sprintf("%s=%v\n", k, v)); err != nil || n == 0 {
return nil, gerror.Wrapf(err, "w.WriteString failed")
}
}
continue
}
// Simple key-value pairs.
for k, v := range data {
if n, err = w.WriteString(fmt.Sprintf("%s=%v\n", k, v)); err != nil || n == 0 {
return nil, gerror.Wrapf(err, "w.WriteString failed")
}
}
break
}
res = make([]byte, w.Len())
if n, err = w.Read(res); err != nil || n == 0 {

View File

@ -172,7 +172,7 @@ func (j *Json) MustToTomlString() string {
func (j *Json) ToIni() ([]byte, error) {
j.mu.RLock()
defer j.mu.RUnlock()
return gini.Encode((*(j.p)).(map[string]interface{}))
return gini.Encode(j.Map())
}
// ToIniString ini to string

View File

@ -20,6 +20,21 @@ func Valid(data interface{}) bool {
return json.Valid(gconv.Bytes(data))
}
// Marshal is alias of Encode in order to fit the habit of json.Marshal/Unmarshal functions.
func Marshal(v interface{}) (marshaledBytes []byte, err error) {
return Encode(v)
}
// MarshalIndent is alias of json.MarshalIndent in order to fit the habit of json.MarshalIndent function.
func MarshalIndent(v interface{}, prefix, indent string) (marshaledBytes []byte, err error) {
return json.MarshalIndent(v, prefix, indent)
}
// Unmarshal is alias of DecodeTo in order to fit the habit of json.Marshal/Unmarshal functions.
func Unmarshal(data []byte, v interface{}) (err error) {
return DecodeTo(data, v)
}
// Encode encodes any golang variable `value` to JSON bytes.
func Encode(value interface{}) ([]byte, error) {
return json.Marshal(value)

View File

@ -38,6 +38,27 @@ dd = 11
cache = "127.0.0.1:6379,1"
`
func Test_Encode(t *testing.T) {
// Map.
gtest.C(t, func(t *gtest.T) {
b, err := gyaml.Encode(g.Map{
"k": "v",
})
t.AssertNil(err)
t.Assert(string(b), `k: v
`)
})
// Array.
gtest.C(t, func(t *gtest.T) {
b, err := gyaml.Encode([]string{"a", "b", "c"})
t.AssertNil(err)
t.Assert(string(b), `- a
- b
- c
`)
})
}
func Test_Decode(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
result, err := gyaml.Decode([]byte(yamlStr))