-
Notifications
You must be signed in to change notification settings - Fork 8
/
sinker.go
201 lines (182 loc) · 4.41 KB
/
sinker.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package ini
import (
"errors"
"fmt"
"reflect"
"strings"
)
// A generic IniSink that uses fmt.Sscan to parse non-string fields.
type GenericIniSink struct {
// If non-nil, only match this specific section (otherwise
// ignore).
Sec *IniSection
// Pointers to the fields that should be parsed.
Fields map[string]interface{}
}
// NewGenericSink([section [, subsection])
func NewGenericSink(args...string) *GenericIniSink {
var sec *IniSection
switch len(args) {
case 0:
sec = nil
case 1:
sec = &IniSection{
Section: args[0],
}
case 2:
sec = &IniSection{
Section: args[0],
Subsection: &args[1],
}
default:
panic(fmt.Errorf("NewGenericSink takes at most 2 arguments, not %d",
len(args)))
}
return &GenericIniSink{
Sec: sec,
Fields: make(map[string]interface{}),
}
}
// Add a field to be parsed
func (s *GenericIniSink) AddField(name string, ptr interface{}) {
s.Fields[name] = ptr
}
var errNotStructPtr = errors.New("argument must be pointer to struct")
// Populate a GenericIniSink with fields of a struct, using the field
// name or or the ini struct field tag (`ini:"field-name"`) if one
// exists. Tag `ini:"-"` says to ignore a field. Note that i must be
// a pointer to a structure or this function will panic.
func (s *GenericIniSink) AddStruct(i interface{}) {
v := reflect.ValueOf(i)
if v.Kind() != reflect.Ptr {
panic(errNotStructPtr)
}
v = v.Elem()
if v.Kind() != reflect.Struct {
panic(errNotStructPtr)
}
t := v.Type()
for i, n := 0, t.NumField(); i < n; i++ {
f := t.Field(i)
name := f.Tag.Get("ini")
if name == "-" {
continue
} else if name == "" {
name = strings.ReplaceAll(f.Name, "_", "-")
}
s.AddField(name, v.Field(i).Addr().Interface())
}
}
// Save the current state of an Ini-parsable structure to a set of
// IniEdits. This is useful for creating an initial file. If
// includeZero is true, then all fields are saved; otherwise, only
// ones with non-default values are saved.
func (s *GenericIniSink) SaveAll(ies *IniEdits, includeZero bool) {
for name, i := range s.Fields {
*ies = append(*ies, func(ie *IniEditor){
v := reflect.ValueOf(i).Elem()
if includeZero || !v.IsZero() {
ie.Set(s.Sec, name, fmt.Sprint(v.Interface()))
}
})
}
}
func (s *GenericIniSink) String() string {
out := strings.Builder{}
if s.Sec != nil {
fmt.Fprintf(&out, "%s\n", s.Sec.String())
}
for name, i := range s.Fields {
v := reflect.ValueOf(i).Elem()
if v.Kind() == reflect.Slice {
for j := 0; j < v.Len(); j++ {
fmt.Fprintf(&out, "\t%s = %s\n", name,
EscapeIniValue(fmt.Sprint(v.Index(j).Interface())))
}
} else {
fmt.Fprintf(&out, "\t%s = %s\n", name,
EscapeIniValue(fmt.Sprint(v.Interface())))
}
}
return out.String()
}
func (s *GenericIniSink) Item(ii IniItem) error {
if s.Sec.Eq(ii.IniSection) {
if i, ok := s.Fields[ii.Key]; ok {
v := reflect.ValueOf(i).Elem()
if ii.Value == nil {
v.Set(reflect.Zero(v.Type()))
} else if v.Kind() == reflect.String {
v.SetString(ii.Val())
} else if v.Kind() == reflect.Slice {
e := reflect.New(v.Type().Elem())
if e.Elem().Kind() == reflect.String {
e.Elem().SetString(ii.Val())
} else if _, err :=
fmt.Sscan(*ii.Value, e.Interface()); err != nil {
return err
}
v.Set(reflect.Append(v, e.Elem()))
} else {
_, err := fmt.Sscan(*ii.Value, i)
return err
}
return nil
}
}
return nil
}
func (s *GenericIniSink) IniSink() IniSink {
return s
}
var _ IniSinker = &GenericIniSink{} // XXX
type IniSinker interface {
IniSink() IniSink
}
type IniSinks []IniSink
func (s *IniSinks) Push(i IniSink) {
*s = append(*s, i)
}
func (s IniSinks) Init() {
for i := range s {
if init, ok := s[i].(interface{ Init() }); ok {
init.Init()
}
}
}
func (s IniSinks) Item(ii IniItem) error {
for i := range s {
if err := s[i].Item(ii); err != nil {
return err
}
}
return nil
}
func (s IniSinks) Section(ss IniSecStart) error {
for i := range s {
if sec, ok := s[i].(interface{ Section(IniSecStart) error }); ok {
if err := sec.Section(ss); err != nil {
return err
}
}
}
return nil
}
func (s IniSinks) Done(IniRange) {
for i := range s {
if done, ok := s[i].(interface{ Done() }); ok {
done.Done()
}
}
}
func (s IniSinks) IniSink() IniSink {
return s
}
func (s IniSinks) String() string {
ret := strings.Builder{}
for i := range s {
fmt.Fprintln(&ret, s[i])
}
return ret.String()
}
var _ IniSinker = IniSinks{}