-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson.go
63 lines (50 loc) · 1.3 KB
/
json.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
package json
import (
"encoding/json"
"io"
"reflect"
"strings"
)
// Parser parses YAML documents.
type Parser struct {
delimiter string
}
// New constructs a new Parser.
func New() *Parser {
return &Parser{
delimiter: ".",
}
}
// SetDelimeter sets the key delimiter.
func (p *Parser) SetDelimeter(v string) {
p.delimiter = v
}
// Values parses yaml configuration, iterating over each key value pair and returning them until
// parsing has been completed.
func (p *Parser) Values(src io.ReadCloser) (<-chan func() (string, interface{}), error) {
var dst map[string]interface{}
d := json.NewDecoder(src)
if err := d.Decode(&dst); err != nil {
return nil, err
}
ch := make(chan func() (string, interface{}))
go func() {
defer close(ch)
p.recurse("", dst, ch)
}()
return ch, src.Close()
}
func (p *Parser) recurse(key string, m map[string]interface{}, ch chan func() (string, interface{})) {
for k, v := range m {
name := strings.Trim(strings.Join(append(strings.Split(key, p.delimiter), k), p.delimiter), p.delimiter)
if reflect.ValueOf(v).Kind() == reflect.Map {
p.recurse(name, v.(map[string]interface{}), ch)
continue
}
ch <- (func(key string, val interface{}) func() (string, interface{}) {
return func() (string, interface{}) {
return key, val
}
}(name, v))
}
}