jq

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyToAll

func ApplyToAll(f Filter, w, r []byte, st int, 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.

func IsTrue

func IsTrue(val []byte, st int) (bool, int, error)

func 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

type Array

type Array struct {
	Filter Filter
	Buf    []byte
}

func (*Array) Next

func (f *Array) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)

type Base64

type Base64 struct {
	*base64.Encoding
	Buf []byte
}

Base64 reads decoded string, encodes it into base64 format, and returns it as a string.

func (*Base64) Next

func (f *Base64) Next(w, r []byte, st int, _ State) (_ []byte, i int, _ State, err error)

type Base64d

type Base64d struct {
	*base64.Encoding
	Buf []byte
}

Base64d reads decoded string, decodes if from base64 format, and returns it as a string.

Example
package main

import (
	"encoding/base64"
	"fmt"

	"nikand.dev/go/json/jq"
)

func main() {
	// generated by command
	// jq -nc '{key3: "value"} | {key2: (. | tojson)} | @base64 | {key1: .}'
	data := []byte(`{"key1":"eyJrZXkyIjoie1wia2V5M1wiOlwidmFsdWVcIn0ifQ=="}`)

	f := jq.NewPipe(
		jq.Index{"key1"},
		&jq.Base64d{
			Encoding: base64.StdEncoding,
		},
		&jq.JSONDecoder{},
		jq.Index{"key2"},
		&jq.JSONDecoder{},
		jq.Index{"key3"},
	)

	res, _, _, err := f.Next(nil, data, 0, nil)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%s", res)

}
Output:

"value"

func (*Base64d) Next

func (f *Base64d) Next(w, r []byte, st int, _ State) (_ []byte, i int, _ State, err error)

type Cat

type Cat struct {
	Separator []byte
}

func (Cat) Next

func (f Cat) Next(w, r []byte, st int, _ State) (_ []byte, i int, _ State, err error)

type 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.

func NewComma

func NewComma(fs ...Filter) *Comma

func (*Comma) Next

func (f *Comma) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)

type Dot

type Dot struct{}

Dot is a trivial filter that parses the values and returns it unchanged.

func (Dot) Next

func (f Dot) Next(w, r []byte, st int, state State) ([]byte, int, State, error)

type Dumper

type Dumper func(w, r []byte, st, end int)

func (Dumper) Next

func (f Dumper) Next(w, r []byte, st int, state State) ([]byte, int, State, error)

type Empty

type Empty struct{}

Empty is a filter returning nothing on any input value. It parses the input value though.

func (Empty) Next

func (f Empty) Next(w, r []byte, st int, _ State) (_ []byte, i int, _ State, err error)

type 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. It a filter needs to carry a state it returns it from the first call and expects to get 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 for example.

Filter also may have more than one result, iterating over long array for 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 state back to the Filter. Second return value is a position where parsing ended, it must be passed back unchanged when iterating over result values. If returned state is nil, there are no more values to return.

type First

type First struct{}

First returns at most one value of the input.

func (First) Next

func (f First) Next(w, r []byte, st int, state State) ([]byte, int, State, error)

type Func

type Func func(w, r []byte, st int, state State) ([]byte, int, State, error)

func (Func) Next

func (f Func) Next(w, r []byte, st int, state State) ([]byte, int, State, error)

type Index

type Index []interface{}

Index returns given Object key value or Array element at the index. Supported index types: string (object key) and int (array index).

Example
package main

import (
	"fmt"

	"nikand.dev/go/json/jq"
)

func main() {
	data := []byte(`{"key0":"skip it", "key1": {"next_key": ["array", null, {"obj":"val"}, "trailing element"]}}  "next"`)

	f := jq.Index{"key1", "next_key", 2} // string keys and int array indexes are supported

	var res []byte // reusable buffer
	var i int      // start index

	// Most filters only parse single value and return index where the value ended.
	// Use jq.ApplyToAll(f, res[:0], data, 0, []byte("\n")) to process all values in a buffer.
	res, i, _, err := f.Next(res[:0], data, i, nil)
	if err != nil {
		// i is an index in a source buffer where the error occurred.
	}

	fmt.Printf("value: %s\n", res)
	fmt.Printf("final position: %d of %d\n", i, len(data)) // object was parsed to the end to be able to read next
	_ = i < len(data)                                      // but not the next value

}
Output:

value: {"obj":"val"}
final position: 92 of 100

func (Index) Next

func (f Index) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)

type Iter

type Iter struct{}

func (Iter) Next

func (f Iter) Next(w, r []byte, st int, state State) ([]byte, int, State, error)

type JSONDecoder

type JSONDecoder struct {
	Buf []byte
}

JSONDecoder reads decoded string, parses it a JSON, and returns the raw value.

func (*JSONDecoder) Next

func (f *JSONDecoder) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)

type Length

type Length struct{}

func (Length) Next

func (f Length) Next(w, r []byte, st int, state State) ([]byte, int, State, error)

type Literal

type Literal []byte

Literal is a filter returning the same values on any input. It parses the input value though.

func (Literal) Next

func (f Literal) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)

type Map

type Map struct {
	Filter Filter
	Buf    []byte
	Values bool
}

func (*Map) Next

func (f *Map) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)

type MapFilter

type MapFilter interface {
	Next(w, r []byte, st int, state State) ([]byte, []byte, int, State, error)
}

type ParseError

type ParseError struct {
	Err error
	Pos int
}

ParseError contains error and position returned by json.Decoder.

func (ParseError) Error

func (e ParseError) Error() string

func (ParseError) Unwrap

func (e ParseError) Unwrap() error

type 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).

func NewPipe

func NewPipe(fs ...Filter) *Pipe

func (*Pipe) Next

func (f *Pipe) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)

type Select

type Select struct {
	Filter Filter
	Buf    []byte
}

func (*Select) Next

func (f *Select) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)

type Slice

type Slice struct {
	L, R   int
	Circle bool
	Buf    []byte
}

func (*Slice) Next

func (f *Slice) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error)

type State

type State interface{}

State is a general state of a Filter stored outside of it. 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 position returned with the state.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL