mirror of
https://gitee.com/johng/gf
synced 2026-07-04 21:03:13 +08:00
Merge pull request #326 from wenzi1/master
This commit is contained in:
@ -16,7 +16,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Decode converts INI format to map.
|
||||
//Decode converts INI format to map
|
||||
func Decode(data []byte) (res map[string]interface{}, err error) {
|
||||
res = make(map[string]interface{})
|
||||
fieldMap := make(map[string]interface{})
|
||||
@ -72,10 +72,13 @@ func Decode(data []byte) (res map[string]interface{}, err error) {
|
||||
|
||||
}
|
||||
|
||||
if haveSection == false {
|
||||
return nil, fmt.Errorf("Failed to parse INI file, not found section")
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Encode converts map to INI format.
|
||||
//Encode converts map to INI format
|
||||
func Encode(data map[string]interface{}) (res []byte, err error) {
|
||||
w := new(bytes.Buffer)
|
||||
|
||||
@ -102,11 +105,12 @@ func Encode(data map[string]interface{}) (res []byte, err error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToJson convert INI format to JSON.
|
||||
//ToJson convert INI format to JSON
|
||||
func ToJson(data []byte) (res []byte, err error) {
|
||||
iniMap, err := Decode(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return json.Marshal(iniMap)
|
||||
}
|
||||
|
||||
@ -7,7 +7,6 @@
|
||||
package gini_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/encoding/gini"
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
@ -39,7 +38,6 @@ func TestDecode(t *testing.T) {
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
fmt.Println(res)
|
||||
gtest.Assert(res["addr"].(map[string]interface{})["ip"], "127.0.0.1")
|
||||
gtest.Assert(res["addr"].(map[string]interface{})["port"], "9001")
|
||||
gtest.Assert(res["DBINFO"].(map[string]interface{})["user"], "root")
|
||||
@ -47,6 +45,15 @@ func TestDecode(t *testing.T) {
|
||||
gtest.Assert(res["键"].(map[string]interface{})["呵呵"], "值")
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
errContent := `
|
||||
a = b
|
||||
`
|
||||
_, err := gini.Decode([]byte(errContent))
|
||||
if err == nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestEncode(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user