-
Notifications
You must be signed in to change notification settings - Fork 0
/
gini.go
112 lines (97 loc) · 2.76 KB
/
gini.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/rglujing/gf.
// Package gini provides accessing and converting for INI content.
package gini
import (
"bufio"
"bytes"
"fmt"
"github.com/rglujing/gf/errors/gcode"
"github.com/rglujing/gf/errors/gerror"
"github.com/rglujing/gf/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, gerror.NewCode(gcode.CodeInvalidParameter, "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)
}