mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 13:48:06 +00:00
Handle empty files
This commit is contained in:
parent
a13617407e
commit
28a8721f49
@ -68,6 +68,17 @@ func (e *allAtOnceEvaluator) EvaluateFiles(expression string, filenames []string
|
|||||||
allDocuments.PushBackList(fileDocuments)
|
allDocuments.PushBackList(fileDocuments)
|
||||||
fileIndex = fileIndex + 1
|
fileIndex = fileIndex + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if allDocuments.Len() == 0 {
|
||||||
|
candidateNode := &CandidateNode{
|
||||||
|
Document: 0,
|
||||||
|
Filename: "",
|
||||||
|
Node: &yaml.Node{Tag: "!!null", Kind: yaml.ScalarNode},
|
||||||
|
FileIndex: 0,
|
||||||
|
}
|
||||||
|
allDocuments.PushBack(candidateNode)
|
||||||
|
}
|
||||||
|
|
||||||
matches, err := e.EvaluateCandidateNodes(expression, allDocuments)
|
matches, err := e.EvaluateCandidateNodes(expression, allDocuments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -114,7 +114,7 @@ func formatYaml(yaml string, filename string) string {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
streamEvaluator := NewStreamEvaluator()
|
streamEvaluator := NewStreamEvaluator()
|
||||||
err = streamEvaluator.Evaluate(filename, strings.NewReader(yaml), node, printer)
|
_, err = streamEvaluator.Evaluate(filename, strings.NewReader(yaml), node, printer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
// Uses less memory than loading all documents and running the expression once, but this cannot process
|
// Uses less memory than loading all documents and running the expression once, but this cannot process
|
||||||
// cross document expressions.
|
// cross document expressions.
|
||||||
type StreamEvaluator interface {
|
type StreamEvaluator interface {
|
||||||
Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer) error
|
Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer) (uint, error)
|
||||||
EvaluateFiles(expression string, filenames []string, printer Printer) error
|
EvaluateFiles(expression string, filenames []string, printer Printer) error
|
||||||
EvaluateNew(expression string, printer Printer) error
|
EvaluateNew(expression string, printer Printer) error
|
||||||
}
|
}
|
||||||
@ -49,7 +49,7 @@ func (s *streamEvaluator) EvaluateNew(expression string, printer Printer) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer) error {
|
func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer) error {
|
||||||
|
var totalProcessDocs uint = 0
|
||||||
node, err := s.treeCreator.ParseExpression(expression)
|
node, err := s.treeCreator.ParseExpression(expression)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -63,23 +63,28 @@ func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, p
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = s.Evaluate(filename, reader, node, printer)
|
processedDocs, err := s.Evaluate(filename, reader, node, printer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
totalProcessDocs = totalProcessDocs + processedDocs
|
||||||
|
|
||||||
switch reader := reader.(type) {
|
switch reader := reader.(type) {
|
||||||
case *os.File:
|
case *os.File:
|
||||||
safelyCloseFile(reader)
|
safelyCloseFile(reader)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if totalProcessDocs == 0 {
|
||||||
|
return s.EvaluateNew(expression, printer)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer) error {
|
func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer) (uint, error) {
|
||||||
|
|
||||||
var currentIndex uint
|
var currentIndex uint
|
||||||
|
|
||||||
decoder := yaml.NewDecoder(reader)
|
decoder := yaml.NewDecoder(reader)
|
||||||
for {
|
for {
|
||||||
var dataBucket yaml.Node
|
var dataBucket yaml.Node
|
||||||
@ -87,9 +92,9 @@ func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *Expr
|
|||||||
|
|
||||||
if errorReading == io.EOF {
|
if errorReading == io.EOF {
|
||||||
s.fileIndex = s.fileIndex + 1
|
s.fileIndex = s.fileIndex + 1
|
||||||
return nil
|
return currentIndex, nil
|
||||||
} else if errorReading != nil {
|
} else if errorReading != nil {
|
||||||
return errorReading
|
return currentIndex, errorReading
|
||||||
}
|
}
|
||||||
candidateNode := &CandidateNode{
|
candidateNode := &CandidateNode{
|
||||||
Document: currentIndex,
|
Document: currentIndex,
|
||||||
@ -102,11 +107,11 @@ func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *Expr
|
|||||||
|
|
||||||
result, errorParsing := s.treeNavigator.GetMatchingNodes(Context{MatchingNodes: inputList}, node)
|
result, errorParsing := s.treeNavigator.GetMatchingNodes(Context{MatchingNodes: inputList}, node)
|
||||||
if errorParsing != nil {
|
if errorParsing != nil {
|
||||||
return errorParsing
|
return currentIndex, errorParsing
|
||||||
}
|
}
|
||||||
err := printer.PrintResults(result.MatchingNodes)
|
err := printer.PrintResults(result.MatchingNodes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return currentIndex, err
|
||||||
}
|
}
|
||||||
currentIndex = currentIndex + 1
|
currentIndex = currentIndex + 1
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ if [[ $? != 1 ]]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Test leading seperator logic
|
echo "Test: leading seperator logic"
|
||||||
expected=$(cat examples/leading-seperator.yaml)
|
expected=$(cat examples/leading-seperator.yaml)
|
||||||
|
|
||||||
X=$(cat examples/leading-seperator.yaml | ./yq e '.' -)
|
X=$(cat examples/leading-seperator.yaml | ./yq e '.' -)
|
||||||
@ -113,7 +113,7 @@ if [[ $X != $expected ]]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# handle empty files
|
echo "Test: handle empty files"
|
||||||
./yq e '.' examples/empty.yaml
|
./yq e '.' examples/empty.yaml
|
||||||
if [[ $? != 0 ]]; then
|
if [[ $? != 0 ]]; then
|
||||||
echo "Expected no error when processing empty file but got one"
|
echo "Expected no error when processing empty file but got one"
|
||||||
@ -126,6 +126,29 @@ if [[ $? != 0 ]]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# run expression against empty file
|
||||||
|
touch temp.yaml
|
||||||
|
expected="apple: tree"
|
||||||
|
|
||||||
|
./yq e '.apple = "tree"' temp.yaml -i
|
||||||
|
X=$(cat temp.yaml)
|
||||||
|
rm temp.yaml
|
||||||
|
if [[ $X != $expected ]]; then
|
||||||
|
echo "Write empty doc"
|
||||||
|
echo "Expected $expected but was $X"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
touch temp.yaml
|
||||||
|
|
||||||
|
./yq ea '.apple = "tree"' temp.yaml -i
|
||||||
|
X=$(cat temp.yaml)
|
||||||
|
rm temp.yaml
|
||||||
|
if [[ $X != $expected ]]; then
|
||||||
|
echo "Write all empty doc"
|
||||||
|
echo "Expected $expected but was $X"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
echo "--success"
|
echo "--success"
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user