Skip to content

Commit 1d12eb9

Browse files
committed
fix: workaround to fix the indexing of array of strings not working
The bug is in the jsonparser: buger/jsonparser#232 Signed-off-by: Leonardo Di Donato <[email protected]>
1 parent e6587b2 commit 1d12eb9

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Like this:
137137

138138
```caddyfile
139139
log {
140-
format jsonselect "{level} {timestamp:ts} {httpRequest>requestMethod:request>method} {httpRequest>protocol:request>proto} {httpRequest>status:status} {httpRequest>responseSize:size}" {
140+
format jsonselect "{level} {timestamp:ts} {httpRequest>requestMethod:request>method} {httpRequest>protocol:request>proto} {httpRequest>status:status} {httpRequest>responseSize:size} {httpRequest>userAgent:request>headers>User-Agent>[0]}" {
141141
time_format "rfc3339_nano"
142142
}
143143
}
@@ -146,7 +146,7 @@ log {
146146
Which outputs:
147147

148148
```json
149-
{"level":"info","timestamp":"2021-07-19T14:48:56.262966Z","httpRequest":{"protocol":"HTTP/2.0","requestMethod":"GET","responseSize":17604,"status":200}}
149+
{"level":"info","timestamp":"2021-07-19T14:48:56.262966Z","httpRequest":{"protocol":"HTTP/2.0","requestMethod":"GET","responseSize":17604,"status":200,"userAgent":"Mozilla/5.0 ..."}}
150150
```
151151

152152
## Try it out

plugin.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,23 @@ func (e JSONSelectEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Fie
109109
return buf, err
110110
}
111111

112-
// fixme > indexing array of strings not working at the moment
113-
// fixme > this is a bug in jsonparser (see https://github.com/buger/jsonparser/issues/232)
114-
// todo > workaround by iterating on paths and calling jsonparser.Get()
115112
res := []byte{'{', '}'}
116-
jsonparser.EachKey(
117-
buf.Bytes(),
118-
func(idx int, val []byte, typ jsonparser.ValueType, err error) {
119-
// todo > handle error
120-
switch typ {
121-
case jsonparser.NotExist:
122-
// path not found, skip
123-
case jsonparser.String:
124-
res, _ = jsonparser.Set(res, append(append([]byte{'"'}, val...), '"'), e.setters[idx]...)
125-
default:
126-
res, _ = jsonparser.Set(res, val, e.setters[idx]...)
127-
}
128-
},
129-
e.getters...,
130-
)
113+
// Temporary workaround the bug https://github.com/buger/jsonparser/issues/232
114+
// todo > switch back to EachKey (see git history) for perf reasons when fixed
115+
for idx, paths := range e.getters {
116+
val, typ, _, err := jsonparser.Get(buf.Bytes(), paths...)
117+
if err != nil {
118+
return nil, err
119+
}
120+
switch typ {
121+
case jsonparser.NotExist:
122+
// path not found, skip
123+
case jsonparser.String:
124+
res, _ = jsonparser.Set(res, append(append([]byte{'"'}, val...), '"'), e.setters[idx]...)
125+
default:
126+
res, _ = jsonparser.Set(res, val, e.setters[idx]...)
127+
}
128+
}
131129

132130
// Reset the buffer to output our own content
133131
buf.Reset()

0 commit comments

Comments
 (0)