yq/pkg/yqlib/operators.go

49 lines
1.3 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-10 04:00:39 +00:00
2020-10-11 00:45:20 +00:00
import (
2020-10-21 01:54:58 +00:00
"container/list"
2020-12-22 00:45:51 +00:00
"fmt"
2020-10-12 01:24:59 +00:00
2020-10-11 00:45:20 +00:00
"gopkg.in/yaml.v3"
)
2020-10-10 04:00:39 +00:00
2020-10-21 01:54:58 +00:00
type OperatorHandler func(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error)
2020-10-10 04:00:39 +00:00
2020-10-27 05:45:16 +00:00
func UnwrapDoc(node *yaml.Node) *yaml.Node {
if node.Kind == yaml.DocumentNode {
return node.Content[0]
}
return node
}
2020-11-13 02:19:54 +00:00
func EmptyOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
return list.New(), nil
}
2020-10-17 11:10:47 +00:00
func createBooleanCandidate(owner *CandidateNode, value bool) *CandidateNode {
valString := "true"
if !value {
valString = "false"
}
node := &yaml.Node{Kind: yaml.ScalarNode, Value: valString, Tag: "!!bool"}
return &CandidateNode{Node: node, Document: owner.Document, Path: owner.Path}
}
2020-10-21 01:54:58 +00:00
func nodeToMap(candidate *CandidateNode) *list.List {
elMap := list.New()
elMap.PushBack(candidate)
2020-10-16 01:29:26 +00:00
return elMap
}
2020-12-22 00:45:51 +00:00
func createTraversalTree(path []interface{}) *PathTreeNode {
if len(path) == 0 {
return &PathTreeNode{Operation: &Operation{OperationType: SelfReference}}
} else if len(path) == 1 {
return &PathTreeNode{Operation: &Operation{OperationType: TraversePath, Value: path[0], StringValue: fmt.Sprintf("%v", path[0])}}
}
return &PathTreeNode{
Operation: &Operation{OperationType: ShortPipe},
Lhs: createTraversalTree(path[0:1]),
Rhs: createTraversalTree(path[1:])}
}