jq

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package jq provides compatibility with JQ queries for working with streams of JSON data.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildPipeline added in v1.2.2

func BuildPipeline(ctx context.Context, query string) (pipeline.Pipeline, error)

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

func Pipeline(query string) pipeline.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

func PipelineContext(ctx context.Context, query string) pipeline.Pipeline

PipelineContext is the same as Pipeline, but runs the generated JQ code in the given context.

func Query

func Query(data io.Reader, query string) ([]byte, error)

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!"

func QueryContext added in v0.9.0

func QueryContext(ctx context.Context, data io.Reader, query string) ([]byte, error)

QueryContext is the same as Query, but runs the generated JQ code in the given context.

Types

This section is empty.

Jump to

Keyboard shortcuts

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