Removed global vars

This commit is contained in:
Mike Farah 2021-01-13 10:04:52 +11:00
parent b749973fe0
commit 7d5b6b5442
6 changed files with 21 additions and 22 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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