-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamjson.go
More file actions
162 lines (151 loc) · 2.88 KB
/
Copy pathstreamjson.go
File metadata and controls
162 lines (151 loc) · 2.88 KB
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 streamjson
import (
"encoding/json"
"fmt"
"io"
"strings"
)
type StreamJson struct {
cbs map[string]func(any, error)
called map[string]bool
dec *json.Decoder
rangeFun func(string, any)
}
func NewStreamJson() *StreamJson {
s := &StreamJson{
cbs: make(map[string]func(any, error)),
called: make(map[string]bool),
rangeFun: func(string, any) {},
}
return s
}
func (s *StreamJson) AddMonitor(pattern string, cb func(any, error)) {
s.cbs[pattern] = cb
s.called[pattern] = false
}
func (s *StreamJson) MonitorAll(cb func(string, any)) {
s.rangeFun = cb
}
func (s *StreamJson) ProcessStream(r io.Reader) error {
s.dec = json.NewDecoder(r)
var f json.Delim
var ok bool
keys := []string{}
firstTok, err := s.dec.Token()
if err != nil {
if err == io.EOF {
goto finally
}
return err
}
f, ok = firstTok.(json.Delim)
if ok && f == '{' {
err = s.process(&keys)
}
if ok && f == '[' {
err = s.array(&keys, "")
}
if err != nil {
return err
}
finally:
for pattern, ok := range s.called {
if !ok {
s.cbs[pattern](nil, fmt.Errorf("not find pattern: "+pattern))
}
}
return nil
}
func (s *StreamJson) process(keys *[]string) error {
var key string
for {
tok, err := s.dec.Token()
if err != nil {
if err == io.EOF {
break
}
return err
}
f, ok := tok.(json.Delim)
if ok && f == '{' {
if key != "" {
*keys = append(*keys, key)
key = ""
}
err := s.process(keys)
if err != nil {
return err
}
continue
}
if ok && f == '}' {
if len(*keys) > 0 && (*keys)[len(*keys)-1] != "*" {
*keys = (*keys)[:len(*keys)-1]
}
break
}
if key == "" {
key = tok.(string)
continue
}
newKey := key
if len(*keys) > 0 {
newKey = strings.Join(append(*keys, key), ".")
}
if f, ok := tok.(json.Delim); ok && f == '[' {
*keys = append(*keys, key)
*keys = append(*keys, "*")
s.array(keys, newKey)
if len(*keys) > 0 {
*keys = (*keys)[:len(*keys)-1]
}
key = ""
continue
}
s.rangeFun(newKey, tok)
if cb, ok := s.cbs[newKey]; ok {
cb(tok, nil)
s.called[newKey] = true
}
key = ""
}
return nil
}
func (s *StreamJson) array(keys *[]string, key string) error {
if key != "" {
key = key + ".*"
} else {
key = "*"
}
for {
tok, err := s.dec.Token()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
f, ok := tok.(json.Delim)
if ok && f == '[' {
*keys = append(*keys, "*")
s.array(keys, key)
continue
}
if ok && f == ']' {
if len(*keys) > 0 {
*keys = (*keys)[:len(*keys)-1]
}
break
}
if ok && f == '{' {
s.process(keys)
continue
}
s.rangeFun(key, tok)
if cb, ok := s.cbs[key]; ok {
cb(tok, nil)
s.called[key] = true
}
}
return nil
}