From 4e385a1b934a0a4a28cf52dfb79d21ff7528e71e Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 20 Nov 2020 15:50:15 +1100 Subject: [PATCH] get file wip --- pkg/yqlib/candidate_node.go | 11 ++++++----- pkg/yqlib/lib.go | 2 ++ pkg/yqlib/operator_file.go | 38 +++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 pkg/yqlib/operator_file.go diff --git a/pkg/yqlib/candidate_node.go b/pkg/yqlib/candidate_node.go index 49a3df94..603bd9b8 100644 --- a/pkg/yqlib/candidate_node.go +++ b/pkg/yqlib/candidate_node.go @@ -6,14 +6,15 @@ import ( "strings" "github.com/jinzhu/copier" - "gopkg.in/yaml.v3" + yaml "gopkg.in/yaml.v3" ) type CandidateNode struct { - Node *yaml.Node // the actual node - Path []interface{} /// the path we took to get to this node - Document uint // the document index of this node - Filename string + Node *yaml.Node // the actual node + Path []interface{} /// the path we took to get to this node + Document uint // the document index of this node + Filename string + FileIndex int } func (n *CandidateNode) GetKey() string { diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 463948fb..0c93f086 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -51,6 +51,8 @@ var GetStyle = &OperationType{Type: "GET_STYLE", NumArgs: 0, Precedence: 50, Han var GetTag = &OperationType{Type: "GET_TAG", NumArgs: 0, Precedence: 50, Handler: GetTagOperator} var GetComment = &OperationType{Type: "GET_COMMENT", NumArgs: 0, Precedence: 50, Handler: GetCommentsOperator} var GetDocumentIndex = &OperationType{Type: "GET_DOCUMENT_INDEX", NumArgs: 0, Precedence: 50, Handler: GetDocumentIndexOperator} +var GetFilename = &OperationType{Type: "GET_FILENAME", NumArgs: 0, Precedence: 50, Handler: GetFilename} +var GetFileIndex = &OperationType{Type: "GET_FILE_INDEX", NumArgs: 0, Precedence: 50, Handler: GetFileIndex} var Explode = &OperationType{Type: "EXPLODE", NumArgs: 1, Precedence: 50, Handler: ExplodeOperator} diff --git a/pkg/yqlib/operator_file.go b/pkg/yqlib/operator_file.go new file mode 100644 index 00000000..ad0653b4 --- /dev/null +++ b/pkg/yqlib/operator_file.go @@ -0,0 +1,38 @@ +package yqlib + +import ( + "container/list" + "fmt" + + yaml "gopkg.in/yaml.v3" +) + +func GetFilename(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { + log.Debugf("GetFilename") + + var results = list.New() + + for el := matchingNodes.Front(); el != nil; el = el.Next() { + candidate := el.Value.(*CandidateNode) + node := &yaml.Node{Kind: yaml.ScalarNode, Value: candidate.Filename, Tag: "!!str"} + lengthCand := &CandidateNode{Node: node, Document: candidate.Document, Path: candidate.Path} + results.PushBack(lengthCand) + } + + return results, nil +} + +func GetFileIndex(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { + log.Debugf("GetFileIndex") + + var results = list.New() + + for el := matchingNodes.Front(); el != nil; el = el.Next() { + candidate := el.Value.(*CandidateNode) + node := &yaml.Node{Kind: yaml.ScalarNode, Value: fmt.Sprintf("%v", candidate.FileIndex), Tag: "!!int"} + lengthCand := &CandidateNode{Node: node, Document: candidate.Document, Path: candidate.Path} + results.PushBack(lengthCand) + } + + return results, nil +}