jsonpath

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 7 Imported by: 0

README

jsonpath

Go Reference

Go 语言实现的 JSONPath 查询语法解析器和求值器,提供类似 gjson 的 API 风格。

完全遵循 RFC 9535 标准。

特性

  • RFC 9535 标准实现
  • 类似 gjson 的简洁 API
  • 支持过滤表达式 ?(@.price < 10)
  • 支持递归 descent ..
  • 支持数组切片 [start:end:step]
  • 内置函数:length(), count(), match(), search(), value()
  • 支持自定义函数注册
  • 零依赖,纯 Go 实现

安装

go get github.com/saltfishpr/jsonpath

快速开始

package main

import (
    "fmt"
    "github.com/saltfishpr/jsonpath"
)

func main() {
    json := `{
        "store": {
            "book": [
                {"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95},
                {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99},
                {"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99}
            ],
            "bicycle": {"color": "red", "price": 399}
        }
    }`

    // 获取所有书籍的作者
    result := jsonpath.GetMany(json, "$.store.book[*].author")
    for _, r := range result {
        fmt.Println(r.String())
    }
    // Output:
    // Nigel Rees
    // Evelyn Waugh
    // Herman Melville
}

API 用法

Get / GetMany
// Get 获取第一个匹配结果
result := jsonpath.Get(json, "$.store.book[0].author")
fmt.Println(result.String()) // Nigel Rees

// GetMany 获取所有匹配结果
results := jsonpath.GetMany(json, "$..author")
for _, r := range results {
    fmt.Println(r.String())
}
结果类型转换
result := jsonpath.Get(json, "$.store.book[0].price")

// 转换为各种类型
fmt.Println(result.Float())  // 8.95
fmt.Println(result.Int())    // 8
fmt.Println(result.String()) // 8.95
fmt.Println(result.Bool())   // true

// 检查类型
result.IsArray()  // false
result.IsObject() // false
result.IsBool()   // false
result.Exists()   // true
数组和对象操作
// 获取数组
arr := jsonpath.Get(json, "$.store.book").Array()
for i, elem := range arr {
    fmt.Printf("[%d] %s\n", i, elem.String())
}

// 获取对象
obj := jsonpath.Get(json, "$.store.bicycle").Map()
for k, v := range obj {
    fmt.Printf("%s: %v\n", k, v.Value())
}
链式查询
// 在已有结果上继续查询
result := jsonpath.Get(json, "$.store")
bicycle := result.Get("$.bicycle")
color := bicycle.Get("$.color")
fmt.Println(color.String()) // red
函数支持

支持以下 RFC 9535 标准函数:

函数 说明 示例
length(value) 返回字符串长度/数组元素个数/对象属性个数 length(@.title)
count(nodes) 统计节点数量 count(@.price[?(@ > 10)])
match(value, pattern) 完全匹配正则表达式 match(@.category, "^ref")
search(value, pattern) 搜索正则表达式 search(@.title, "Of")
value(nodes) 从节点提取单个值 value(@..isbn)

JSONPath 语法示例

表达式 描述
$.store.book[*].author 所有书籍的作者
$..author 所有作者(递归查找)
$.store.* store 下的所有值
$.store..price 所有价格值
$..book[2] 第三本书
$..book[-1] 最后一本书
$..book[:2] 前两本书
$..book[0,1] 前两本书(另一种写法)
$..book[?(@.isbn)] 有 ISBN 的书
$..book[?(@.price<10)] 价格小于 10 的书
$..* 所有成员值和数组元素

许可证

MIT License

Documentation

Overview

Package jsonpath implements JSONPath query syntax parser and evaluator with gjson-style API.

The implementation follows RFC 9535: https://www.rfc-editor.org/rfc/rfc9535.html

Index

Examples

Constants

View Source
const (
	MinSafeInteger = -(2<<52 - 1)
	MaxSafeInteger = 2<<52 - 1
)

I-JSON safe integer range

Variables

View Source
var FunctionValueNothing = Result{}

Functions

This section is empty.

Types

type CompOp

type CompOp int

CompOp is a comparison operator.

const (
	CompEq CompOp = iota // ==
	CompNe               // !=
	CompLt               // <
	CompLe               // <=
	CompGt               // >
	CompGe               // >=
)

type Comparable

type Comparable struct {
	Type          ComparableType
	Literal       *LiteralValue
	SingularQuery *SingularQuery
	FuncExpr      *FuncCall
}

Comparable is one side of a comparison.

type ComparableType

type ComparableType int

ComparableType identifies what a comparable holds.

const (
	ComparableLiteral ComparableType = iota
	ComparableSingularQuery
	ComparableFuncExpr
)

type Comparison

type Comparison struct {
	Left  *Comparable
	Op    CompOp
	Right *Comparable
}

Comparison represents a comparison expression.

type Evaluator

type Evaluator struct {
	// contains filtered or unexported fields
}

Evaluator evaluates JSONPath expressions against JSON data

func NewEvaluator

func NewEvaluator(json string, query *Query) *Evaluator

NewEvaluator creates a new evaluator for the given JSON and query

func (*Evaluator) Evaluate

func (e *Evaluator) Evaluate() []Result

Evaluate executes the query and returns all matching results

type FilterExpr

type FilterExpr struct {
	Type    FilterExprType
	Left    *FilterExpr
	Right   *FilterExpr
	Operand *FilterExpr
	Comp    *Comparison
	Test    *TestExpr
}

FilterExpr represents a filter expression.

type FilterExprType

type FilterExprType int

FilterExprType identifies the type of filter expression.

const (
	FilterLogicalOr  FilterExprType = iota // left || right
	FilterLogicalAnd                       // left && right
	FilterLogicalNot                       // !operand
	FilterParen                            // (operand)
	FilterComparison                       // comparison
	FilterTest                             // test expression
)

type FilterQuery

type FilterQuery struct {
	Relative bool // true = starts with @, false = starts with $
	Segments []*Segment
}

FilterQuery is a query used in a filter (relative or absolute)

type FuncArg

type FuncArg struct {
	Type        FuncArgType
	Literal     *LiteralValue
	FilterQuery *FilterQuery
	LogicalExpr *FilterExpr
	FuncExpr    *FuncCall
}

FuncArg represents a function argument

type FuncArgType

type FuncArgType int

FuncArgType identifies the type of function argument

const (
	FuncArgLiteral FuncArgType = iota
	FuncArgFilterQuery
	FuncArgLogicalExpr
	FuncArgFuncExpr
)

type FuncCall

type FuncCall struct {
	Name string
	Args []*FuncArg
}

FuncCall represents a function call expression

type FunctionSignature added in v0.2.0

type FunctionSignature struct {
	Name       string
	ParamTypes []FunctionValueType
	ReturnType FunctionValueType
	Handler    func(args []interface{}) (interface{}, error)
}

FunctionSignature 定义函数签名

type FunctionValueType added in v0.2.0

type FunctionValueType int

FunctionValueType 表示函数参数/返回值的类型

const (
	FunctionValueTypeValue   FunctionValueType = iota // Result / Noting
	FunctionValueTypeLogical                          // true/false
	FunctionValueTypeNodes                            // []Result
)

func (FunctionValueType) String added in v0.2.0

func (t FunctionValueType) String() string

String 返回类型的字符串表示

type JSONType

type JSONType int

JSONType represents the type of a JSON value

const (
	// JSONTypeNull is a null json value
	JSONTypeNull JSONType = iota
	// JSONTypeFalse is a json false boolean
	JSONTypeFalse
	// JSONTypeNumber is json number
	JSONTypeNumber
	// JSONTypeString is json string
	JSONTypeString
	// JSONTypeTrue is a json true boolean
	JSONTypeTrue
	// JSONTypeJSON is a raw block of JSON
	JSONTypeJSON
)

func (JSONType) String

func (t JSONType) String() string

String returns a string representation of the type

type KV

type KV struct {
	Key   string
	Value Result
}

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer tokenizes JSONPath expressions

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new lexer for the input string

func (*Lexer) NextToken

func (l *Lexer) NextToken() Token

NextToken reads and returns the next token from the input

type LiteralType

type LiteralType int
const (
	LiteralString LiteralType = iota
	LiteralNumber
	LiteralTrue
	LiteralFalse
	LiteralNull
)

type LiteralValue

type LiteralValue struct {
	Type  LiteralType
	Value string
}

LiteralValue represents a literal value in expressions

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses JSONPath expressions into an AST

type Query

type Query struct {
	Segments []*Segment
}

Query represents a JSONPath query: $ followed by segments.

func Parse

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

Parse parses a JSONPath expression string and returns an AST

type Result

type Result struct {
	// Type is the json type
	Type JSONType
	// Raw is the raw json
	Raw string
	// Str is the json string
	Str string
	// Num is the json number
	Num float64
	// Index of raw value in original json, zero means index unknown
	Index int
}

Result represents a JSON value returned from Get()

func Get

func Get(json, path string) Result

Get executes a JSONPath query and returns the first result

func GetBytes

func GetBytes(json []byte, path string) Result

GetBytes executes a JSONPath query with []byte input

func GetMany

func GetMany(json, path string) []Result

GetMany executes a JSONPath query and returns all results

Example
package main

import (
	"fmt"

	"github.com/saltfishpr/jsonpath"
)

var rfcExampleJSON = `{
  "store": {
    "book": [
      {"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95},
      {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99},
      {"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99},
      {"category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99}
    ],
    "bicycle": {"color": "red", "price": 399}
  }
}`

func main() {
	fmt.Println("1. the authors of all books in the store")
	r := jsonpath.GetMany(rfcExampleJSON, "$.store.book[*].author")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n2. all authors")
	r = jsonpath.GetMany(rfcExampleJSON, "$..author")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n3. all things in the store, which are some books and a red bicycle")
	r = jsonpath.GetMany(rfcExampleJSON, "$.store.*")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n4. the prices of everything in the store")
	r = jsonpath.GetMany(rfcExampleJSON, "$.store..price")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n5. the third book")
	r = jsonpath.GetMany(rfcExampleJSON, "$..book[2]")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n6. the third book's author")
	r = jsonpath.GetMany(rfcExampleJSON, "$..book[2].author")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n7. empty result: the third book does not have a \"publisher\" member")
	r = jsonpath.GetMany(rfcExampleJSON, "$..book[2].publisher")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n8. the last book in order")
	r = jsonpath.GetMany(rfcExampleJSON, "$..book[-1]")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n9. the first two books")
	r = jsonpath.GetMany(rfcExampleJSON, "$..book[:2]")
	for _, v := range r {
		fmt.Println(v.Value())
	}
	r = jsonpath.GetMany(rfcExampleJSON, "$..book[0,1]")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n10. all books with an ISBN number")
	r = jsonpath.GetMany(rfcExampleJSON, "$..book[?(@.isbn)]")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n11. all books cheaper than 10")
	r = jsonpath.GetMany(rfcExampleJSON, "$..book[?(@.price<10)]")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n12. all member values and array elements contained in the input value")
	r = jsonpath.GetMany(rfcExampleJSON, "$..*")
	for _, v := range r {
		fmt.Println(v.Value())
	}

}
Output:

1. the authors of all books in the store
Nigel Rees
Evelyn Waugh
Herman Melville
J. R. R. Tolkien

2. all authors
Nigel Rees
Evelyn Waugh
Herman Melville
J. R. R. Tolkien

3. all things in the store, which are some books and a red bicycle
[{"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95} {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99} {"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99} {"category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99}]
map[color:red price:399]

4. the prices of everything in the store
8.95
12.99
8.99
22.99
399

5. the third book
map[author:Herman Melville category:fiction isbn:0-553-21311-3 price:8.99 title:Moby Dick]

6. the third book's author
Herman Melville

7. empty result: the third book does not have a "publisher" member

8. the last book in order
map[author:J. R. R. Tolkien category:fiction isbn:0-395-19395-8 price:22.99 title:The Lord of the Rings]

9. the first two books
map[author:Nigel Rees category:reference price:8.95 title:Sayings of the Century]
map[author:Evelyn Waugh category:fiction price:12.99 title:Sword of Honour]
map[author:Nigel Rees category:reference price:8.95 title:Sayings of the Century]
map[author:Evelyn Waugh category:fiction price:12.99 title:Sword of Honour]

10. all books with an ISBN number
map[author:Herman Melville category:fiction isbn:0-553-21311-3 price:8.99 title:Moby Dick]
map[author:J. R. R. Tolkien category:fiction isbn:0-395-19395-8 price:22.99 title:The Lord of the Rings]

11. all books cheaper than 10
map[author:Nigel Rees category:reference price:8.95 title:Sayings of the Century]
map[author:Herman Melville category:fiction isbn:0-553-21311-3 price:8.99 title:Moby Dick]

12. all member values and array elements contained in the input value
map[bicycle:{"color": "red", "price": 399} book:[
      {"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95},
      {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99},
      {"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99},
      {"category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99}
    ]]
[{"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95} {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99} {"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99} {"category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99}]
map[color:red price:399]
map[author:Nigel Rees category:reference price:8.95 title:Sayings of the Century]
map[author:Evelyn Waugh category:fiction price:12.99 title:Sword of Honour]
map[author:Herman Melville category:fiction isbn:0-553-21311-3 price:8.99 title:Moby Dick]
map[author:J. R. R. Tolkien category:fiction isbn:0-395-19395-8 price:22.99 title:The Lord of the Rings]
reference
Nigel Rees
Sayings of the Century
8.95
fiction
Evelyn Waugh
Sword of Honour
12.99
fiction
Herman Melville
Moby Dick
0-553-21311-3
8.99
fiction
J. R. R. Tolkien
The Lord of the Rings
0-395-19395-8
22.99
red
399
Example (Functions)
package main

import (
	"fmt"

	"github.com/saltfishpr/jsonpath"
)

var rfcExampleJSON = `{
  "store": {
    "book": [
      {"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95},
      {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99},
      {"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99},
      {"category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99}
    ],
    "bicycle": {"color": "red", "price": 399}
  }
}`

func main() {
	// length() 函数示例 - 计算值的长度
	fmt.Println("1. length() - 计算数组元素的个数")
	r := jsonpath.GetMany(rfcExampleJSON, "$.store.book[?length(@.category) == 9]")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n2. length() - 筛选有 4 个成员的对象(有 isbn 字段的书)")
	r = jsonpath.GetMany(rfcExampleJSON, "$.store.book[?length(@) == 4]")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	// count() 函数示例 - 计算节点列表中的节点数量
	fmt.Println("\n3. count() - 检查所有属性数量是否等于 2")
	r = jsonpath.GetMany(rfcExampleJSON, "$.store[?count(@.*) == 2]")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	// value() 函数示例 - 将节点列表转换为值
	fmt.Println("\n4. value() - 获取 descendant nodes 中唯一值为 'red' 的节点")
	r = jsonpath.GetMany(rfcExampleJSON, "$.store[?value(@..color) == \"red\"]")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	// 添加更多示例数据的 JSON
	functionsExampleJSON := `{
		"users": [
			{"name": "Bob", "email": "[email protected]", "role": "admin"},
			{"name": "Alice", "email": "[email protected]", "role": "user"},
			{"name": "Rob", "email": "[email protected]", "role": "user"}
		],
		"products": [
			{"id": "A001", "name": "Apple", "category": "fruit"},
			{"id": "B002", "name": "Banana", "category": "fruit"},
			{"id": "C003", "name": "Carrot", "category": "vegetable"}
		],
		"dates": ["2024-01-15", "2024-02-20", "2024-03-25"]
	}`

	// match() 函数示例 - 完全匹配正则表达式
	fmt.Println("\n5. match() - 匹配以 2024-02 开头的日期")
	r = jsonpath.GetMany(functionsExampleJSON, "$.dates[?match(@, '2024-02-..')]")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n6. match() - 匹配以 example.com 结尾的邮箱")
	r = jsonpath.GetMany(functionsExampleJSON, "$.users[?match(@.email, '.*@example.com')]")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	// search() 函数示例 - 搜索包含匹配正则表达式的子串
	fmt.Println("\n7. search() - 名字包含 [BR]ob 的用户")
	r = jsonpath.GetMany(functionsExampleJSON, "$.users[?search(@.name, '[BR]ob')]")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n8. search() - ID 包含数字 0 的产品")
	r = jsonpath.GetMany(functionsExampleJSON, "$.products[?search(@.id, '0')]")
	for _, v := range r {
		fmt.Println(v.Value())
	}

	fmt.Println("\n9. search() - 类别包含 'e' 的产品")
	r = jsonpath.GetMany(functionsExampleJSON, "$.products[?search(@.category, 'e')]")
	for _, v := range r {
		fmt.Println(v.Value())
	}

}
Output:

1. length() - 计算数组元素的个数
map[author:Nigel Rees category:reference price:8.95 title:Sayings of the Century]

2. length() - 筛选有 4 个成员的对象(有 isbn 字段的书)
map[author:Nigel Rees category:reference price:8.95 title:Sayings of the Century]
map[author:Evelyn Waugh category:fiction price:12.99 title:Sword of Honour]

3. count() - 检查所有属性数量是否等于 2
map[color:red price:399]

4. value() - 获取 descendant nodes 中唯一值为 'red' 的节点
map[color:red price:399]

5. match() - 匹配以 2024-02 开头的日期
2024-02-20

6. match() - 匹配以 example.com 结尾的邮箱
map[email:[email protected] name:Bob role:admin]
map[email:[email protected] name:Alice role:user]
map[email:[email protected] name:Rob role:user]

7. search() - 名字包含 [BR]ob 的用户
map[email:[email protected] name:Bob role:admin]
map[email:[email protected] name:Rob role:user]

8. search() - ID 包含数字 0 的产品
map[category:fruit id:A001 name:Apple]
map[category:fruit id:B002 name:Banana]
map[category:vegetable id:C003 name:Carrot]

9. search() - 类别包含 'e' 的产品
map[category:vegetable id:C003 name:Carrot]

func GetManyBytes

func GetManyBytes(json []byte, path string) []Result

GetManyBytes executes a JSONPath query with []byte input

func (Result) Array

func (r Result) Array() []Result

Array returns the []Result representation

func (Result) Bool

func (r Result) Bool() bool

Bool returns the bool representation

func (Result) Exists

func (r Result) Exists() bool

Exists checks if the result exists

func (Result) Float

func (r Result) Float() float64

Float returns the float64 representation

func (Result) Get

func (r Result) Get(path string) Result

Get continues a query from the current result

func (Result) GetMany

func (r Result) GetMany(path string) []Result

GetMany continues a query from the current result

func (Result) Int

func (r Result) Int() int64

Int returns the int64 representation

func (Result) IsArray

func (r Result) IsArray() bool

IsArray checks if the result is a JSON array

func (Result) IsBool

func (r Result) IsBool() bool

IsBool checks if the result is a boolean

func (Result) IsObject

func (r Result) IsObject() bool

IsObject checks if the result is a JSON object

func (Result) IsString added in v0.2.0

func (r Result) IsString() bool

IsString checks if the result is a string

func (Result) Map

func (r Result) Map() map[string]Result

Map returns the map[string]Result representation

func (Result) MapKVList

func (r Result) MapKVList() []KV

func (Result) String

func (r Result) String() string

String returns the string representation

func (Result) Uint

func (r Result) Uint() uint64

Uint returns the uint64 representation

func (Result) Value

func (r Result) Value() interface{}

Value returns the Go native value representation

type Segment

type Segment struct {
	Type      SegmentType
	Selectors []*Selector
}

Segment is a child or descendant segment containing selectors.

type SegmentType

type SegmentType int

SegmentType distinguishes child vs descendant segments.

const (
	ChildSegment SegmentType = iota
	DescendantSegment
)

type Selector

type Selector struct {
	Type   SelectorType
	Name   string       // for NameSelector
	Index  int          // for IndexSelector
	Slice  *SliceParams // for SliceSelector
	Filter *FilterExpr  // for FilterSelector
}

Selector represents a single selector within a segment.

type SelectorType

type SelectorType int

SelectorType distinguishes different selector types.

const (
	NameSelector     SelectorType = iota // 'name' or "name"
	WildcardSelector                     // *
	IndexSelector                        // integer index
	SliceSelector                        // start:end:step
	FilterSelector                       // ?<logical-expr>
)

type SingularQuery

type SingularQuery struct {
	Relative bool // true = starts with @, false = starts with $
	Segments []*SingularSegment
}

SingularQuery is a query that produces at most one node

type SingularSegment

type SingularSegment struct {
	Type  SingularSegmentType
	Name  string
	Index int
}

SingularSegment is a name or index segment in a singular query

type SingularSegmentType

type SingularSegmentType int
const (
	SingularNameSegment SingularSegmentType = iota
	SingularIndexSegment
)

type SliceParams

type SliceParams struct {
	Start *int
	End   *int
	Step  *int
}

SliceParams holds start:end:step for a slice selector.

type TestExpr

type TestExpr struct {
	FilterQuery *FilterQuery
	FuncExpr    *FuncCall
}

TestExpr represents a test expression (existence or function)

type Token

type Token struct {
	Type  TokenType
	Value string
	Pos   int
}

Token represents a lexical token

type TokenType

type TokenType int

TokenType is the type of a token

const (
	TokenIllegal TokenType = iota
	TokenEOF

	// Identifiers
	TokenRoot    // $  - Root node identifier
	TokenCurrent // @  - Current node identifier

	// Operators
	TokenDot      // .  - Dot (child segment shorthand)
	TokenDotDot   // .. - Double dot (descendant segment)
	TokenLBracket // [  - Left bracket
	TokenRBracket // ]  - Right bracket
	TokenComma    // ,  - Comma
	TokenQuestion // ?  - Question mark (filter)
	TokenColon    // :  - Colon (slice)
	TokenWildcard // * - Wildcard

	// Comparison operators
	TokenEq // ==  - Equal
	TokenNe // !=  - Not equal
	TokenLt // <   - Less than
	TokenLe // <=  - Less than or equal
	TokenGt // >   - Greater than
	TokenGe // >=  - Greater than or equal

	// Logical operators
	TokenLAnd // &&  - Logical and
	TokenLOr  // ||  - Logical or
	TokenLNot // !   - Logical not

	// Parentheses
	TokenLParen // ( - Left parenthesis
	TokenRParen // ) - Right parenthesis

	// Literals
	TokenIdent  // Identifier/name/function name
	TokenNumber // Number
	TokenString // String
	TokenTrue   // true
	TokenFalse  // false
	TokenNull   // null
)

Token type constants

func (TokenType) String

func (t TokenType) String() string

String returns the string representation of TokenType

Jump to

Keyboard shortcuts

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