Documentation ¶
Index ¶
- func ApplyToAll(f Filter, w, r []byte, st int, sep []byte) ([]byte, error)
- func IsTrue(val []byte, st int) (bool, int, error)
- func NextAll(f Filter, w, r []byte, st int, sep []byte) ([]byte, int, error)
- type Array
- type Base64
- type Base64d
- type Cat
- type Comma
- type Dot
- type Dumper
- type Empty
- type Filter
- type First
- type Func
- type Index
- type Iter
- type JSONDecoder
- type Length
- type Literal
- type Map
- type MapFilter
- type ParseError
- type Pipe
- type Select
- type Slice
- type State
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyToAll ¶
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.
Types ¶
type Base64 ¶
Base64 reads decoded string, encodes it into base64 format, and returns it as a string.
type Base64d ¶
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"
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.
type Dot ¶
type Dot struct{}
Dot is a trivial filter that parses the values and returns it unchanged.
type Empty ¶
type Empty struct{}
Empty is a filter returning nothing on any input value. It parses the input value though.
type Filter ¶
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 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
type JSONDecoder ¶
type JSONDecoder struct {
Buf []byte
}
JSONDecoder reads decoded string, parses it a JSON, and returns the raw value.
type Literal ¶
type Literal []byte
Literal is a filter returning the same values on any input. It parses the input value though.
type ParseError ¶
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).