From 7d5b6b544283f54bfb319b17f4da64369c09fd4c Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Wed, 13 Jan 2021 10:04:52 +1100 Subject: [PATCH] Removed global vars --- pkg/yqlib/all_at_once_evaluator.go | 4 ++-- pkg/yqlib/operators_test.go | 10 +++++----- pkg/yqlib/path_tree_test.go | 14 +++++++------- pkg/yqlib/printer.go | 4 +++- pkg/yqlib/stream_evaluator.go | 8 ++++---- pkg/yqlib/utils.go | 3 --- 6 files changed, 21 insertions(+), 22 deletions(-) diff --git a/pkg/yqlib/all_at_once_evaluator.go b/pkg/yqlib/all_at_once_evaluator.go index 4a9c6d5e..e7cd1ffe 100644 --- a/pkg/yqlib/all_at_once_evaluator.go +++ b/pkg/yqlib/all_at_once_evaluator.go @@ -35,11 +35,11 @@ func (e *allAtOnceEvaluator) EvaluateNodes(expression string, nodes ...*yaml.Nod } func (e *allAtOnceEvaluator) EvaluateCandidateNodes(expression string, inputCandidates *list.List) (*list.List, error) { - node, err := treeCreator.ParsePath(expression) + node, err := e.treeCreator.ParsePath(expression) if err != nil { return nil, err } - return treeNavigator.GetMatchingNodes(inputCandidates, node) + return e.treeNavigator.GetMatchingNodes(inputCandidates, node) } func (e *allAtOnceEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer) error { diff --git a/pkg/yqlib/operators_test.go b/pkg/yqlib/operators_test.go index 5d6dc1ea..838f34a8 100644 --- a/pkg/yqlib/operators_test.go +++ b/pkg/yqlib/operators_test.go @@ -30,7 +30,7 @@ func testScenario(t *testing.T, s *expressionScenario) { var results *list.List var err error - node, err := treeCreator.ParsePath(s.expression) + node, err := NewPathTreeCreator().ParsePath(s.expression) if err != nil { t.Error(fmt.Errorf("Error parsing expression %v of %v: %v", s.expression, s.description, err)) return @@ -66,7 +66,7 @@ func testScenario(t *testing.T, s *expressionScenario) { os.Setenv("myenv", s.environmentVariable) } - results, err = treeNavigator.GetMatchingNodes(inputs, node) + results, err = NewDataTreeNavigator().GetMatchingNodes(inputs, node) if err != nil { t.Error(fmt.Errorf("%v: %v", err, s.expression)) @@ -110,7 +110,7 @@ func formatYaml(yaml string, filename string) string { var output bytes.Buffer printer := NewPrinter(bufio.NewWriter(&output), false, true, false, 2, true) - node, err := treeCreator.ParsePath(".. style= \"\"") + node, err := NewPathTreeCreator().ParsePath(".. style= \"\"") if err != nil { panic(err) } @@ -219,7 +219,7 @@ func documentOutput(t *testing.T, w *bufio.Writer, s expressionScenario, formatt var err error printer := NewPrinter(bufio.NewWriter(&output), false, true, false, 2, true) - node, err := treeCreator.ParsePath(s.expression) + node, err := NewPathTreeCreator().ParsePath(s.expression) if err != nil { t.Error(fmt.Errorf("Error parsing expression %v of %v: %v", s.expression, s.description, err)) return @@ -252,7 +252,7 @@ func documentOutput(t *testing.T, w *bufio.Writer, s expressionScenario, formatt } - results, err := treeNavigator.GetMatchingNodes(inputs, node) + results, err := NewDataTreeNavigator().GetMatchingNodes(inputs, node) if err != nil { t.Error(err, s.expression) } diff --git a/pkg/yqlib/path_tree_test.go b/pkg/yqlib/path_tree_test.go index 6b2ab713..268c63a8 100644 --- a/pkg/yqlib/path_tree_test.go +++ b/pkg/yqlib/path_tree_test.go @@ -7,36 +7,36 @@ import ( ) func TestPathTreeNoArgsForTwoArgOp(t *testing.T) { - _, err := treeCreator.ParsePath("=") + _, err := NewPathTreeCreator().ParsePath("=") test.AssertResultComplex(t, "'=' expects 2 args but there is 0", err.Error()) } func TestPathTreeOneLhsArgsForTwoArgOp(t *testing.T) { - _, err := treeCreator.ParsePath(".a =") + _, err := NewPathTreeCreator().ParsePath(".a =") test.AssertResultComplex(t, "'=' expects 2 args but there is 1", err.Error()) } func TestPathTreeOneRhsArgsForTwoArgOp(t *testing.T) { - _, err := treeCreator.ParsePath("= .a") + _, err := NewPathTreeCreator().ParsePath("= .a") test.AssertResultComplex(t, "'=' expects 2 args but there is 1", err.Error()) } func TestPathTreeTwoArgsForTwoArgOp(t *testing.T) { - _, err := treeCreator.ParsePath(".a = .b") + _, err := NewPathTreeCreator().ParsePath(".a = .b") test.AssertResultComplex(t, nil, err) } func TestPathTreeNoArgsForOneArgOp(t *testing.T) { - _, err := treeCreator.ParsePath("explode") + _, err := NewPathTreeCreator().ParsePath("explode") test.AssertResultComplex(t, "'explode' expects 1 arg but received none", err.Error()) } func TestPathTreeOneArgForOneArgOp(t *testing.T) { - _, err := treeCreator.ParsePath("explode(.)") + _, err := NewPathTreeCreator().ParsePath("explode(.)") test.AssertResultComplex(t, nil, err) } func TestPathTreeExtraArgs(t *testing.T) { - _, err := treeCreator.ParsePath("sortKeys(.) explode(.)") + _, err := NewPathTreeCreator().ParsePath("sortKeys(.) explode(.)") test.AssertResultComplex(t, "expected end of expression but found 'explode', please check expression syntax", err.Error()) } diff --git a/pkg/yqlib/printer.go b/pkg/yqlib/printer.go index 17af7ca2..d558962f 100644 --- a/pkg/yqlib/printer.go +++ b/pkg/yqlib/printer.go @@ -24,6 +24,7 @@ type resultsPrinter struct { previousDocIndex uint previousFileIndex int printedMatches bool + treeNavigator DataTreeNavigator } func NewPrinter(writer io.Writer, outputToJSON bool, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer { @@ -35,6 +36,7 @@ func NewPrinter(writer io.Writer, outputToJSON bool, unwrapScalar bool, colorsEn indent: indent, printDocSeparators: printDocSeparators, firstTimePrinting: true, + treeNavigator: NewDataTreeNavigator(), } } @@ -76,7 +78,7 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error { if p.outputToJSON { explodeOp := Operation{OperationType: explodeOpType} explodeNode := PathTreeNode{Operation: &explodeOp} - matchingNodes, err = treeNavigator.GetMatchingNodes(matchingNodes, &explodeNode) + matchingNodes, err = p.treeNavigator.GetMatchingNodes(matchingNodes, &explodeNode) if err != nil { return err } diff --git a/pkg/yqlib/stream_evaluator.go b/pkg/yqlib/stream_evaluator.go index b0faaf02..67286f57 100644 --- a/pkg/yqlib/stream_evaluator.go +++ b/pkg/yqlib/stream_evaluator.go @@ -28,7 +28,7 @@ func NewStreamEvaluator() StreamEvaluator { } func (s *streamEvaluator) EvaluateNew(expression string, printer Printer) error { - node, err := treeCreator.ParsePath(expression) + node, err := s.treeCreator.ParsePath(expression) if err != nil { return err } @@ -41,7 +41,7 @@ func (s *streamEvaluator) EvaluateNew(expression string, printer Printer) error inputList := list.New() inputList.PushBack(candidateNode) - matches, errorParsing := treeNavigator.GetMatchingNodes(inputList, node) + matches, errorParsing := s.treeNavigator.GetMatchingNodes(inputList, node) if errorParsing != nil { return errorParsing } @@ -50,7 +50,7 @@ func (s *streamEvaluator) EvaluateNew(expression string, printer Printer) error func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer) error { - node, err := treeCreator.ParsePath(expression) + node, err := s.treeCreator.ParsePath(expression) if err != nil { return err } @@ -97,7 +97,7 @@ func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *Path inputList := list.New() inputList.PushBack(candidateNode) - matches, errorParsing := treeNavigator.GetMatchingNodes(inputList, node) + matches, errorParsing := s.treeNavigator.GetMatchingNodes(inputList, node) if errorParsing != nil { return errorParsing } diff --git a/pkg/yqlib/utils.go b/pkg/yqlib/utils.go index 092df9d2..3e8084bc 100644 --- a/pkg/yqlib/utils.go +++ b/pkg/yqlib/utils.go @@ -9,9 +9,6 @@ import ( yaml "gopkg.in/yaml.v3" ) -var treeNavigator = NewDataTreeNavigator() -var treeCreator = NewPathTreeCreator() - func readStream(filename string) (io.Reader, error) { if filename == "-" { return bufio.NewReader(os.Stdin), nil