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

Fixed linting

This commit is contained in:
Mike Farah
2020-11-13 14:07:11 +11:00
parent f8a700e400
commit af39fc737d
18 changed files with 135 additions and 101 deletions

View File

@@ -20,10 +20,13 @@ func (n *CandidateNode) GetKey() string {
return fmt.Sprintf("%v - %v", n.Document, n.Path)
}
func (n *CandidateNode) Copy() *CandidateNode {
func (n *CandidateNode) Copy() (*CandidateNode, error) {
clone := &CandidateNode{}
copier.Copy(clone, n)
return clone
err := copier.Copy(clone, n)
if err != nil {
return nil, err
}
return clone, nil
}
// updates this candidate from the given candidate node

View File

@@ -83,10 +83,14 @@ func collect(d *dataTreeNavigator, aggregate *list.List, remainingMatches *list.
aggCandidate := el.Value.(*CandidateNode)
for splatEl := splatted.Front(); splatEl != nil; splatEl = splatEl.Next() {
splatCandidate := splatEl.Value.(*CandidateNode)
newCandidate := aggCandidate.Copy()
newCandidate, err := aggCandidate.Copy()
if err != nil {
return nil, err
}
newCandidate.Path = nil
newCandidate, err := multiply(d, newCandidate, splatCandidate)
newCandidate, err = multiply(d, newCandidate, splatCandidate)
if err != nil {
return nil, err
}

View File

@@ -18,7 +18,10 @@ func ExplodeOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTr
return nil, err
}
for childEl := rhs.Front(); childEl != nil; childEl = childEl.Next() {
explodeNode(childEl.Value.(*CandidateNode).Node)
err = explodeNode(childEl.Value.(*CandidateNode).Node)
if err != nil {
return nil, err
}
}
}
@@ -65,11 +68,17 @@ func explodeNode(node *yaml.Node) error {
log.Debugf("an alias merge list!")
for index := 0; index < len(valueNode.Content); index = index + 1 {
aliasNode := valueNode.Content[index]
applyAlias(node, aliasNode.Alias, index, newContent)
err := applyAlias(node, aliasNode.Alias, index, newContent)
if err != nil {
return err
}
}
} else {
log.Debugf("an alias merge!")
applyAlias(node, valueNode.Alias, index, newContent)
err := applyAlias(node, valueNode.Alias, index, newContent)
if err != nil {
return err
}
}
}
}

View File

@@ -72,7 +72,10 @@ func multiply(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*Ca
func mergeObjects(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
var results = list.New()
recursiveDecent(d, results, nodeToMap(rhs))
err := recursiveDecent(d, results, nodeToMap(rhs))
if err != nil {
return nil, err
}
var pathIndexToStartFrom int = 0
if results.Front() != nil {

View File

@@ -136,7 +136,10 @@ func traverseMap(newMatches *orderedmap.OrderedMap, candidate *CandidateNode, op
//skip the 'merge' tag, find a direct match first
if key.Tag == "!!merge" && followAlias {
log.Debug("Merge anchor")
traverseMergeAnchor(newMatches, candidate, value, operation)
err := traverseMergeAnchor(newMatches, candidate, value, operation)
if err != nil {
return err
}
} else if keyMatches(key, operation) {
log.Debug("MATCHED")
candidateNode := &CandidateNode{
@@ -151,7 +154,7 @@ func traverseMap(newMatches *orderedmap.OrderedMap, candidate *CandidateNode, op
return nil
}
func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, originalCandidate *CandidateNode, value *yaml.Node, operation *Operation) {
func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, originalCandidate *CandidateNode, value *yaml.Node, operation *Operation) error {
switch value.Kind {
case yaml.AliasNode:
candidateNode := &CandidateNode{
@@ -159,13 +162,16 @@ func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, originalCandidate *C
Path: originalCandidate.Path,
Document: originalCandidate.Document,
}
traverseMap(newMatches, candidateNode, operation)
return traverseMap(newMatches, candidateNode, operation)
case yaml.SequenceNode:
for _, childValue := range value.Content {
traverseMergeAnchor(newMatches, originalCandidate, childValue, operation)
err := traverseMergeAnchor(newMatches, originalCandidate, childValue, operation)
if err != nil {
return err
}
}
}
return
return nil
}
func traverseArray(candidate *CandidateNode, operation *Operation) ([]*CandidateNode, error) {

View File

@@ -48,6 +48,13 @@ func testScenario(t *testing.T, s *expressionScenario) {
test.AssertResultComplexWithContext(t, s.expected, resultsToString(results), fmt.Sprintf("exp: %v\ndoc: %v", s.expression, s.document))
}
func writeOrPanic(w *bufio.Writer, text string) {
_, err := w.WriteString(text)
if err != nil {
panic(err)
}
}
func documentScenarios(t *testing.T, title string, scenarios []expressionScenario) {
f, err := os.Create(fmt.Sprintf("doc/%v.md", title))
@@ -56,27 +63,27 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
}
defer f.Close()
w := bufio.NewWriter(f)
w.WriteString(fmt.Sprintf("# %v\n", title))
w.WriteString(fmt.Sprintf("## Examples\n"))
writeOrPanic(w, fmt.Sprintf("# %v\n", title))
writeOrPanic(w, "## Examples\n")
for index, s := range scenarios {
if !s.skipDoc {
if s.description != "" {
w.WriteString(fmt.Sprintf("### %v\n", s.description))
writeOrPanic(w, fmt.Sprintf("### %v\n", s.description))
} else {
w.WriteString(fmt.Sprintf("### Example %v\n", index))
writeOrPanic(w, fmt.Sprintf("### Example %v\n", index))
}
if s.document != "" {
w.WriteString(fmt.Sprintf("sample.yml:\n"))
w.WriteString(fmt.Sprintf("```yaml\n%v\n```\n", s.document))
writeOrPanic(w, "sample.yml:\n")
writeOrPanic(w, fmt.Sprintf("```yaml\n%v\n```\n", s.document))
}
if s.expression != "" {
w.WriteString(fmt.Sprintf("Expression\n"))
w.WriteString(fmt.Sprintf("```bash\nyq '%v' < sample.yml\n```\n", s.expression))
writeOrPanic(w, "Expression\n")
writeOrPanic(w, fmt.Sprintf("```bash\nyq '%v' < sample.yml\n```\n", s.expression))
}
w.WriteString(fmt.Sprintf("Result\n"))
writeOrPanic(w, "Result\n")
var output bytes.Buffer
var err error
@@ -88,15 +95,18 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
t.Error(err)
}
err = EvaluateStream("sample.yaml", strings.NewReader(s.document), node, printer)
if err != nil {
t.Error(err)
}
} else {
err = EvaluateAllFileStreams(s.expression, []string{}, printer)
if err != nil {
t.Error(err)
}
}
w.WriteString(fmt.Sprintf("```yaml\n%v```\n", output.String()))
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n", output.String()))
if err != nil {
t.Error(err)
}
}
}

View File

@@ -59,7 +59,7 @@ func GetStyleOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *
for el := matchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
var style = ""
var style string
switch candidate.Node.Style {
case yaml.TaggedStyle:
style = "tagged"

View File

@@ -1,7 +1,6 @@
package yqlib
import (
"fmt"
"strconv"
lex "github.com/timtadh/lexmachine"
@@ -47,14 +46,14 @@ func (t *Token) toString() string {
} else if t.TokenType == CloseCollectObject {
return "}"
} else {
return fmt.Sprintf("NFI")
return "NFI"
}
}
func pathToken(wrapped bool) lex.Action {
return func(s *lex.Scanner, m *machines.Match) (interface{}, error) {
value := string(m.Bytes)
value = value[1:len(value)]
value = value[1:]
if wrapped {
value = unwrap(value)
}
@@ -73,7 +72,7 @@ func literalPathToken(value string) lex.Action {
func documentToken() lex.Action {
return func(s *lex.Scanner, m *machines.Match) (interface{}, error) {
var numberString = string(m.Bytes)
numberString = numberString[1:len(numberString)]
numberString = numberString[1:]
var number, errParsingInt = strconv.ParseInt(numberString, 10, 64) // nolint
if errParsingInt != nil {
return nil, errParsingInt

View File

@@ -7,8 +7,8 @@ var myPathPostfixer = NewPathPostFixer()
type PathTreeNode struct {
Operation *Operation
Lhs *PathTreeNode
Rhs *PathTreeNode
Lhs *PathTreeNode
Rhs *PathTreeNode
}
type PathTreeCreator interface {

View File

@@ -77,7 +77,10 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
mappedDoc := el.Value.(*CandidateNode)
if (!p.firstTimePrinting || (previousDocIndex != mappedDoc.Document)) && p.printDocSeparators {
p.writeString(bufferedWriter, "---\n")
if err := p.writeString(bufferedWriter, "---\n"); err != nil {
return err
}
}
if err := p.printNode(mappedDoc.Node, bufferedWriter); err != nil {

View File

@@ -46,7 +46,10 @@ func EvaluateStream(filename string, reader io.Reader, node *PathTreeNode, print
if errorParsing != nil {
return errorParsing
}
printer.PrintResults(matches)
err := printer.PrintResults(matches)
if err != nil {
return err
}
currentIndex = currentIndex + 1
}
}
@@ -61,9 +64,9 @@ func readDocuments(reader io.Reader, filename string) (*list.List, error) {
errorReading := decoder.Decode(&dataBucket)
if errorReading == io.EOF {
switch reader.(type) {
switch reader := reader.(type) {
case *os.File:
safelyCloseFile(reader.(*os.File))
safelyCloseFile(reader)
}
return inputList, nil
} else if errorReading != nil {
@@ -122,49 +125,49 @@ func EvaluateFileStreamsSequence(expression string, filenames []string, printer
return err
}
switch reader.(type) {
switch reader := reader.(type) {
case *os.File:
safelyCloseFile(reader.(*os.File))
safelyCloseFile(reader)
}
}
return nil
}
func safelyRenameFile(from string, to string) {
if renameError := os.Rename(from, to); renameError != nil {
log.Debugf("Error renaming from %v to %v, attempting to copy contents", from, to)
log.Debug(renameError.Error())
// can't do this rename when running in docker to a file targeted in a mounted volume,
// so gracefully degrade to copying the entire contents.
if copyError := copyFileContents(from, to); copyError != nil {
log.Errorf("Failed copying from %v to %v", from, to)
log.Error(copyError.Error())
} else {
removeErr := os.Remove(from)
if removeErr != nil {
log.Errorf("failed removing original file: %s", from)
}
}
}
}
// func safelyRenameFile(from string, to string) {
// if renameError := os.Rename(from, to); renameError != nil {
// log.Debugf("Error renaming from %v to %v, attempting to copy contents", from, to)
// log.Debug(renameError.Error())
// // can't do this rename when running in docker to a file targeted in a mounted volume,
// // so gracefully degrade to copying the entire contents.
// if copyError := copyFileContents(from, to); copyError != nil {
// log.Errorf("Failed copying from %v to %v", from, to)
// log.Error(copyError.Error())
// } else {
// removeErr := os.Remove(from)
// if removeErr != nil {
// log.Errorf("failed removing original file: %s", from)
// }
// }
// }
// }
// thanks https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file-in-golang
func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src) // nolint gosec
if err != nil {
return err
}
defer safelyCloseFile(in)
out, err := os.Create(dst)
if err != nil {
return err
}
defer safelyCloseFile(out)
if _, err = io.Copy(out, in); err != nil {
return err
}
return out.Sync()
}
// // thanks https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file-in-golang
// func copyFileContents(src, dst string) (err error) {
// in, err := os.Open(src) // nolint gosec
// if err != nil {
// return err
// }
// defer safelyCloseFile(in)
// out, err := os.Create(dst)
// if err != nil {
// return err
// }
// defer safelyCloseFile(out)
// if _, err = io.Copy(out, in); err != nil {
// return err
// }
// return out.Sync()
// }
func safelyFlush(writer *bufio.Writer) {
if err := writer.Flush(); err != nil {