peekcty

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2021 License: MIT Imports: 10 Imported by: 0

README

JSONPath for go-cty: peekty

Go Test Go Reference

peekcty lets you iterate over cty datastructures using JSONPath syntax.

Note: go-cty is the serialization / typesystem library for Go powering HCL, Terraform, zclconf.

Example

Given text_fixture_cars.json:

import "github.com/clean8s/peekcty"

func demo() {
  p, err := peekcty.NewPath("$..has")
  fmt.Println(p.Search(carExample))
}

Prints:

".carOwners.A.has" => []{StringVal("Honda Accord"), StringVal("VW Up"), StringVal("Porsche 911")}
".carOwners.B.has" => []{StringVal("Renault Clio"), StringVal("Jaguar F-Type"), StringVal("Dodge Viper")}
".cars[0].has" => []{StringVal("4 doors")}

Implementation

It's based on Kubernetes/kubectl's implementation here. With two differences:

  • it doesn't require templates or range blocks
  • it operates on cty.Value instead of reflect.Value

You can use all features except for filters:

  • $[0, 1]
  • $.field
  • $.wildcard[*]
  • $.x.y..recursive
  • m[1:], slice2[:2], slice3[1:5]

LICENSE

Licensed under MIT.

This is an extension not officially affiliated with the cty library by Martin Atkins.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSyntax = errors.New("invalid syntax")
)
View Source
var NodeTypeName = map[NodeType]string{
	NodeText:       "NodeText",
	NodeArray:      "NodeArray",
	NodeList:       "NodeList",
	NodeField:      "NodeField",
	NodeIdentifier: "NodeIdentifier",
	NodeFilter:     "NodeFilter",
	NodeInt:        "NodeInt",
	NodeFloat:      "NodeFloat",
	NodeWildcard:   "NodeWildcard",
	NodeRecursive:  "NodeRecursive",
	NodeUnion:      "NodeUnion",
	NodeBool:       "NodeBool",
}

Functions

func PrettyCtyPath

func PrettyCtyPath(path cty.Path) string

Pretty prints a cty.Path into a string

Example:

PrettyCtyPath(c.IndexInt(3)) == ".[3]"

func UnquoteExtend

func UnquoteExtend(s string) (string, error)

UnquoteExtend is almost same as strconv.Unquote(), but it support parse single quotes as a string

Types

type ArrayNode

type ArrayNode struct {
	NodeType
	Params [3]ParamsEntry // start, end, step
}

ArrayNode holds start, end, step information for array index selection

func (*ArrayNode) String

func (a *ArrayNode) String() string

type BoolNode

type BoolNode struct {
	NodeType
	Value bool
}

BoolNode holds bool value

func (*BoolNode) String

func (b *BoolNode) String() string

type FieldNode

type FieldNode struct {
	NodeType
	Value string
}

FieldNode holds field of struct

func (*FieldNode) String

func (f *FieldNode) String() string

type FilterNode

type FilterNode struct {
	NodeType
	Left     *ListNode
	Right    *ListNode
	Operator string
}

FilterNode holds operand and operator information for filter

func (*FilterNode) String

func (f *FilterNode) String() string

type FloatNode

type FloatNode struct {
	NodeType
	Value float64
}

FloatNode holds float value

func (*FloatNode) String

func (i *FloatNode) String() string

type IdentifierNode

type IdentifierNode struct {
	NodeType
	Name string
}

IdentifierNode holds an identifier

func (*IdentifierNode) String

func (f *IdentifierNode) String() string

type IntNode

type IntNode struct {
	NodeType
	Value int
}

IntNode holds integer value

func (*IntNode) String

func (i *IntNode) String() string

type JSONPath

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

func NewPath

func NewPath(jsonPath string) (*JSONPath, error)

NewPath creates a new JSONPath with the given name.

func (*JSONPath) EnableJSONOutput

func (j *JSONPath) EnableJSONOutput(v bool)

EnableJSONOutput changes the PrintResults behavior to return a JSON array of results

func (*JSONPath) Eval

func (j *JSONPath) Eval(data cty.Value) ([]cty.Value, []cty.Path, error)

Returns a list of matched lists and paths based on a JSON path.

func (*JSONPath) EvalRaw

func (j *JSONPath) EvalRaw(data cty.Value) ([][]cty.Value, error)

EvalRaw is like Eval() without extra processing (cty.Path and unmarking)

func (*JSONPath) Search

func (j *JSONPath) Search(data cty.Value) SearchResult

Given a JSON Path, this lets you search a cty.Value and return a result struct containg Value/Path pairs. There may not be 1:1 mapping between both (due to recursive calls).

Instead, it's better to iterate the paths and call .Apply(value) on them.

type ListNode

type ListNode struct {
	NodeType
	Nodes []Node // The element nodes in lexical order.
}

ListNode holds a sequence of nodes.

func (*ListNode) String

func (l *ListNode) String() string

type Node

type Node interface {
	Type() NodeType
	String() string
}

type NodeType

type NodeType int

NodeType identifies the type of a parse tree node.

const (
	NodeText NodeType = iota
	NodeArray
	NodeList
	NodeField
	NodeIdentifier
	NodeFilter
	NodeInt
	NodeFloat
	NodeWildcard
	NodeRecursive
	NodeUnion
	NodeBool
)

func (NodeType) String

func (t NodeType) String() string

func (NodeType) Type

func (t NodeType) Type() NodeType

Type returns itself and provides an easy default implementation

type ParamsEntry

type ParamsEntry struct {
	Value   int
	Known   bool // whether the value is known when parse it
	Derived bool
}

ParamsEntry holds param information for ArrayNode

type Parser

type Parser struct {
	Root *ListNode
	// contains filtered or unexported fields
}

func Parse

func Parse(text string) (*Parser, error)

Parse parsed the given text and return a node Parser. If an error is encountered, parsing stops and an empty Parser is returned with the error.

func (*Parser) Parse

func (p *Parser) Parse(text string) error

type RecursiveNode

type RecursiveNode struct {
	NodeType
}

RecursiveNode means a recursive descent operator

func (*RecursiveNode) String

func (r *RecursiveNode) String() string

type SearchResult

type SearchResult struct {
	Values []cty.Value
	Paths  []cty.Path
	// contains filtered or unexported fields
}

func (SearchResult) String

func (s SearchResult) String() (out string)

type TextNode

type TextNode struct {
	NodeType
	Text string // The text; may span newlines.
}

TextNode holds plain text.

func (*TextNode) String

func (t *TextNode) String() string

type UnionNode

type UnionNode struct {
	NodeType
	Nodes []*ListNode
}

UnionNode is union of ListNode

func (*UnionNode) String

func (u *UnionNode) String() string

type WildcardNode

type WildcardNode struct {
	NodeType
}

WildcardNode means a wildcard

func (*WildcardNode) String

func (i *WildcardNode) String() string

Jump to

Keyboard shortcuts

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