-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathencode.go
162 lines (139 loc) · 3.77 KB
/
encode.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package parser
import (
"bytes"
"errors"
"fmt"
"sort"
"github.com/gookit/goutil/timex"
)
// Encode golang data(map, struct) to INI string.
func Encode(v any) ([]byte, error) { return EncodeWithDefName(v) }
// EncodeWithDefName golang data(map, struct) to INI, can set default section name
func EncodeWithDefName(v any, defSection ...string) (out []byte, err error) {
switch vd := v.(type) {
case map[string]any: // from full mode
return EncodeFull(vd, defSection...)
case map[string]map[string]string: // from lite mode
return EncodeSimple(vd, defSection...)
default:
err = errors.New("ini: invalid data to encode as INI")
}
return
}
// EncodeFull full mode data to INI, can set default section name
func EncodeFull(data map[string]any, defSection ...string) (out []byte, err error) {
ln := len(data)
if ln == 0 {
return
}
defSecName := ""
if len(defSection) > 0 {
defSecName = defSection[0]
}
sortedGroups := make([]string, 0, ln)
for section := range data {
sortedGroups = append(sortedGroups, section)
}
buf := &bytes.Buffer{}
buf.Grow(ln * 4)
buf.WriteString("; exported at " + timex.Now().Datetime() + "\n\n")
secBuf := &bytes.Buffer{}
sort.Strings(sortedGroups)
max := len(sortedGroups) - 1
for idx, section := range sortedGroups {
item := data[section]
switch tpData := item.(type) {
case []int:
case []string: // array of the default section
for _, v := range tpData {
buf.WriteString(fmt.Sprintf("%s[] = %v\n", section, v))
}
// case map[string]string: // is section
case map[string]any: // is section
if section != defSecName {
secBuf.WriteString("[" + section + "]\n")
writeAnyMap(secBuf, tpData)
} else {
writeAnyMap(buf, tpData)
}
if idx < max {
secBuf.WriteByte('\n')
}
default: // k-v of the default section
buf.WriteString(fmt.Sprintf("%s = %v\n", section, tpData))
}
}
buf.WriteByte('\n')
buf.Write(secBuf.Bytes())
out = buf.Bytes()
secBuf = nil
return
}
func writeAnyMap(buf *bytes.Buffer, data map[string]any) {
for key, item := range data {
switch tpData := item.(type) {
case []int:
case []string: // array of the default section
for _, v := range tpData {
buf.WriteString(key + "[] = ")
buf.WriteString(fmt.Sprint(v))
buf.WriteByte('\n')
}
default: // k-v of the section
buf.WriteString(key + " = ")
buf.WriteString(fmt.Sprint(tpData))
buf.WriteByte('\n')
}
}
}
// EncodeSimple data to INI
func EncodeSimple(data map[string]map[string]string, defSection ...string) ([]byte, error) {
return EncodeLite(data, defSection...)
}
// EncodeLite data to INI
func EncodeLite(data map[string]map[string]string, defSection ...string) (out []byte, err error) {
ln := len(data)
if ln == 0 {
return
}
defSecName := ""
if len(defSection) > 0 {
defSecName = defSection[0]
}
sortedGroups := make([]string, 0, ln)
for section := range data {
// don't add section title for default section
if section != defSecName {
sortedGroups = append(sortedGroups, section)
}
}
buf := &bytes.Buffer{}
buf.Grow(ln * 4)
buf.WriteString("; exported at " + timex.Now().Datetime() + "\n\n")
// first, write default section values
if defSec, ok := data[defSecName]; ok {
writeStringMap(buf, defSec)
buf.WriteByte('\n')
}
sort.Strings(sortedGroups)
max := len(sortedGroups) - 1
for idx, section := range sortedGroups {
buf.WriteString("[" + section + "]\n")
writeStringMap(buf, data[section])
if idx < max {
buf.WriteByte('\n')
}
}
out = buf.Bytes()
return
}
func writeStringMap(buf *bytes.Buffer, strMap map[string]string) {
sortedKeys := make([]string, 0, len(strMap))
for key := range strMap {
sortedKeys = append(sortedKeys, key)
}
sort.Strings(sortedKeys)
for _, key := range sortedKeys {
buf.WriteString(key + " = " + strMap[key] + "\n")
}
}