query

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: May 2, 2023 License: BSD-3-Clause Imports: 6 Imported by: 23

README

go-json-query

Go package for querying and filter JSON documents using tidwall/gjson-style paths and regular expressions for testing values.

Documentation

Go Reference

Important

Documentation is incomplete.

Example

import (
	"context"
	"flag"
	"fmt"
	"github.com/aaronland/go-json-query"
	"io"
	"os"
	"strings"
)

func main() {

	var queries query.QueryFlags
	flag.Var(&queries, "query", "One or more {PATH}={REGEXP} parameters for filtering records.")

	valid_modes := strings.Join([]string{query.QUERYSET_MODE_ALL, query.QUERYSET_MODE_ANY}, ", ")
	desc_modes := fmt.Sprintf("Specify how query filtering should be evaluated. Valid modes are: %s", valid_modes)

	query_mode := flag.String("query-mode", query.QUERYSET_MODE_ALL, desc_modes)

	flag.Parse()

	paths := flag.Args()

	qs := &query.QuerySet{
		Queries: queries,
		Mode:    *query_mode,
	}

	ctx := context.Background()

	for _, path := range paths {

		fh, _ := os.Open(path)
		defer fh.Close()

		body, _ := io.ReadAll(fh)

		matches, _ := query.Matches(ctx, qs, body)

		fmt.Printf("%s\t%t\n", path, matches)
	}
}

See also

Documentation

Overview

package provides a lightweight interface for querying and filter JSON documents using tidwall/gjson-style paths and regular expressions for testing values.

Example

import (
	"context"
	"flag"
	"fmt"
	"github.com/aaronland/go-json-query"
	"io"
	"os"
	"strings"
)

func main() {

	var queries query.QueryFlags
	flag.Var(&queries, "query", "One or more {PATH}={REGEXP} parameters for filtering records.")

	valid_modes := strings.Join([]string{query.QUERYSET_MODE_ALL, query.QUERYSET_MODE_ANY}, ", ")
	desc_modes := fmt.Sprintf("Specify how query filtering should be evaluated. Valid modes are: %s", valid_modes)

	query_mode := flag.String("query-mode", query.QUERYSET_MODE_ALL, desc_modes)

	flag.Parse()

	paths := flag.Args()

	qs := &query.QuerySet{
		Queries: queries,
		Mode:    *query_mode,
	}

	ctx := context.Background()

	for _, path := range paths {

		fh, _ := os.Open(path)
		defer fh.Close()

		body, _ := io.ReadAll(fh)

		matches, _ := query.Matches(ctx, qs, body)

		fmt.Printf("%s\t%t\n", path, matches)
	}
}

Index

Constants

View Source
const QUERYSET_MODE_ALL string = "ALL"

QUERYSET_MODE_ALL is a flag to signal that only all matches in a QuerySet needs to be successful.

View Source
const QUERYSET_MODE_ANY string = "ANY"

QUERYSET_MODE_ANY is a flag to signal that only one match in a QuerySet needs to be successful.

View Source
const SEP string = "="

The separator string used to distinguish {PATH}={REGULAR_EXPRESSION} strings.

Variables

This section is empty.

Functions

func Matches

func Matches(ctx context.Context, qs *QuerySet, body []byte) (bool, error)

Matches compares the set of queries in 'qs' against a JSON record ('body') and returns true or false depending on whether or not some or all of those queries are matched successfully.

Types

type Query

type Query struct {
	// A valid tidwall/gjson query path.
	Path string
	// A valid regular expression.
	Match *regexp.Regexp
}

Query is an atomic query to perform against a JSON document.

type QueryFlags

type QueryFlags []*Query

QueryFlags holds one or more Query instances that are created using {PATH}={REGULAR_EXPRESSION} strings.

func (*QueryFlags) Set

func (m *QueryFlags) Set(value string) error

Parse a {PATH}={REGULAR_EXPRESSION} string and store it as one of a set of Query instances.

func (*QueryFlags) String

func (m *QueryFlags) String() string

Return the string value of the set of Query instances. Currently returns "".

type QuerySet

type QuerySet struct {
	// A set of Query instances
	Queries []*Query
	// A string flag representing how query results should be interpreted.
	Mode string
}

QuerySet is a struct containing one or more Query instances and flags for how the results of those queries should be interpreted.

Directories

Path Synopsis
cmd
matches
Read one or more files and test whether their contents match one or more query parameters.
Read one or more files and test whether their contents match one or more query parameters.

Jump to

Keyboard shortcuts

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