jq
v0.10.0
Documentation
pkg.go.dev ↗Variables
ErrUnsupportedQueryFilter
var ErrUnsupportedQueryFilter = errors.New("unsupported query filter")
Functions
ApplyToAll
func ApplyToAll(f Filter, w, r, sep []byte) ([]byte, error)
ApplyToAll applies the filter f to all the values from the input buffer and returns all the results separated by sep sequence ('\n' by default).
NextAll reads only the first value from the input and processes it while ApplyToAll does that for each input value until end of buffer reached.
IsTrue
func IsTrue(val []byte, st int) (bool, int, error)
NextAll
func NextAll(f Filter, w, r []byte, st int, sep []byte) ([]byte, int, error)
NextAll applies filter to the first value in input and return all the result values.
NextAll reads only the first value from the input and processes it while ApplyToAll does that for each input value until end of buffer reached.
Types
Array
type Array struct {
Filter Filter
Buf []byte
}
NewArray
func NewArray(of Filter) *Array
Array.Next
func (f *Array) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)
Base64
type Base64 struct {
*base64.Encoding
Buf []byte
}
Base64 reads decoded string, encodes it into base64 format, and returns it as a string.
Base64.Next
func (f *Base64) Next(w, r []byte, st int, _ State) (_ []byte, i int, _ State, err error)
Base64d
type Base64d struct {
*base64.Encoding
Buf []byte
}
Base64d reads decoded string, decodes if from base64 format, and returns it as a string.
Example
{
data := []byte(`{"key1":"eyJrZXkyIjoie1wia2V5M1wiOlwidmFsdWVcIn0ifQ=="}`)
f := jq.NewPipe(
jq.Key("key1"),
&jq.Base64d{
Encoding: base64.StdEncoding,
},
&jq.JSONDecoder{},
jq.Key("key2"),
&jq.JSONDecoder{},
jq.Key("key3"),
)
res, _, _, err := f.Next(nil, data, 0, nil)
if err != nil {
panic(err)
}
fmt.Printf("%s", res)
}
Output:
"value"
Base64d.Next
func (f *Base64d) Next(w, r []byte, st int, _ State) (_ []byte, i int, _ State, err error)
Cat
type Cat struct {
Separator []byte
}
Cat.Next
func (f Cat) Next(w, r []byte, st int, _ State) (_ []byte, i int, _ State, err error)
Comma
type Comma struct {
Filters []Filter
// contains filtered or unexported fields
}
Comma passes the same input to each of the Filters and combines returned values.
NewComma
func NewComma(fs ...Filter) *Comma
Comma.Next
func (f *Comma) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)
Dot
type Dot struct{}
Dot is a trivial filter that parses the values and returns it unchanged.
Dot.Next
func (f Dot) Next(w, r []byte, st int, state State) ([]byte, int, State, error)
Dumper
type Dumper func(w, r []byte, st, end int)
Dumper.Next
func (f Dumper) Next(w, r []byte, st int, state State) ([]byte, int, State, error)
Empty
type Empty struct{}
Empty is a filter returning nothing on any input value. It parses the input value though.
Empty.Next
func (f Empty) Next(w, r []byte, st int, _ State) (_ []byte, i int, _ State, err error)
Equal
type Equal struct {
L, R Filter
Not bool
Buf []byte
}
NewEqual
func NewEqual(l, r Filter) *Equal
NewNotEqual
func NewNotEqual(l, r Filter) *Equal
Equal.Next
func (f *Equal) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)
Filter
type Filter interface {
Next(w, r []byte, st int, state State) ([]byte, int, State, error)
}
Filter is a general filter interface. Filter parses single value from r buffer starting from st index, processes it, appends result to the w buffer and returns it as the first return parameter.
Filters are stateless, but some of them may have temporary buffer they use for internal processing. If a filter needs to carry a state, it returns it from the first call and expects to receive it back on the next call. The first time the filter is called on a new value state must be nil.
Filter may not add anything to w buffer if there is an empty result, iterating over empty array is an example.
Filter also may have more than one result, iterating over array with more than one element is an example. In that case Next returns only one value at a time and non-nil state. Non-nil returned state means there are more values possible, but not guaranteed. To get them call the Next once again passing returned index and state back to the Filter. Thus the returned index is a part of the state. If returned state is nil, there are no more values to return.
Filter may add no value to w and return non-nil state as many times as it needs.
First
type First struct{}
First returns at most one value of the input.
First.Next
func (f First) Next(w, r []byte, st int, state State) ([]byte, int, State, error)
Func
type Func func(w, r []byte, st int, state State) ([]byte, int, State, error)
Func.Next
func (f Func) Next(w, r []byte, st int, state State) ([]byte, int, State, error)
Index
type Index int
Index.Next
func (f Index) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)
Iter
type Iter struct{}
Iter.Next
func (f Iter) Next(w, r []byte, st int, state State) ([]byte, int, State, error)
JSONDecoder
type JSONDecoder struct {
Buf []byte
}
JSONDecoder reads decoded string, parses it a JSON, and returns the raw value.
JSONDecoder.Next
func (f *JSONDecoder) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)
Key
type Key string
Key.Next
func (f Key) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)
Length
type Length struct{}
Length.Next
func (f Length) Next(w, r []byte, st int, state State) ([]byte, int, State, error)
Literal
type Literal []byte
Literal is a filter returning the same values on any input. It parses the input value though.
Literal.Next
func (f Literal) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)
Map
type Map struct {
Filter Filter
Buf []byte
Values bool
}
NewMap
func NewMap(f Filter) *Map
NewMapValues
func NewMapValues(f Filter) *Map
Map.Next
func (f *Map) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)
Object
type Object struct {
Keys []ObjectKey
// contains filtered or unexported fields
}
NewObject
func NewObject(keys ...ObjectKey) *Object
Object.Next
func (f *Object) Next(w, r []byte, st int, state State) ([]byte, int, State, error)
ObjectKey
type ObjectKey struct {
Key string
Filter Filter
}
ParseError
type ParseError struct {
Err error
Pos int
}
ParseError contains error and position returned by json.Decoder.
ParseError.Error
func (e ParseError) Error() string
ParseError.Unwrap
func (e ParseError) Unwrap() error
Pipe
type Pipe struct {
Filters []Filter
// contains filtered or unexported fields
}
Pipe passes the input to the first Filter. Then it passes the prevoius filter result(s) ot the input of the next Filter. And then it returns the last filter result(s).
NewPipe
func NewPipe(fs ...Filter) *Pipe
Pipe.Next
func (f *Pipe) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)
Query
type Query struct {
Filters []any
// contains filtered or unexported fields
}
Query returns given Object key value or Array element at the index. Supported index types: string (object key), int (array index), Iter (iterate over all values of Object or Array).
Example
{
data := []byte(`{"key0":"skip it", "key1": {"next_key": ["array", null, {"obj":"val"}, "trailing element"]}} "next"`)
f := jq.NewQuery("key1", "next_key", 2)
var res []byte // reusable buffer
var i int // start index
res, i, _, err := f.Next(res[:0], data, i, nil)
if err != nil {
}
fmt.Printf("value: %s\n", res)
fmt.Printf("final position: %d of %d\n", i, len(data))
_ = i < len(data)
}
Output:
value: {"obj":"val"}
final position: 92 of 100
NewQuery
func NewQuery(filters ...any) *Query
Query.Next
func (f *Query) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)
Select
type Select struct {
Filter Filter
}
NewSelect
func NewSelect(f Filter) *Select
Select.Next
func (f *Select) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)
Slice
type Slice struct {
L, R int
Circle bool
Buf []byte
}
Slice.Next
func (f *Slice) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)
State
type State interface{}
State is a state of a Filter stored externally as Filters are stateless by design.
It's opaque value for the caller and only should be used to pass it back to the same Filter with the same buffer and index returned with the state.