1
1
mirror of https://github.com/wader/fq.git synced 2024-12-23 13:22:58 +03:00
fq/format/json/jsonl.go
Mattias Wadman 8e0dde03d0 decode: Support multiple format args and some rename and refactor
This will allow passing both cli options and format options to sub decoder.
Ex: pass keylog option to a tls decoder when decoding a pcap.
Ex: pass decode options to a format inside a http body inside a pcap.

Add ArgAs method to lookup argument based on type. This also makes the format
decode function have same signature as sub decoders in the decode API.

This change decode.Format a bit:
DecodeFn is now just func(d *D) any
DecodeInArg renamed to DefaultInArg
2023-02-18 21:38:51 +01:00

47 lines
908 B
Go

package json
import (
"bytes"
"embed"
"github.com/wader/fq/format"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
)
//go:embed jsonl.jq
var jsonlFS embed.FS
// TODO: not strictly JSONL, allows any whitespace between
func init() {
interp.RegisterFormat(decode.Format{
Name: format.JSONL,
Description: "JavaScript Object Notation Lines",
ProbeOrder: format.ProbeOrderTextFuzzy,
Groups: []string{format.PROBE},
DecodeFn: decodeJSONL,
Functions: []string{"_todisplay"},
})
interp.RegisterFS(jsonlFS)
interp.RegisterFunc0("to_jsonl", toJSONL)
}
func decodeJSONL(d *decode.D) any {
return decodeJSONEx(d, true)
}
func toJSONL(i *interp.Interp, c []any) any {
cj := makeEncoder(ToJSONOpts{})
bb := &bytes.Buffer{}
for _, v := range c {
if err := cj.Marshal(v, bb); err != nil {
return err
}
bb.WriteByte('\n')
}
return bb.String()
}