mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-12 11:05:37 +00:00
wip
This commit is contained in:
parent
38b9856f50
commit
ac988d9655
@ -18,12 +18,12 @@ type Evaluator interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type allAtOnceEvaluator struct {
|
type allAtOnceEvaluator struct {
|
||||||
treeNavigator DataTreeNavigator
|
treeNavigator dataTreeNavigator
|
||||||
treeCreator ExpressionParser
|
treeCreator ExpressionParser
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAllAtOnceEvaluator() Evaluator {
|
func NewAllAtOnceEvaluator() Evaluator {
|
||||||
return &allAtOnceEvaluator{treeNavigator: NewDataTreeNavigator(), treeCreator: NewExpressionParser()}
|
return &allAtOnceEvaluator{treeNavigator: newDataTreeNavigator(), treeCreator: NewExpressionParser()}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *allAtOnceEvaluator) EvaluateNodes(expression string, nodes ...*yaml.Node) (*list.List, error) {
|
func (e *allAtOnceEvaluator) EvaluateNodes(expression string, nodes ...*yaml.Node) (*list.List, error) {
|
||||||
|
@ -6,21 +6,21 @@ import (
|
|||||||
logging "gopkg.in/op/go-logging.v1"
|
logging "gopkg.in/op/go-logging.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DataTreeNavigator interface {
|
type dataTreeNavigator interface {
|
||||||
// given the context and a expressionNode,
|
// given the context and a expressionNode,
|
||||||
// this will process the against the given expressionNode and return
|
// this will process the against the given expressionNode and return
|
||||||
// a new context of matching candidates
|
// a new context of matching candidates
|
||||||
GetMatchingNodes(context Context, expressionNode *ExpressionNode) (Context, error)
|
GetMatchingNodes(context Context, expressionNode *ExpressionNode) (Context, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type dataTreeNavigator struct {
|
type dataTreeNavigatorImpl struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDataTreeNavigator() DataTreeNavigator {
|
func newDataTreeNavigator() dataTreeNavigator {
|
||||||
return &dataTreeNavigator{}
|
return &dataTreeNavigatorImpl{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dataTreeNavigator) GetMatchingNodes(context Context, expressionNode *ExpressionNode) (Context, error) {
|
func (d *dataTreeNavigatorImpl) GetMatchingNodes(context Context, expressionNode *ExpressionNode) (Context, error) {
|
||||||
if expressionNode == nil {
|
if expressionNode == nil {
|
||||||
log.Debugf("getMatchingNodes - nothing to do")
|
log.Debugf("getMatchingNodes - nothing to do")
|
||||||
return context, nil
|
return context, nil
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
yaml "gopkg.in/yaml.v3"
|
yaml "gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Encoder interface {
|
type encoder interface {
|
||||||
Encode(node *yaml.Node) error
|
Encode(node *yaml.Node) error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ type yamlEncoder struct {
|
|||||||
firstDoc bool
|
firstDoc bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewYamlEncoder(destination io.Writer, indent int, colorise bool) Encoder {
|
func newYamlEncoder(destination io.Writer, indent int, colorise bool) encoder {
|
||||||
if indent < 0 {
|
if indent < 0 {
|
||||||
indent = 0
|
indent = 0
|
||||||
}
|
}
|
||||||
@ -74,7 +74,7 @@ func mapKeysToStrings(node *yaml.Node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewJsonEncoder(destination io.Writer, indent int) Encoder {
|
func newJsonEncoder(destination io.Writer, indent int) encoder {
|
||||||
var encoder = json.NewEncoder(destination)
|
var encoder = json.NewEncoder(destination)
|
||||||
encoder.SetEscapeHTML(false) // do not escape html chars e.g. &, <, >
|
encoder.SetEscapeHTML(false) // do not escape html chars e.g. &, <, >
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ func testScenario(t *testing.T, s *expressionScenario) {
|
|||||||
os.Setenv("myenv", s.environmentVariable)
|
os.Setenv("myenv", s.environmentVariable)
|
||||||
}
|
}
|
||||||
|
|
||||||
context, err := NewDataTreeNavigator().GetMatchingNodes(Context{MatchingNodes: inputs}, node)
|
context, err := newDataTreeNavigator().GetMatchingNodes(Context{MatchingNodes: inputs}, node)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(fmt.Errorf("%v: %v", err, s.expression))
|
t.Error(fmt.Errorf("%v: %v", err, s.expression))
|
||||||
@ -251,7 +251,7 @@ func documentOutput(t *testing.T, w *bufio.Writer, s expressionScenario, formatt
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
context, err := NewDataTreeNavigator().GetMatchingNodes(Context{MatchingNodes: inputs}, node)
|
context, err := newDataTreeNavigator().GetMatchingNodes(Context{MatchingNodes: inputs}, node)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err, s.expression)
|
t.Error(err, s.expression)
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ type resultsPrinter struct {
|
|||||||
previousDocIndex uint
|
previousDocIndex uint
|
||||||
previousFileIndex int
|
previousFileIndex int
|
||||||
printedMatches bool
|
printedMatches bool
|
||||||
treeNavigator DataTreeNavigator
|
treeNavigator dataTreeNavigator
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPrinter(writer io.Writer, outputToJSON bool, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer {
|
func NewPrinter(writer io.Writer, outputToJSON bool, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer {
|
||||||
@ -36,7 +36,7 @@ func NewPrinter(writer io.Writer, outputToJSON bool, unwrapScalar bool, colorsEn
|
|||||||
indent: indent,
|
indent: indent,
|
||||||
printDocSeparators: !outputToJSON && printDocSeparators,
|
printDocSeparators: !outputToJSON && printDocSeparators,
|
||||||
firstTimePrinting: true,
|
firstTimePrinting: true,
|
||||||
treeNavigator: NewDataTreeNavigator(),
|
treeNavigator: newDataTreeNavigator(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,14 +48,14 @@ func (p *resultsPrinter) printNode(node *yaml.Node, writer io.Writer) error {
|
|||||||
p.printedMatches = p.printedMatches || (node.Tag != "!!null" &&
|
p.printedMatches = p.printedMatches || (node.Tag != "!!null" &&
|
||||||
(node.Tag != "!!bool" || node.Value != "false"))
|
(node.Tag != "!!bool" || node.Value != "false"))
|
||||||
|
|
||||||
var encoder Encoder
|
var encoder encoder
|
||||||
if node.Kind == yaml.ScalarNode && p.unwrapScalar && !p.outputToJSON {
|
if node.Kind == yaml.ScalarNode && p.unwrapScalar && !p.outputToJSON {
|
||||||
return p.writeString(writer, node.Value+"\n")
|
return p.writeString(writer, node.Value+"\n")
|
||||||
}
|
}
|
||||||
if p.outputToJSON {
|
if p.outputToJSON {
|
||||||
encoder = NewJsonEncoder(writer, p.indent)
|
encoder = newJsonEncoder(writer, p.indent)
|
||||||
} else {
|
} else {
|
||||||
encoder = NewYamlEncoder(writer, p.indent, p.colorsEnabled)
|
encoder = newYamlEncoder(writer, p.indent, p.colorsEnabled)
|
||||||
}
|
}
|
||||||
return encoder.Encode(node)
|
return encoder.Encode(node)
|
||||||
}
|
}
|
||||||
|
@ -18,13 +18,13 @@ type StreamEvaluator interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type streamEvaluator struct {
|
type streamEvaluator struct {
|
||||||
treeNavigator DataTreeNavigator
|
treeNavigator dataTreeNavigator
|
||||||
treeCreator ExpressionParser
|
treeCreator ExpressionParser
|
||||||
fileIndex int
|
fileIndex int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStreamEvaluator() StreamEvaluator {
|
func NewStreamEvaluator() StreamEvaluator {
|
||||||
return &streamEvaluator{treeNavigator: NewDataTreeNavigator(), treeCreator: NewExpressionParser()}
|
return &streamEvaluator{treeNavigator: newDataTreeNavigator(), treeCreator: NewExpressionParser()}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *streamEvaluator) EvaluateNew(expression string, printer Printer) error {
|
func (s *streamEvaluator) EvaluateNew(expression string, printer Printer) error {
|
||||||
|
Loading…
Reference in New Issue
Block a user