1 Star 0 Fork 0

micro-tools/wf

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gini.go 2.51 KB
一键复制 编辑 原始数据 按行查看 历史
545403892 提交于 2023-09-27 22:16 +08:00 . 升级go-ole
// Package gini provides accessing and converting for INI content.
package gini
import (
"bufio"
"bytes"
"errors"
"fmt"
"gitee.com/micro-tools/wf/internal/json"
"io"
"strings"
)
// 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{})
a := bytes.NewReader(data)
r := bufio.NewReader(a)
var section string
var lastSection string
var haveSection bool
for {
line, err := r.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
lineStr := strings.TrimSpace(string(line))
if len(lineStr) == 0 {
continue
}
if lineStr[0] == ';' || lineStr[0] == '#' {
continue
}
sectionBeginPos := strings.Index(lineStr, "[")
sectionEndPos := strings.Index(lineStr, "]")
if sectionBeginPos >= 0 && sectionEndPos >= 2 {
section = lineStr[sectionBeginPos+1 : sectionEndPos]
if lastSection == "" {
lastSection = section
} else if lastSection != section {
lastSection = section
fieldMap = make(map[string]interface{})
}
haveSection = true
} else if haveSection == false {
continue
}
if strings.Contains(lineStr, "=") && haveSection {
values := strings.Split(lineStr, "=")
fieldMap[strings.TrimSpace(values[0])] = strings.TrimSpace(strings.Join(values[1:], ""))
res[section] = fieldMap
}
}
if haveSection == false {
return nil, errors.New("failed to parse INI file, section not found")
}
return res, nil
}
// Encode converts map to INI format.
func Encode(data map[string]interface{}) (res []byte, err error) {
w := new(bytes.Buffer)
w.WriteString("; this ini file is produced by package gini\n")
for k, v := range data {
n, err := w.WriteString(fmt.Sprintf("[%s]\n", k))
if err != nil || n == 0 {
return nil, fmt.Errorf("write data failed. %v", err)
}
for kk, vv := range v.(map[string]interface{}) {
n, err := w.WriteString(fmt.Sprintf("%s=%s\n", kk, vv.(string)))
if err != nil || n == 0 {
return nil, fmt.Errorf("write data failed. %v", err)
}
}
}
res = make([]byte, w.Len())
n, err := w.Read(res)
if err != nil || n == 0 {
return nil, fmt.Errorf("write data failed. %v", err)
}
return res, nil
}
// 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)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/micro-tools/wf.git
git@gitee.com:micro-tools/wf.git
micro-tools
wf
wf
v1.0.2

搜索帮助