gojq

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2019 License: MIT Imports: 9 Imported by: 472

README

gojq Travis Build Status

Yet another Go implementation of jq.

Usage

 $ echo '{"foo": 128}' | gojq '.foo'
128
 $ echo '{"a": {"b": 42}}' | gojq '.a.b'
42
 $ echo '{"id": "sample", "10": {"b": 42}}' | gojq '{(.id): .["10"].b}'
{
  "sample": 42
}
 $ echo '[{"id":1},{"id":2},{"id":3}]' | gojq '.[] | .id'
1
2
3

Nice error messages.

 $ echo '[1,2,3]' | gojq  '.foo & .bar'
gojq: invalid query: .foo & .bar
    .foo & .bar
         ^  unexpected token "&"
 $ echo '{"foo": { bar: [] } }' | gojq '.'
gojq: invalid json: invalid character 'b' looking for beginning of object key string
    {"foo": { bar: [] } }
              ^

Installation

Homebrew
brew install itchyny/tap/gojq
Build from source
go get -u github.com/itchyny/gojq/cmd/gojq

Bug Tracker

Report bug at Issues・itchyny/gojq - GitHub.

Author

itchyny (https://github.com/itchyny)

License

This software is released under the MIT License, see LICENSE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AndExpr added in v0.1.0

type AndExpr struct {
	Left  *Compare `@@`
	Right []struct {
		Op    Operator `@"and"`
		Right *Compare `@@`
	} `@@*`
}

AndExpr ...

type Arith added in v0.1.0

type Arith struct {
	Left  *Factor `( @@`
	Right []struct {
		Op    Operator `@("+" | "-")`
		Right *Factor  `@@`
	} `@@* )`
}

Arith ...

type Array

type Array struct {
	Pipe *Pipe `"[" @@? "]"`
}

Array ...

type ArrayIndex

type ArrayIndex struct {
	Index   *Pipe `"." "[" ( @@`
	IsSlice bool  `( @":"`
	End     *Pipe `@@? )? | ":" @@ ) "]"`
}

ArrayIndex ...

type Comma

type Comma struct {
	Exprs []*Expr `@@ ("," @@)*`
}

Comma ...

type Compare added in v0.1.0

type Compare struct {
	Left  *Arith `@@`
	Right *struct {
		Op    Operator `@CompareOp`
		Right *Arith   `@@`
	} `@@?`
}

Compare ...

type Expr added in v0.1.0

type Expr struct {
	Logic *Logic `  @@`
	If    *If    `| @@`
}

Expr ...

type Factor added in v0.1.0

type Factor struct {
	Left  *Term `@@`
	Right []struct {
		Op    Operator `@("*" | "/" | "%")`
		Right *Term    `@@`
	} `@@*`
}

Factor ...

type Func added in v0.1.0

type Func struct {
	Name string  `@Ident`
	Args []*Pipe `( "(" @@ (";" @@)* ")" )?`
}

Func ...

type FuncDef added in v0.1.0

type FuncDef struct {
	Name string   `"def" @Ident`
	Args []string `("(" @Ident (";" @Ident)* ")")? ":"`
	Body *Query   `@@ ";"`
}

FuncDef ...

type If added in v0.1.0

type If struct {
	Cond *Pipe `"if" @@`
	Then *Pipe `"then" @@`
	Elif []struct {
		Cond *Pipe `"elif" @@`
		Then *Pipe `"then" @@`
	} `@@*`
	Else *Pipe `("else" @@)? "end"`
}

If ...

type Logic added in v0.1.0

type Logic struct {
	Left  *AndExpr `@@`
	Right []struct {
		Op    Operator `@"or"`
		Right *AndExpr `@@`
	} `@@*`
}

Logic ...

type Object

type Object struct {
	KeyVals []struct {
		Key     string  `( ( ( @Ident | @String )`
		Pipe    *Pipe   `| "(" @@ ")" ) ":"`
		Val     *Expr   `@@`
		KeyOnly *string `| ( @Ident | @String ) )`
	} `"{" (@@ ("," @@)*)? "}"`
}

Object ...

type ObjectIndex

type ObjectIndex struct {
	Name string `"." ( @Ident | "[" @String "]" )`
}

ObjectIndex ...

type Operator added in v0.1.0

type Operator int

Operator ...

const (
	OpAdd Operator = iota
	OpSub
	OpMul
	OpDiv
	OpMod
	OpEq
	OpNe
	OpGt
	OpLt
	OpGe
	OpLe
	OpAnd
	OpOr
)

Operators ...

func (*Operator) Capture added in v0.1.0

func (op *Operator) Capture(s []string) error

Capture implements participle.Capture.

func (Operator) Eval added in v0.1.0

func (op Operator) Eval(l, r interface{}) interface{}

Eval the expression.

func (Operator) String added in v0.1.0

func (op Operator) String() string

String implements Stringer.

type Pipe

type Pipe struct {
	Commas []*Comma `@@ ("|" @@)*`
}

Pipe ...

type Query

type Query struct {
	FuncDefs []*FuncDef `@@*`
	Pipe     *Pipe      `@@?`
}

Query ...

func Parse added in v0.1.0

func Parse(src string) (*Query, error)

Parse parses a query.

func (*Query) Run added in v0.1.0

func (q *Query) Run(v interface{}) <-chan interface{}

Run query.

type Suffix

type Suffix struct {
	ObjectIndex *ObjectIndex `  @@`
	ArrayIndex  *ArrayIndex  `| @@`
	Array       *Array       `| @@`
	Optional    bool         `| @"?"`
}

Suffix ...

type Term

type Term struct {
	ObjectIndex *ObjectIndex `( @@`
	ArrayIndex  *ArrayIndex  `| @@`
	Identity    bool         `| @"."`
	Recurse     bool         `| @".."`
	Func        *Func        `| @@`
	Object      *Object      `| @@`
	Array       *Array       `| @@`
	Number      *float64     `| @Number`
	Unary       *struct {
		Op   Operator `@("+" | "-")`
		Term *Term    `@@`
	} `| @@`
	String     *string   `| @String`
	Pipe       *Pipe     `| "(" @@ ")" )`
	SuffixList []*Suffix `@@*`
}

Term ...

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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