2019-05-13 01:27:26 +02:00
|
|
|
package jsn
|
2019-05-09 01:03:18 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-05-12 07:36:52 +02:00
|
|
|
|
|
|
|
"github.com/cespare/xxhash/v2"
|
2019-05-09 01:03:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Filter(w *bytes.Buffer, b []byte, keys []string) error {
|
|
|
|
var err error
|
|
|
|
|
2019-05-12 07:36:52 +02:00
|
|
|
kmap := make(map[uint64]struct{}, len(keys))
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-12 07:36:52 +02:00
|
|
|
for i := range keys {
|
|
|
|
kmap[xxhash.Sum64String(keys[i])] = struct{}{}
|
2019-05-09 01:03:18 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
// is an list
|
2019-05-09 01:03:18 +02:00
|
|
|
isList := false
|
2019-05-09 05:52:04 +02:00
|
|
|
|
|
|
|
// list item
|
2019-05-09 01:03:18 +02:00
|
|
|
item := 0
|
2019-05-09 05:52:04 +02:00
|
|
|
|
|
|
|
// field in an object
|
2019-05-09 01:03:18 +02:00
|
|
|
field := 0
|
2019-05-09 05:52:04 +02:00
|
|
|
|
|
|
|
s, e, d := 0, 0, 0
|
|
|
|
|
2019-05-12 07:36:52 +02:00
|
|
|
var k []byte
|
|
|
|
state := expectKey
|
2019-05-09 05:52:04 +02:00
|
|
|
|
2019-05-09 01:03:18 +02:00
|
|
|
for i := 0; i < len(b); i++ {
|
2019-05-09 05:52:04 +02:00
|
|
|
if state == expectObjClose || state == expectListClose {
|
|
|
|
switch b[i] {
|
|
|
|
case '{', '[':
|
|
|
|
d++
|
|
|
|
case '}', ']':
|
|
|
|
d--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 01:03:18 +02:00
|
|
|
switch {
|
|
|
|
case state == expectKey:
|
|
|
|
switch b[i] {
|
|
|
|
case '[':
|
|
|
|
if !isList {
|
|
|
|
err = w.WriteByte('[')
|
|
|
|
}
|
|
|
|
isList = true
|
|
|
|
case '{':
|
|
|
|
if item == 0 {
|
|
|
|
err = w.WriteByte('{')
|
|
|
|
} else {
|
2019-05-09 05:52:04 +02:00
|
|
|
_, err = w.Write([]byte("},{"))
|
2019-05-09 01:03:18 +02:00
|
|
|
}
|
|
|
|
item++
|
|
|
|
field = 0
|
|
|
|
case '"':
|
|
|
|
state = expectKeyClose
|
|
|
|
s = i
|
|
|
|
i++
|
|
|
|
}
|
2019-05-09 05:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-09 01:03:18 +02:00
|
|
|
case state == expectKeyClose && b[i] == '"':
|
|
|
|
state = expectColon
|
2019-05-12 07:36:52 +02:00
|
|
|
k = b[(s + 1):i]
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectColon && b[i] == ':':
|
|
|
|
state = expectValue
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectValue && b[i] == '"':
|
|
|
|
state = expectString
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectString && b[i] == '"':
|
|
|
|
e = i
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectValue && b[i] == '[':
|
|
|
|
state = expectListClose
|
|
|
|
d++
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectListClose && d == 0 && b[i] == ']':
|
|
|
|
e = i
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectValue && b[i] == '{':
|
|
|
|
state = expectObjClose
|
|
|
|
d++
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectObjClose && d == 0 && b[i] == '}':
|
|
|
|
e = i
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectValue && (b[i] >= '0' && b[i] <= '9'):
|
|
|
|
state = expectNumClose
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectNumClose &&
|
|
|
|
((b[i] < '0' || b[i] > '9') &&
|
|
|
|
(b[i] != '.' && b[i] != 'e' && b[i] != 'E' && b[i] != '+' && b[i] != '-')):
|
|
|
|
i--
|
|
|
|
e = i
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectValue &&
|
|
|
|
(b[i] == 'f' || b[i] == 'F' || b[i] == 't' || b[i] == 'T'):
|
|
|
|
state = expectBoolClose
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectBoolClose && (b[i] == 'e' || b[i] == 'E'):
|
|
|
|
e = i
|
|
|
|
}
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
if e != 0 {
|
|
|
|
state = expectKey
|
|
|
|
cb := b[s:(e + 1)]
|
|
|
|
e = 0
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-12 07:36:52 +02:00
|
|
|
if _, ok := kmap[xxhash.Sum64(k)]; !ok {
|
2019-05-09 05:52:04 +02:00
|
|
|
continue
|
2019-05-09 01:03:18 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
if field != 0 {
|
|
|
|
if err := w.WriteByte(','); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
sk := 0
|
|
|
|
for i := 0; i < len(cb); i++ {
|
|
|
|
if cb[i] == '\n' || cb[i] == '\t' {
|
|
|
|
if _, err := w.Write(cb[sk:i]); err != nil {
|
2019-05-09 01:03:18 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-05-09 05:52:04 +02:00
|
|
|
sk = i + 1
|
2019-05-09 01:03:18 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 05:52:04 +02:00
|
|
|
|
|
|
|
if sk > 0 && sk < len(cb) {
|
|
|
|
_, err = w.Write(cb[sk:len(cb)])
|
|
|
|
} else {
|
|
|
|
_, err = w.Write(cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
field++
|
2019-05-09 01:03:18 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 05:52:04 +02:00
|
|
|
|
2019-05-09 01:03:18 +02:00
|
|
|
if item != 0 {
|
|
|
|
if err := w.WriteByte('}'); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 05:52:04 +02:00
|
|
|
|
2019-05-09 01:03:18 +02:00
|
|
|
if isList {
|
|
|
|
if err := w.WriteByte(']'); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|