-
Notifications
You must be signed in to change notification settings - Fork 2
/
section.go
177 lines (152 loc) · 4.71 KB
/
section.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package ini
import (
"bytes"
"fmt"
"io"
)
// manages all the key/value defined in the .ini file format
type Section struct {
//Name of the section
Name string
//key values
keyValues map[string]Key
}
// construct a new section with section name
func NewSection(name string) *Section {
return &Section{Name: name,
keyValues: make(map[string]Key)}
}
// add key/value to the section and overwrite the old one
func (section *Section) Add(key, value string) {
section.keyValues[key] = newNormalKey(key, value)
}
// check if the key is in the section
//
// return true if the section contains the key
func (section *Section) HasKey(key string) bool {
_, ok := section.keyValues[key]
return ok
}
// Get all the keys in the section
//
// return: all keys in the section
func (section *Section) Keys() []Key {
r := make([]Key, 0)
for _, v := range section.keyValues {
r = append(r, v)
}
return r
}
// Get the key.
//
// This method can be called even if the key is not in the
// section.
func (section *Section) Key(key string) Key {
if v, ok := section.keyValues[key]; ok {
return v
}
return newNonExistKey(key)
}
// Get value of key as string
func (section *Section) GetValue(key string) (string, error) {
return section.Key(key).Value()
}
// Get value of key and if the key does not exist, return the defValue
func (section *Section) GetValueWithDefault(key string, defValue string) string {
return section.Key(key).ValueWithDefault(defValue)
}
// Get the value of key as bool, it will return true if the value of the key is one
// of following( case insensitive):
// - true
// - yes
// - t
// - y
// - 1
func (section *Section) GetBool(key string) (bool, error) {
return section.Key(key).Bool()
}
// Get the value of key as bool and if the key does not exist, return the
// default value
func (section *Section) GetBoolWithDefault(key string, defValue bool) bool {
return section.Key(key).BoolWithDefault(defValue)
}
// Get the value of the key as int
func (section *Section) GetInt(key string) (int, error) {
return section.Key(key).Int()
}
// Get the value of the key as int and if the key does not exist return
// the default value
func (section *Section) GetIntWithDefault(key string, defValue int) int {
return section.Key(key).IntWithDefault(defValue)
}
// Get the value of the key as uint
func (section *Section) GetUint(key string) (uint, error) {
return section.Key(key).Uint()
}
// Get the value of the key as int and if the key does not exist return
// the default value
func (section *Section) GetUintWithDefault(key string, defValue uint) uint {
return section.Key(key).UintWithDefault(defValue)
}
// Get the value of the key as int64
func (section *Section) GetInt64(key string) (int64, error) {
return section.Key(key).Int64()
}
// Get the value of the key as int64 and if the key does not exist return
// the default value
func (section *Section) GetInt64WithDefault(key string, defValue int64) int64 {
return section.Key(key).Int64WithDefault(defValue)
}
// Get the value of the key as uint64
func (section *Section) GetUint64(key string) (uint64, error) {
return section.Key(key).Uint64()
}
// Get the value of the key as uint64 and if the key does not exist return
// the default value
func (section *Section) GetUint64WithDefault(key string, defValue uint64) uint64 {
return section.Key(key).Uint64WithDefault(defValue)
}
// Get the value of the key as float32
func (section *Section) GetFloat32(key string) (float32, error) {
return section.Key(key).Float32()
}
// Get the value of the key as float32 and if the key does not exist return
// the default value
func (section *Section) GetFloat32WithDefault(key string, defValue float32) float32 {
return section.Key(key).Float32WithDefault(defValue)
}
// Get the value of the key as float64
func (section *Section) GetFloat64(key string) (float64, error) {
return section.Key(key).Float64()
}
// Get the value of the key as float64 and if the key does not exist return
// the default value
func (section *Section) GetFloat64WithDefault(key string, defValue float64) float64 {
return section.Key(key).Float64WithDefault(defValue)
}
// convert the section content to the .ini section format, so the section content will
// be converted to following format:
//
// [sectionx]
// key1 = value1
// key2 = value2
//
func (section *Section) String() string {
buf := bytes.NewBuffer(make([]byte, 0))
section.Write(buf)
return buf.String()
}
// write the section content to the writer with .ini section format.
func (section *Section) Write(writer io.Writer) error {
_, err := fmt.Fprintf(writer, "[%s]\n", section.Name)
if err != nil {
return err
}
for _, v := range section.keyValues {
_, err = fmt.Fprintf(writer, "%s\n", v.String())
if err != nil {
return err
}
}
return nil
}