yqlib

package
v4.44.5 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2024 License: MIT Imports: 42 Imported by: 54

Documentation

Overview

Use the top level Evaluator or StreamEvaluator to evaluate expressions and return matches.

Index

Constants

This section is empty.

Variables

View Source
var Base64Format = &Format{"base64", []string{},
	func() Encoder { return NewBase64Encoder() },
	func() Decoder { return NewBase64Decoder() },
}
View Source
var CSVFormat = &Format{"csv", []string{"c"},
	func() Encoder { return NewCsvEncoder(ConfiguredCsvPreferences) },
	func() Decoder { return NewCSVObjectDecoder(ConfiguredCsvPreferences) },
}
View Source
var ConfiguredCsvPreferences = NewDefaultCsvPreferences()
View Source
var ConfiguredJSONPreferences = NewDefaultJsonPreferences()
View Source
var ConfiguredLuaPreferences = NewDefaultLuaPreferences()
View Source
var ConfiguredPropertiesPreferences = NewDefaultPropertiesPreferences()
View Source
var ConfiguredTsvPreferences = NewDefaultTsvPreferences()
View Source
var ConfiguredXMLPreferences = NewDefaultXmlPreferences()
View Source
var ConfiguredYamlPreferences = NewDefaultYamlPreferences()
View Source
var JSONFormat = &Format{"json", []string{"j"},
	func() Encoder { return NewJSONEncoder(ConfiguredJSONPreferences) },
	func() Decoder { return NewJSONDecoder() },
}
View Source
var LoadYamlPreferences = YamlPreferences{
	LeadingContentPreProcessing: false,
	PrintDocSeparators:          true,
	UnwrapScalar:                true,
	EvaluateTogether:            false,
}
View Source
var LuaFormat = &Format{"lua", []string{"l"},
	func() Encoder { return NewLuaEncoder(ConfiguredLuaPreferences) },
	func() Decoder { return NewLuaDecoder(ConfiguredLuaPreferences) },
}
View Source
var Now = time.Now

for unit tests

View Source
var PrettyPrintExp = `(... | (select(tag != "!!str"), select(tag == "!!str") | select(test("(?i)^(y|yes|n|no|on|off)$") | not))  ) style=""`
View Source
var PropertiesFormat = &Format{"props", []string{"p", "properties"},
	func() Encoder { return NewPropertiesEncoder(ConfiguredPropertiesPreferences) },
	func() Decoder { return NewPropertiesDecoder() },
}
View Source
var ShFormat = &Format{"", nil,
	func() Encoder { return NewShEncoder() },
	nil,
}
View Source
var ShellVariablesFormat = &Format{"shell", []string{"s", "sh"},
	func() Encoder { return NewShellVariablesEncoder() },
	nil,
}
View Source
var StringInterpolationEnabled = true
View Source
var TSVFormat = &Format{"tsv", []string{"t"},
	func() Encoder { return NewCsvEncoder(ConfiguredTsvPreferences) },
	func() Decoder { return NewCSVObjectDecoder(ConfiguredTsvPreferences) },
}
View Source
var TomlFormat = &Format{"toml", []string{},
	func() Encoder { return NewTomlEncoder() },
	func() Decoder { return NewTomlDecoder() },
}
View Source
var UriFormat = &Format{"uri", []string{},
	func() Encoder { return NewUriEncoder() },
	func() Decoder { return NewUriDecoder() },
}
View Source
var XMLFormat = &Format{"xml", []string{"x"},
	func() Encoder { return NewXMLEncoder(ConfiguredXMLPreferences) },
	func() Decoder { return NewXMLDecoder(ConfiguredXMLPreferences) },
}
View Source
var YamlFormat = &Format{"yaml", []string{"y", "yml"},
	func() Encoder { return NewYamlEncoder(ConfiguredYamlPreferences) },
	func() Decoder { return NewYamlDecoder(ConfiguredYamlPreferences) },
}

Functions

func FormatStringFromFilename added in v4.42.1

func FormatStringFromFilename(filename string) string

func GetAvailableInputFormatString added in v4.42.1

func GetAvailableInputFormatString() string

func GetAvailableOutputFormatString added in v4.42.1

func GetAvailableOutputFormatString() string

func GetLogger added in v4.16.1

func GetLogger() *logging.Logger

GetLogger returns the yq logger instance.

func InitExpressionParser added in v4.19.1

func InitExpressionParser()

func KindString

func KindString(kind Kind) string

func MapToYamlStyle added in v4.40.1

func MapToYamlStyle(original Style) yaml.Style

func NewFrontMatterHandler added in v4.11.0

func NewFrontMatterHandler(originalFilename string) frontMatterHandler

func NewWriteInPlaceHandler

func NewWriteInPlaceHandler(inputFile string) writeInPlaceHandler

func NodeContentToString added in v4.40.1

func NodeContentToString(node *CandidateNode, depth int) string

func NodeToString

func NodeToString(node *CandidateNode) string

func NodesToString

func NodesToString(collection *list.List) string

use for debugging only

func ReadDocuments added in v4.43.1

func ReadDocuments(reader io.Reader, decoder Decoder) (*list.List, error)

func SafelyCloseReader added in v4.11.0

func SafelyCloseReader(reader io.Reader)

Types

type CandidateNode

type CandidateNode struct {
	Kind  Kind
	Style Style

	Tag     string
	Value   string
	Anchor  string
	Alias   *CandidateNode
	Content []*CandidateNode

	HeadComment string
	LineComment string
	FootComment string

	Parent *CandidateNode // parent node
	Key    *CandidateNode // node key, if this is a value from a map (or index in an array)

	LeadingContent string

	Line   int
	Column int

	// when performing op against all nodes given, this will treat all the nodes as one
	// (e.g. top level cross document merge). This property does not propagate to child nodes.
	EvaluateTogether bool
	IsMapKey         bool
	// contains filtered or unexported fields
}

func (*CandidateNode) AddChild added in v4.40.1

func (n *CandidateNode) AddChild(rawChild *CandidateNode)

func (*CandidateNode) AddChildren added in v4.40.1

func (n *CandidateNode) AddChildren(children []*CandidateNode)

func (*CandidateNode) AddKeyValueChild added in v4.40.1

func (n *CandidateNode) AddKeyValueChild(rawKey *CandidateNode, rawValue *CandidateNode) (*CandidateNode, *CandidateNode)

func (*CandidateNode) AsList added in v4.14.1

func (n *CandidateNode) AsList() *list.List

func (*CandidateNode) Copy

func (n *CandidateNode) Copy() *CandidateNode

func (*CandidateNode) CopyAsReplacement added in v4.40.1

func (n *CandidateNode) CopyAsReplacement(replacement *CandidateNode) *CandidateNode

func (*CandidateNode) CopyWithoutContent added in v4.40.1

func (n *CandidateNode) CopyWithoutContent() *CandidateNode

func (*CandidateNode) CreateChild added in v4.3.2

func (n *CandidateNode) CreateChild() *CandidateNode

func (*CandidateNode) CreateReplacement added in v4.15.1

func (n *CandidateNode) CreateReplacement(kind Kind, tag string, value string) *CandidateNode

func (*CandidateNode) CreateReplacementWithComments added in v4.40.1

func (n *CandidateNode) CreateReplacementWithComments(kind Kind, tag string, style Style) *CandidateNode

func (*CandidateNode) GetDocument added in v4.40.1

func (n *CandidateNode) GetDocument() uint

func (*CandidateNode) GetFileIndex added in v4.40.1

func (n *CandidateNode) GetFileIndex() int

func (*CandidateNode) GetFilename added in v4.40.1

func (n *CandidateNode) GetFilename() string

func (*CandidateNode) GetKey

func (n *CandidateNode) GetKey() string

func (*CandidateNode) GetNicePath added in v4.16.2

func (n *CandidateNode) GetNicePath() string

func (*CandidateNode) GetPath added in v4.40.1

func (n *CandidateNode) GetPath() []interface{}

func (*CandidateNode) GetValueRep added in v4.40.1

func (n *CandidateNode) GetValueRep() (interface{}, error)

func (*CandidateNode) MarshalJSON added in v4.40.1

func (o *CandidateNode) MarshalJSON() ([]byte, error)

func (*CandidateNode) MarshalYAML added in v4.40.1

func (o *CandidateNode) MarshalYAML() (*yaml.Node, error)

func (*CandidateNode) SetDocument added in v4.40.1

func (n *CandidateNode) SetDocument(idx uint)

func (*CandidateNode) SetFileIndex added in v4.40.1

func (n *CandidateNode) SetFileIndex(idx int)

func (*CandidateNode) SetFilename added in v4.40.1

func (n *CandidateNode) SetFilename(name string)

func (*CandidateNode) SetParent added in v4.40.1

func (n *CandidateNode) SetParent(parent *CandidateNode)

func (*CandidateNode) UnmarshalGoccyYAML added in v4.40.1

func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap) error

func (*CandidateNode) UnmarshalJSON added in v4.40.1

func (o *CandidateNode) UnmarshalJSON(data []byte) error

func (*CandidateNode) UnmarshalYAML added in v4.40.1

func (o *CandidateNode) UnmarshalYAML(node *yaml.Node, anchorMap map[string]*CandidateNode) error

func (*CandidateNode) UpdateAttributesFrom

func (n *CandidateNode) UpdateAttributesFrom(other *CandidateNode, prefs assignPreferences)

func (*CandidateNode) UpdateFrom

func (n *CandidateNode) UpdateFrom(other *CandidateNode, prefs assignPreferences)

updates this candidate from the given candidate node

type Context added in v4.5.0

type Context struct {
	MatchingNodes  *list.List
	Variables      map[string]*list.List
	DontAutoCreate bool
	// contains filtered or unexported fields
}

func (*Context) ChildContext added in v4.5.0

func (n *Context) ChildContext(results *list.List) Context

func (*Context) Clone added in v4.5.1

func (n *Context) Clone() Context

func (*Context) DeepClone added in v4.13.0

func (n *Context) DeepClone() Context

func (*Context) GetDateTimeLayout added in v4.20.1

func (n *Context) GetDateTimeLayout() string

func (*Context) GetVariable added in v4.5.0

func (n *Context) GetVariable(name string) *list.List

func (*Context) ReadOnlyClone added in v4.9.2

func (n *Context) ReadOnlyClone() Context

func (*Context) SetDateTimeLayout added in v4.20.1

func (n *Context) SetDateTimeLayout(newDateTimeLayout string)

func (*Context) SetVariable added in v4.5.0

func (n *Context) SetVariable(name string, value *list.List)

func (*Context) SingleChildContext added in v4.5.0

func (n *Context) SingleChildContext(candidate *CandidateNode) Context

func (*Context) SingleReadonlyChildContext added in v4.9.2

func (n *Context) SingleReadonlyChildContext(candidate *CandidateNode) Context

func (*Context) ToString added in v4.12.2

func (n *Context) ToString() string

func (*Context) WritableClone added in v4.9.7

func (n *Context) WritableClone() Context

type CsvPreferences added in v4.41.1

type CsvPreferences struct {
	Separator rune
	AutoParse bool
}

func NewDefaultCsvPreferences added in v4.41.1

func NewDefaultCsvPreferences() CsvPreferences

func NewDefaultTsvPreferences added in v4.41.1

func NewDefaultTsvPreferences() CsvPreferences

type DataTreeNavigator

type DataTreeNavigator interface {
	// given the context and an expressionNode,
	// this will process the against the given expressionNode and return
	// a new context of matching candidates
	GetMatchingNodes(context Context, expressionNode *ExpressionNode) (Context, error)

	DeeplyAssign(context Context, path []interface{}, rhsNode *CandidateNode) error
}

func NewDataTreeNavigator

func NewDataTreeNavigator() DataTreeNavigator

type Decoder added in v4.17.1

type Decoder interface {
	Init(reader io.Reader) error
	Decode() (*CandidateNode, error)
}

func NewBase64Decoder added in v4.21.1

func NewBase64Decoder() Decoder

func NewCSVObjectDecoder added in v4.27.1

func NewCSVObjectDecoder(prefs CsvPreferences) Decoder

func NewGoccyYAMLDecoder added in v4.40.1

func NewGoccyYAMLDecoder() Decoder

func NewJSONDecoder added in v4.27.1

func NewJSONDecoder() Decoder

func NewLuaDecoder added in v4.40.1

func NewLuaDecoder(prefs LuaPreferences) Decoder

func NewPropertiesDecoder added in v4.20.1

func NewPropertiesDecoder() Decoder

func NewTomlDecoder added in v4.33.1

func NewTomlDecoder() Decoder

func NewUriDecoder added in v4.31.1

func NewUriDecoder() Decoder

func NewXMLDecoder added in v4.20.1

func NewXMLDecoder(prefs XmlPreferences) Decoder

func NewYamlDecoder added in v4.17.1

func NewYamlDecoder(prefs YamlPreferences) Decoder

type DecoderFactoryFunction added in v4.42.1

type DecoderFactoryFunction func() Decoder

type Encoder

type Encoder interface {
	Encode(writer io.Writer, node *CandidateNode) error
	PrintDocumentSeparator(writer io.Writer) error
	PrintLeadingContent(writer io.Writer, content string) error
	CanHandleAliases() bool
}

func NewBase64Encoder added in v4.21.1

func NewBase64Encoder() Encoder

func NewCsvEncoder added in v4.16.1

func NewCsvEncoder(prefs CsvPreferences) Encoder

func NewJSONEncoder added in v4.27.1

func NewJSONEncoder(prefs JsonPreferences) Encoder

func NewLuaEncoder added in v4.35.1

func NewLuaEncoder(prefs LuaPreferences) Encoder

func NewPropertiesEncoder added in v4.12.0

func NewPropertiesEncoder(prefs PropertiesPreferences) Encoder

func NewShEncoder added in v4.31.1

func NewShEncoder() Encoder

func NewShellVariablesEncoder added in v4.34.1

func NewShellVariablesEncoder() Encoder

func NewTomlEncoder added in v4.33.3

func NewTomlEncoder() Encoder

func NewUriEncoder added in v4.31.1

func NewUriEncoder() Encoder

func NewXMLEncoder added in v4.20.1

func NewXMLEncoder(prefs XmlPreferences) Encoder

func NewYamlEncoder

func NewYamlEncoder(prefs YamlPreferences) Encoder

type EncoderFactoryFunction added in v4.42.1

type EncoderFactoryFunction func() Encoder

type Evaluator

type Evaluator interface {
	EvaluateFiles(expression string, filenames []string, printer Printer, decoder Decoder) error

	// EvaluateNodes takes an expression and one or more yaml nodes, returning a list of matching candidate nodes
	EvaluateNodes(expression string, nodes ...*CandidateNode) (*list.List, error)

	// EvaluateCandidateNodes takes an expression and list of candidate nodes, returning a list of matching candidate nodes
	EvaluateCandidateNodes(expression string, inputCandidateNodes *list.List) (*list.List, error)
}

A yaml expression evaluator that runs the expression once against all files/nodes in memory.

func NewAllAtOnceEvaluator

func NewAllAtOnceEvaluator() Evaluator

type ExpressionNode added in v4.3.2

type ExpressionNode struct {
	Operation *Operation
	LHS       *ExpressionNode
	RHS       *ExpressionNode
}

type ExpressionParserInterface added in v4.19.1

type ExpressionParserInterface interface {
	ParseExpression(expression string) (*ExpressionNode, error)
}
var ExpressionParser ExpressionParserInterface

type Format added in v4.42.1

type Format struct {
	FormalName     string
	Names          []string
	EncoderFactory EncoderFactoryFunction
	DecoderFactory DecoderFactoryFunction
}

func FormatFromString added in v4.42.1

func FormatFromString(format string) (*Format, error)

func GetAvailableInputFormats added in v4.42.1

func GetAvailableInputFormats() []*Format

func GetAvailableOutputFormats added in v4.42.1

func GetAvailableOutputFormats() []*Format

func (*Format) GetConfiguredEncoder added in v4.42.1

func (f *Format) GetConfiguredEncoder() Encoder

func (*Format) MatchesName added in v4.42.1

func (f *Format) MatchesName(name string) bool

type JsonPreferences added in v4.42.1

type JsonPreferences struct {
	Indent        int
	ColorsEnabled bool
	UnwrapScalar  bool
}

func NewDefaultJsonPreferences added in v4.42.1

func NewDefaultJsonPreferences() JsonPreferences

func (*JsonPreferences) Copy added in v4.42.1

func (p *JsonPreferences) Copy() JsonPreferences

type Kind added in v4.40.1

type Kind uint32
const (
	SequenceNode Kind = 1 << iota
	MappingNode
	ScalarNode
	AliasNode
)

type LuaPreferences added in v4.35.1

type LuaPreferences struct {
	DocPrefix    string
	DocSuffix    string
	UnquotedKeys bool
	Globals      bool
}

func NewDefaultLuaPreferences added in v4.35.1

func NewDefaultLuaPreferences() LuaPreferences

type Operation

type Operation struct {
	OperationType *operationType
	Value         interface{}
	StringValue   string
	CandidateNode *CandidateNode // used for Value Path elements
	Preferences   interface{}
	UpdateAssign  bool // used for assign ops, when true it means we evaluate the rhs given the lhs
}

type Printer

type Printer interface {
	PrintResults(matchingNodes *list.List) error
	PrintedAnything() bool
	//e.g. when given a front-matter doc, like jekyll
	SetAppendix(reader io.Reader)
	SetNulSepOutput(nulSepOutput bool)
}

func NewPrinter

func NewPrinter(encoder Encoder, printerWriter PrinterWriter) Printer

type PrinterWriter added in v4.14.1

type PrinterWriter interface {
	GetWriter(node *CandidateNode) (*bufio.Writer, error)
}

func NewMultiPrinterWriter added in v4.14.1

func NewMultiPrinterWriter(expression *ExpressionNode, format *Format) PrinterWriter

func NewSinglePrinterWriter added in v4.14.1

func NewSinglePrinterWriter(writer io.Writer) PrinterWriter

type PropertiesPreferences added in v4.42.1

type PropertiesPreferences struct {
	UnwrapScalar      bool
	KeyValueSeparator string
	UseArrayBrackets  bool
}

func NewDefaultPropertiesPreferences added in v4.42.1

func NewDefaultPropertiesPreferences() PropertiesPreferences

func (*PropertiesPreferences) Copy added in v4.42.1

type StreamEvaluator

type StreamEvaluator interface {
	Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer, decoder Decoder) (uint, error)
	EvaluateFiles(expression string, filenames []string, printer Printer, decoder Decoder) error
	EvaluateNew(expression string, printer Printer) error
}

A yaml expression evaluator that runs the expression multiple times for each given yaml document. Uses less memory than loading all documents and running the expression once, but this cannot process cross document expressions.

func NewStreamEvaluator

func NewStreamEvaluator() StreamEvaluator

type StringEvaluator added in v4.27.1

type StringEvaluator interface {
	Evaluate(expression string, input string, encoder Encoder, decoder Decoder) (string, error)
	EvaluateAll(expression string, input string, encoder Encoder, decoder Decoder) (string, error)
}

func NewStringEvaluator added in v4.27.1

func NewStringEvaluator() StringEvaluator

type Style added in v4.40.1

type Style uint32
const (
	TaggedStyle Style = 1 << iota
	DoubleQuotedStyle
	SingleQuotedStyle
	LiteralStyle
	FoldedStyle
	FlowStyle
)

func MapYamlStyle added in v4.40.1

func MapYamlStyle(original yaml.Style) Style

type XmlPreferences added in v4.17.1

type XmlPreferences struct {
	Indent          int
	AttributePrefix string
	ContentName     string
	StrictMode      bool
	KeepNamespace   bool
	UseRawToken     bool
	ProcInstPrefix  string
	DirectiveName   string
	SkipProcInst    bool
	SkipDirectives  bool
}

func NewDefaultXmlPreferences added in v4.29.1

func NewDefaultXmlPreferences() XmlPreferences

func (*XmlPreferences) Copy added in v4.42.1

func (p *XmlPreferences) Copy() XmlPreferences

type YamlPreferences added in v4.29.1

type YamlPreferences struct {
	Indent                      int
	ColorsEnabled               bool
	LeadingContentPreProcessing bool
	PrintDocSeparators          bool
	UnwrapScalar                bool
	EvaluateTogether            bool
}

func NewDefaultYamlPreferences added in v4.29.1

func NewDefaultYamlPreferences() YamlPreferences

func (*YamlPreferences) Copy added in v4.42.1

func (p *YamlPreferences) Copy() YamlPreferences

Source Files

Jump to

Keyboard shortcuts

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