1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00

Compare commits

..

3 Commits

Author SHA1 Message Date
Mike Farah
fae2b2643c Added gofmt to format command 2021-06-01 10:52:14 +10:00
Mike Farah
dd86b5e7f2 Fixing doc 2021-05-28 17:00:25 +10:00
Mike Farah
f1f75683c1 Fixed nil RHS bug in alternative operator #838 2021-05-28 16:59:02 +10:00
13 changed files with 50 additions and 24 deletions

View File

@@ -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) {

View File

@@ -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

View File

@@ -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
``` ```

View File

@@ -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. &, <, >

View File

@@ -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`,

View File

@@ -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",

View File

@@ -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",

View File

@@ -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)
} }

View File

@@ -31,7 +31,9 @@ func resultsForRhs(d *dataTreeNavigator, context Context, lhsCandidate *Candidat
if err != nil { if err != nil {
return err return err
} }
if resultCandidate != nil {
results.PushBack(resultCandidate) results.PushBack(resultCandidate)
}
return nil return nil
} }
@@ -42,8 +44,10 @@ func resultsForRhs(d *dataTreeNavigator, context Context, lhsCandidate *Candidat
if err != nil { if err != nil {
return err return err
} }
if resultCandidate != nil {
results.PushBack(resultCandidate) results.PushBack(resultCandidate)
} }
}
return nil return nil
} }

View File

@@ -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)
} }

View File

@@ -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)
} }

View File

@@ -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 {

View File

@@ -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