yqlib

package
v4.35.221 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2023 License: MIT Imports: 41 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const (
	YamlInputFormat = 1 << iota
	XMLInputFormat
	PropertiesInputFormat
	Base64InputFormat
	JsonInputFormat
	CSVObjectInputFormat
	TSVObjectInputFormat
	TomlInputFormat
	UriInputFormat
	LuaInputFormat
)
View Source
const (
	YamlOutputFormat = 1 << iota
	JSONOutputFormat
	PropsOutputFormat
	CSVOutputFormat
	TSVOutputFormat
	XMLOutputFormat
	Base64OutputFormat
	UriOutputFormat
	ShOutputFormat
	TomlOutputFormat
	ShellVariablesOutputFormat
	LuaOutputFormat
)

Variables

View Source
var ConfiguredLuaPreferences = NewDefaultLuaPreferences()
View Source
var ConfiguredXMLPreferences = NewDefaultXmlPreferences()
View Source
var ConfiguredYamlPreferences = NewDefaultYamlPreferences()
View Source
var LoadYamlPreferences = YamlPreferences{
	LeadingContentPreProcessing: false,
	PrintDocSeparators:          true,
	UnwrapScalar:                true,
	EvaluateTogether:            false,
}
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=""`

Functions

func FormatFromFilename

func FormatFromFilename(filename string) string

func GetLogger

func GetLogger() *logging.Logger

GetLogger returns the yq logger instance.

func InitExpressionParser

func InitExpressionParser()

func KindString

func KindString(kind Kind) string

func MapToYamlStyle

func MapToYamlStyle(original Style) yaml.Style

func NewFrontMatterHandler

func NewFrontMatterHandler(originalFilename string) frontMatterHandler

func NewWriteInPlaceHandler

func NewWriteInPlaceHandler(inputFile string) writeInPlaceHandler

func NodeContentToString

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 SafelyCloseReader

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

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

func (*CandidateNode) AddChildren

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

func (*CandidateNode) AddKeyValueChild

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

func (*CandidateNode) AsList

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

func (*CandidateNode) Copy

func (n *CandidateNode) Copy() *CandidateNode

func (*CandidateNode) CopyAsReplacement

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

func (*CandidateNode) CopyWithoutContent

func (n *CandidateNode) CopyWithoutContent() *CandidateNode

func (*CandidateNode) CreateChild

func (n *CandidateNode) CreateChild() *CandidateNode

func (*CandidateNode) CreateReplacement

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

func (*CandidateNode) CreateReplacementWithComments

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

func (*CandidateNode) GetDocument

func (n *CandidateNode) GetDocument() uint

func (*CandidateNode) GetFileIndex

func (n *CandidateNode) GetFileIndex() int

func (*CandidateNode) GetFilename

func (n *CandidateNode) GetFilename() string

func (*CandidateNode) GetKey

func (n *CandidateNode) GetKey() string

func (*CandidateNode) GetNicePath

func (n *CandidateNode) GetNicePath() string

func (*CandidateNode) GetPath

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

func (*CandidateNode) GetValueRep

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

func (*CandidateNode) MarshalJSON

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

func (*CandidateNode) MarshalYAML

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

func (*CandidateNode) SetDocument

func (n *CandidateNode) SetDocument(idx uint)

func (*CandidateNode) SetFileIndex

func (n *CandidateNode) SetFileIndex(idx int)

func (*CandidateNode) SetFilename

func (n *CandidateNode) SetFilename(name string)

func (*CandidateNode) SetParent

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

func (*CandidateNode) UnmarshalGoccyYAML

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

func (*CandidateNode) UnmarshalJSON

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

func (*CandidateNode) UnmarshalYAML

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

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

func (*Context) ChildContext

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

func (*Context) Clone

func (n *Context) Clone() Context

func (*Context) DeepClone

func (n *Context) DeepClone() Context

func (*Context) GetDateTimeLayout

func (n *Context) GetDateTimeLayout() string

func (*Context) GetVariable

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

func (*Context) ReadOnlyClone

func (n *Context) ReadOnlyClone() Context

func (*Context) SetDateTimeLayout

func (n *Context) SetDateTimeLayout(newDateTimeLayout string)

func (*Context) SetVariable

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

func (*Context) SingleChildContext

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

func (*Context) SingleReadonlyChildContext

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

func (*Context) ToString

func (n *Context) ToString() string

func (*Context) WritableClone

func (n *Context) WritableClone() Context

type DataTreeNavigator

type DataTreeNavigator interface {
	// given the context and a 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

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

func NewBase64Decoder

func NewBase64Decoder() Decoder

func NewCSVObjectDecoder

func NewCSVObjectDecoder(separator rune) Decoder

func NewGoccyYAMLDecoder

func NewGoccyYAMLDecoder() Decoder

func NewJSONDecoder

func NewJSONDecoder() Decoder

func NewLuaDecoder

func NewLuaDecoder(prefs LuaPreferences) Decoder

func NewPropertiesDecoder

func NewPropertiesDecoder() Decoder

func NewTomlDecoder

func NewTomlDecoder() Decoder

func NewUriDecoder

func NewUriDecoder() Decoder

func NewXMLDecoder

func NewXMLDecoder(prefs XmlPreferences) Decoder

func NewYamlDecoder

func NewYamlDecoder(prefs YamlPreferences) 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

func NewBase64Encoder() Encoder

func NewCsvEncoder

func NewCsvEncoder(separator rune) Encoder

func NewJSONEncoder

func NewJSONEncoder(indent int, colorise bool, unwrapScalar bool) Encoder

func NewLuaEncoder

func NewLuaEncoder(prefs LuaPreferences) Encoder

func NewPropertiesEncoder

func NewPropertiesEncoder(unwrapScalar bool) Encoder

func NewShEncoder

func NewShEncoder() Encoder

func NewShellVariablesEncoder

func NewShellVariablesEncoder() Encoder

func NewTomlEncoder

func NewTomlEncoder() Encoder

func NewUriEncoder

func NewUriEncoder() Encoder

func NewXMLEncoder

func NewXMLEncoder(indent int, prefs XmlPreferences) Encoder

func NewYamlEncoder

func NewYamlEncoder(indent int, colorise bool, prefs YamlPreferences) Encoder

type Evaluator

type Evaluator interface {
	EvaluateFiles(expression string, filenames []string, printer Printer, decoder Decoder, extra_reader io.Reader) 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

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

type ExpressionParserInterface

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

type InputFormat

type InputFormat uint

func InputFormatFromString

func InputFormatFromString(format string) (InputFormat, error)

type Kind

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

type LuaPreferences

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

func NewDefaultLuaPreferences

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 PrinterOutputFormat

type PrinterOutputFormat uint32

func OutputFormatFromString

func OutputFormatFromString(format string) (PrinterOutputFormat, error)

type PrinterWriter

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

func NewMultiPrinterWriter

func NewMultiPrinterWriter(expression *ExpressionNode, format PrinterOutputFormat) PrinterWriter

func NewSinglePrinterWriter

func NewSinglePrinterWriter(writer io.Writer) PrinterWriter

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, extra_reader io.Reader) 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

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

func NewStringEvaluator

func NewStringEvaluator() StringEvaluator

type Style

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

func MapYamlStyle

func MapYamlStyle(original yaml.Style) Style

type XmlPreferences

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

func NewDefaultXmlPreferences

func NewDefaultXmlPreferences() XmlPreferences

type YamlPreferences

type YamlPreferences struct {
	LeadingContentPreProcessing bool
	PrintDocSeparators          bool
	UnwrapScalar                bool
	EvaluateTogether            bool
}

func NewDefaultYamlPreferences

func NewDefaultYamlPreferences() YamlPreferences

Source Files

Jump to

Keyboard shortcuts

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