Documentation ¶
Overview ¶
Package jq provides compatibility with JQ queries for working with streams of JSON data.
Index ¶
- func BuildPipeline(ctx context.Context, query string) (pipeline.Pipeline, error)
- func Pipeline(query string) pipeline.Pipeline
- func PipelineContext(ctx context.Context, query string) pipeline.Pipeline
- func Query(data io.Reader, query string) ([]byte, error)
- func QueryContext(ctx context.Context, data io.Reader, query string) ([]byte, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildPipeline ¶ added in v1.2.2
Pipeline safely builds a pipeline that runs the JQ query against each line and maps the result to the output, returning an error if the query fails to build. The generated JQ code is run in the given context.
Internally, BuildPipeline uses github.com/itchyny/gojq to build and run the query.
func Pipeline ¶
Pipeline builds a JQ query for a pipeline that runs the query against each line and maps the result to the output. If the query fails to build, Pipeline will return a pipeline that returns an error immediately on read - to handle query build errors, use BuildPipeline instead.
Internally, Pipeline uses github.com/itchyny/gojq to build and run the query.
Example ¶
package main import ( "fmt" "strings" "go.bobheadxi.dev/streamline" "go.bobheadxi.dev/streamline/jq" ) func main() { data := strings.NewReader(`{"message": "hello"} {"message":"world"} {"message":"robert"}`) lines, err := streamline.New(data). WithPipeline(jq.Pipeline(".message")). Lines() if err != nil { fmt.Println("stream failed:", err.Error()) } fmt.Println(lines) }
Output: ["hello" "world" "robert"]
func PipelineContext ¶ added in v0.9.0
PipelineContext is the same as Pipeline, but runs the generated JQ code in the given context.
func Query ¶
Query is a utility for building and executing a JQ query against some data, such as a streamline.Stream instance.
Internally, Query uses github.com/itchyny/gojq to build and run the query.
Example ¶
package main import ( "bytes" "fmt" "strings" "go.bobheadxi.dev/streamline" "go.bobheadxi.dev/streamline/jq" "go.bobheadxi.dev/streamline/pipeline" ) func main() { data := strings.NewReader(`Loading... Still loading... { "message": "this is the real data!" }`) stream := streamline.New(data). // Pipeline to discard loading indicators WithPipeline(pipeline.Filter(func(line []byte) bool { return !bytes.Contains(line, []byte("...")) })) // stream is just an io.Reader message, err := jq.Query(stream, ".message") if err != nil { fmt.Println("query failed:", err.Error()) } fmt.Println(string(message)) }
Output: "this is the real data!"
Types ¶
This section is empty.