forked from sspencer/go-ini
/
decode.go
544 lines (437 loc) · 12 KB
/
decode.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// Decode INI files with a syntax similar to JSON decoding
package ini
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"reflect"
"strconv"
"strings"
)
type Unmatched struct {
lineNum int
line string
}
type IniError struct {
lineNum int
line string
error string
}
// decodeState represents the state while decoding a INI value.
type decodeState struct {
lineNum int
line string
scanner *bufio.Scanner
savedError error
unmatched []Unmatched
}
type property struct {
tag string
value reflect.Value
children propertyMap
isArray bool
//array []interface{}
isInitialized bool
}
type propertyMap map[string]property
//------------------------------------------------------------------
// NewStack returns a new stack.
func NewPropMapStack() *PropMapStack {
return &PropMapStack{}
}
// Stack is a basic LIFO stack that resizes as needed.
type PropMapStack struct {
items []propertyMap
count int
}
// Push adds an iterm to the top of the stack
func (s *PropMapStack) Push(item propertyMap) {
s.items = append(s.items[:s.count], item)
s.count++
}
// Pop removes the top item (LIFO) from the stack
func (s *PropMapStack) Pop() propertyMap {
if s.count == 0 {
return nil
}
s.count--
return s.items[s.count]
}
// Peek returns item at top of stack without removing it
func (s *PropMapStack) Peek() propertyMap {
if s.count == 0 {
return nil
}
return s.items[s.count-1]
}
// Empty returns true when stack is empty, false otherwise
func (s *PropMapStack) Empty() bool {
return s.count == 0
}
// Size returns the number of items in the stack
func (s *PropMapStack) Size() int {
return s.count
}
/*
* Unmarshal parses the INI-encoded data and stores the result
* in the value pointed to by v.
*/
func Unmarshal(data []byte, v interface{}) error {
var d decodeState
d.init(data)
return d.unmarshal(v)
}
/*
* String interfacer for Unmatched
*/
func (u Unmatched) String() string {
return fmt.Sprintf("%d %s", u.lineNum, u.line)
}
/*
* Conform to Error Interfacer
*/
func (e *IniError) Error() string {
if e.lineNum > 0 {
return fmt.Sprintf("%s on line %d: \"%s\"", e.error, e.lineNum, e.line)
} else {
return e.error
}
}
/*
* Stringer interface for property
*/
func (p property) String() string {
return fmt.Sprintf("<property %s, isArray:%t>", p.tag, p.isArray)
}
/*
* Convenience function to prep for decoding byte array.
*/
func (d *decodeState) init(data []byte) *decodeState {
d.lineNum = 0
d.line = ""
d.scanner = bufio.NewScanner(bytes.NewReader(data))
d.savedError = nil
return d
}
/*
* saveError saves the first err it is called with,
* for reporting at the end of the unmarshal.
*/
func (d *decodeState) saveError(err error) {
if d.savedError == nil {
d.savedError = err
}
}
/*
* Recursive function to map data types in the describing structs
* to string markers (tags) in the INI file.
*/
func (d *decodeState) generateMap(m propertyMap, v reflect.Value) {
if v.Type().Kind() == reflect.Ptr {
d.generateMap(m, v.Elem())
} else if v.Kind() == reflect.Struct {
typ := v.Type()
for i := 0; i < typ.NumField(); i++ {
sf := typ.Field(i)
f := v.Field(i)
kind := f.Type().Kind()
tag := sf.Tag.Get("ini")
if len(tag) == 0 {
tag = sf.Name
}
tag = strings.TrimSpace(strings.ToLower(tag))
st := property{tag, f, make(propertyMap), kind == reflect.Slice, true}
// some structures are just for organizing data
if tag != "-" {
m[tag] = st
}
if kind == reflect.Struct {
if tag == "-" {
d.generateMap(m, f)
} else {
// little namespacing here so property names can
// be the same under different sections
//fmt.Printf("Struct tag: %s, type: %s\n", tag, f.Type())
d.generateMap(st.children, f)
}
} else if kind == reflect.Slice {
d.generateMap(st.children, reflect.New(f.Type().Elem()))
}
}
}
}
/*
* Iterates line-by-line through INI file setting values into a struct.
*/
func (d *decodeState) unmarshal(x interface{}) error {
var topMap propertyMap
topMap = make(propertyMap)
d.generateMap(topMap, reflect.ValueOf(x))
propStack := NewPropMapStack()
propStack.Push(topMap)
// for every line in file
for d.scanner.Scan() {
if d.savedError != nil {
break // breaks on first error
}
d.line = d.scanner.Text()
d.lineNum++
line := strings.TrimSpace(d.line)
if len(line) < 1 || line[0] == ';' || line[0] == '#' {
continue // skip comments
}
// Two types of lines:
// 1. NAME=VALUE (at least one equal sign - breaks on first)
// 2. [HEADER] (no equals sign, square brackets NOT required)
matches := strings.SplitN(line, "=", 2)
matched := false
pn := ""
pv := ""
if len(matches) == 2 {
// NAME=VALUE
pn = strings.ToLower(strings.TrimSpace(matches[0]))
pv = strings.TrimSpace(matches[1])
prop := propStack.Peek()[pn]
if prop.isInitialized {
if prop.isArray {
value := reflect.New(prop.value.Type().Elem())
d.setValue(reflect.Indirect(value), pv)
appendValue(prop.value, value)
} else {
d.setValue(prop.value, pv)
}
matched = true
}
// What if property is umatched - keep popping the stack
// until a potential map is found or stay within current section?
// Think answer is pop.
// NOPE
// Section could have unmatched name=value if user doesn't
// care about certain values - only stack crawling happens
// during numMatches==1 time?
// This means if there is > 1 section, there needs to be
// section breaks for everything
} else {
// [Header] section
pn = strings.ToLower(strings.TrimSpace(matches[0]))
for propStack.Size() > 0 {
prop := propStack.Peek()[pn]
if prop.isInitialized {
propStack.Push(prop.children)
matched = true
break
} else if propStack.Size() > 1 {
_ = propStack.Pop()
} else {
break
}
}
}
if !matched {
d.unmatched = append(d.unmatched, Unmatched{d.lineNum, d.line})
}
}
return d.savedError
}
func (d *decodeState) unmarshal2(x interface{}) error {
var sectionMap propertyMap = make(propertyMap)
var tempMap propertyMap = make(propertyMap)
var section, nextSection property
var inSection, nextHasSection bool = false, false
var tempValue reflect.Value // "temp" is for filling in array of structs
var numTempValue int
d.generateMap(sectionMap, reflect.ValueOf(x))
for d.scanner.Scan() {
if d.savedError != nil {
break
}
d.line = d.scanner.Text()
d.lineNum++
//fmt.Printf("%03d: %s\n", d.lineNum, d.line)
line := strings.ToLower(strings.TrimSpace(d.line))
if len(line) < 1 || line[0] == ';' || line[0] == '#' {
continue // skip comments
}
// [Sections] could appear at any time (square brackets not required)
// When in a section, also look in children map
nextSection, nextHasSection = sectionMap[line]
if nextHasSection {
if numTempValue > 0 && section.isArray {
appendValue(section.value, tempValue)
}
section = nextSection
inSection = true
if section.isArray {
tempValue = reflect.New(section.value.Type().Elem())
d.generateMap(tempMap, tempValue)
}
numTempValue = 0
continue
}
// unrecognized section - exit out of current section
if line[0] == '[' && line[len(line)-1] == ']' {
inSection = false
continue
}
matches := strings.SplitN(d.line, "=", 2)
matched := false
// potential property=value
if len(matches) == 2 {
n := strings.ToLower(strings.TrimSpace(matches[0]))
s := strings.TrimSpace(matches[1])
if inSection {
// child property, within a section
childProperty, hasProp := section.children[n]
if hasProp {
if section.isArray {
tempProperty := tempMap[n]
numTempValue++
d.setValue(tempProperty.value, s)
} else {
d.setValue(childProperty.value, s)
}
matched = true
}
}
if !matched {
// top level property
topLevelProperty, hasProp := sectionMap[n]
if hasProp {
// just encountered a top level property - switch out of section mode
inSection = false
matched = true
d.setValue(topLevelProperty.value, s)
}
}
}
if !matched {
d.unmatched = append(d.unmatched, Unmatched{d.lineNum, d.line})
}
}
if numTempValue > 0 {
appendValue(section.value, tempValue)
}
return d.savedError
}
func appendValue(arr, val reflect.Value) {
arr.Set(reflect.Append(arr, reflect.Indirect(val)))
}
// Set Value with given string
func (d *decodeState) setValue(v reflect.Value, s string) {
//fmt.Printf("SET(kind:%s, %s)\n", v.Kind(), s)
switch v.Kind() {
case reflect.String:
v.SetString(s)
case reflect.Bool:
v.SetBool(boolValue(s))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(s, 10, 64)
if err != nil || v.OverflowInt(n) {
d.saveError(&IniError{d.lineNum, d.line, "Invalid int"})
return
}
v.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
n, err := strconv.ParseUint(s, 10, 64)
if err != nil || v.OverflowUint(n) {
d.saveError(&IniError{d.lineNum, d.line, "Invalid uint"})
return
}
v.SetUint(n)
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(s, v.Type().Bits())
if err != nil || v.OverflowFloat(n) {
d.saveError(&IniError{d.lineNum, d.line, "Invalid float"})
return
}
v.SetFloat(n)
case reflect.Slice:
d.sliceValue(v, s)
default:
d.saveError(&IniError{d.lineNum, d.line, fmt.Sprintf("Can't set value of type %s", v.Kind())})
}
}
func (d *decodeState) sliceValue(v reflect.Value, s string) {
//fmt.Printf(":SLICE(%s, %s)\n", v.Kind(), s)
switch v.Type().Elem().Kind() {
case reflect.String:
v.Set(reflect.Append(v, reflect.ValueOf(s)))
case reflect.Bool:
v.Set(reflect.Append(v, reflect.ValueOf(boolValue(s))))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// Hardcoding of []int temporarily
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
d.saveError(&IniError{d.lineNum, d.line, "Invalid int"})
return
}
n1 := reflect.ValueOf(n)
n2 := n1.Convert(v.Type().Elem())
v.Set(reflect.Append(v, n2))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
n, err := strconv.ParseUint(s, 10, 64)
if err != nil {
d.saveError(&IniError{d.lineNum, d.line, "Invalid uint"})
return
}
n1 := reflect.ValueOf(n)
n2 := n1.Convert(v.Type().Elem())
v.Set(reflect.Append(v, n2))
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(s, 64)
if err != nil {
d.saveError(&IniError{d.lineNum, d.line, "Invalid float"})
return
}
n1 := reflect.ValueOf(n)
n2 := n1.Convert(v.Type().Elem())
v.Set(reflect.Append(v, n2))
default:
d.saveError(&IniError{d.lineNum, d.line, fmt.Sprintf("Can't set value in array of type %s",
v.Type().Elem().Kind())})
}
}
// Returns true for truthy values like t/true/y/yes/1, false otherwise
func boolValue(s string) bool {
v := false
switch strings.ToLower(s) {
case "t", "true", "y", "yes", "1":
v = true
}
return v
}
// A Decoder reads and decodes INI object from an input stream.
type Decoder struct {
r io.Reader
d decodeState
}
// NewDecoder returns a new decoder that reads from r.
//
// The decoder introduces its own buffering and may
// read data from r beyond the JSON values requested.
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{r: r}
}
// Decode reads the INI file and stores it in the value pointed to by v.
//
// See the documentation for Unmarshal for details about
// the conversion of an INI into a Go value.
func (dec *Decoder) Decode(v interface{}) error {
buf, readErr := ioutil.ReadAll(dec.r)
if readErr != nil {
return readErr
}
// Don't save err from unmarshal into dec.err:
// the connection is still usable since we read a complete JSON
// object from it before the error happened.
dec.d.init(buf)
err := dec.d.unmarshal(v)
return err
}
// UnparsedLines returns an array of strings where each string is an
// unparsed line from the file.
func (dec *Decoder) Unmatched() []Unmatched {
return dec.d.unmatched
}