mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fae2b2643c | ||
|
|
dd86b5e7f2 | ||
|
|
f1f75683c1 |
@@ -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 dataTreeNavigatorImpl struct {
|
type dataTreeNavigator struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDataTreeNavigator() dataTreeNavigator {
|
func NewDataTreeNavigator() DataTreeNavigator {
|
||||||
return &dataTreeNavigatorImpl{}
|
return &dataTreeNavigator{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dataTreeNavigatorImpl) GetMatchingNodes(context Context, expressionNode *ExpressionNode) (Context, error) {
|
func (d *dataTreeNavigator) 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
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ dog
|
|||||||
Both sides have now been evaluated, so now the operator copies across the value from the RHS to the value on the LHS, and it returns the now updated context:
|
Both sides have now been evaluated, so now the operator copies across the value from the RHS to the value on the LHS, and it returns the now updated context:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
a: cat
|
a: dog
|
||||||
b: dog
|
b: dog
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -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. &, <, >
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var alternativeOperatorScenarios = []expressionScenario{
|
var alternativeOperatorScenarios = []expressionScenario{
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
expression: `.b // .c`,
|
||||||
|
document: `a: bridge`,
|
||||||
|
expected: []string{},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
skipDoc: true,
|
skipDoc: true,
|
||||||
expression: `(.b // "hello") as $x`,
|
expression: `(.b // "hello") as $x`,
|
||||||
|
|||||||
@@ -12,6 +12,14 @@ var booleanOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[], (!!bool)::true\n",
|
"D0, P[], (!!bool)::true\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
document: "b: hi",
|
||||||
|
expression: `.a or .c`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!bool)::false\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
skipDoc: true,
|
skipDoc: true,
|
||||||
document: "b: hi",
|
document: "b: hi",
|
||||||
|
|||||||
@@ -5,6 +5,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var equalsOperatorScenarios = []expressionScenario{
|
var equalsOperatorScenarios = []expressionScenario{
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
expression: ".a == .b",
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!bool)::true\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
skipDoc: true,
|
skipDoc: true,
|
||||||
document: "cat",
|
document: "cat",
|
||||||
|
|||||||
@@ -308,6 +308,6 @@ func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, originalCandidate *C
|
|||||||
|
|
||||||
func traverseArray(candidate *CandidateNode, operation *Operation, prefs traversePreferences) (*list.List, error) {
|
func traverseArray(candidate *CandidateNode, operation *Operation, prefs traversePreferences) (*list.List, error) {
|
||||||
log.Debug("operation Value %v", operation.Value)
|
log.Debug("operation Value %v", operation.Value)
|
||||||
indices := []*yaml.Node{&yaml.Node{Value: operation.StringValue}}
|
indices := []*yaml.Node{{Value: operation.StringValue}}
|
||||||
return traverseArrayWithIndices(candidate, indices, prefs)
|
return traverseArrayWithIndices(candidate, indices, prefs)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,9 @@ func resultsForRhs(d *dataTreeNavigator, context Context, lhsCandidate *Candidat
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
results.PushBack(resultCandidate)
|
if resultCandidate != nil {
|
||||||
|
results.PushBack(resultCandidate)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,7 +44,9 @@ func resultsForRhs(d *dataTreeNavigator, context Context, lhsCandidate *Candidat
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
results.PushBack(resultCandidate)
|
if resultCandidate != nil {
|
||||||
|
results.PushBack(resultCandidate)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
find . \( -path ./vendor \) -prune -o -name "*.go" -exec goimports -w {} \;
|
find . \( -path ./vendor \) -prune -o -name "*.go" -exec goimports -w {} \;
|
||||||
|
gofmt -w -s .
|
||||||
go mod tidy
|
go mod tidy
|
||||||
go mod vendor
|
go mod vendor
|
||||||
|
|||||||
Reference in New Issue
Block a user