yq/pkg/yqlib/treeops/lib.go

155 lines
4.8 KiB
Go
Raw Normal View History

2020-10-08 23:59:03 +00:00
package treeops
import (
"bytes"
2020-10-21 01:54:58 +00:00
"container/list"
2020-10-08 23:59:03 +00:00
"fmt"
"gopkg.in/op/go-logging.v1"
"gopkg.in/yaml.v3"
)
2020-10-10 04:00:39 +00:00
var log = logging.MustGetLogger("yq-treeops")
2020-10-11 23:44:33 +00:00
type OperationType struct {
Type string
NumArgs uint // number of arguments to the op
Precedence uint
Handler OperatorHandler
}
2020-10-17 11:10:47 +00:00
var Or = &OperationType{Type: "OR", NumArgs: 2, Precedence: 20, Handler: OrOperator}
var And = &OperationType{Type: "AND", NumArgs: 2, Precedence: 20, Handler: AndOperator}
var Union = &OperationType{Type: "UNION", NumArgs: 2, Precedence: 10, Handler: UnionOperator}
2020-10-16 01:29:26 +00:00
2020-10-13 03:37:01 +00:00
var Assign = &OperationType{Type: "ASSIGN", NumArgs: 2, Precedence: 40, Handler: AssignOperator}
2020-10-19 05:14:29 +00:00
var AssignAttributes = &OperationType{Type: "ASSIGN_ATTRIBUTES", NumArgs: 2, Precedence: 40, Handler: AssignAttributesOperator}
2020-10-18 21:36:33 +00:00
var Multiply = &OperationType{Type: "MULTIPLY", NumArgs: 2, Precedence: 40, Handler: MultiplyOperator}
2020-10-16 01:29:26 +00:00
var Equals = &OperationType{Type: "EQUALS", NumArgs: 2, Precedence: 40, Handler: EqualsOperator}
2020-10-21 01:54:58 +00:00
var CreateMap = &OperationType{Type: "CREATE_MAP", NumArgs: 2, Precedence: 40, Handler: CreateMapOperator}
2020-10-16 01:29:26 +00:00
var Pipe = &OperationType{Type: "PIPE", NumArgs: 2, Precedence: 45, Handler: PipeOperator}
var Length = &OperationType{Type: "LENGTH", NumArgs: 0, Precedence: 50, Handler: LengthOperator}
2020-10-17 11:10:47 +00:00
var Collect = &OperationType{Type: "COLLECT", NumArgs: 0, Precedence: 50, Handler: CollectOperator}
2020-10-21 01:54:58 +00:00
var CollectObject = &OperationType{Type: "COLLECT_OBJECT", NumArgs: 0, Precedence: 50, Handler: CollectObjectOperator}
2020-10-20 02:53:26 +00:00
var TraversePath = &OperationType{Type: "TRAVERSE_PATH", NumArgs: 0, Precedence: 50, Handler: TraversePathOperator}
var DocumentFilter = &OperationType{Type: "DOCUMENT_FILTER", NumArgs: 0, Precedence: 50, Handler: TraversePathOperator}
2020-10-20 04:33:20 +00:00
var SelfReference = &OperationType{Type: "SELF", NumArgs: 0, Precedence: 50, Handler: SelfOperator}
var ValueOp = &OperationType{Type: "VALUE", NumArgs: 0, Precedence: 50, Handler: ValueOperator}
2020-10-20 05:27:30 +00:00
var Not = &OperationType{Type: "NOT", NumArgs: 0, Precedence: 50, Handler: NotOperator}
2020-10-20 02:53:26 +00:00
2020-10-18 00:31:36 +00:00
var RecursiveDescent = &OperationType{Type: "RECURSIVE_DESCENT", NumArgs: 0, Precedence: 50, Handler: RecursiveDescentOperator}
2020-10-11 23:44:33 +00:00
2020-10-16 01:29:26 +00:00
// not sure yet
2020-10-17 11:10:47 +00:00
var Select = &OperationType{Type: "SELECT", NumArgs: 1, Precedence: 50, Handler: SelectOperator}
2020-10-16 01:29:26 +00:00
var DeleteChild = &OperationType{Type: "DELETE", NumArgs: 2, Precedence: 40, Handler: DeleteChildOperator}
2020-10-12 01:24:59 +00:00
2020-10-16 01:29:26 +00:00
// var Splat = &OperationType{Type: "SPLAT", NumArgs: 0, Precedence: 40, Handler: SplatOperator}
2020-10-12 01:24:59 +00:00
// var Exists = &OperationType{Type: "Length", NumArgs: 2, Precedence: 35}
// filters matches if they have the existing path
2020-10-11 23:44:33 +00:00
2020-10-20 04:33:20 +00:00
type Operation struct {
2020-10-20 02:53:26 +00:00
OperationType *OperationType
Value interface{}
StringValue string
CandidateNode *CandidateNode // used for Value Path elements
2020-10-11 23:44:33 +00:00
}
2020-10-20 04:33:20 +00:00
func CreateValueOperation(value interface{}, stringValue string) *Operation {
var node yaml.Node = yaml.Node{Kind: yaml.ScalarNode}
node.Value = stringValue
switch value.(type) {
case float32, float64:
node.Tag = "!!float"
case int, int64, int32:
node.Tag = "!!int"
case bool:
node.Tag = "!!bool"
case string:
node.Tag = "!!str"
2020-10-20 04:40:11 +00:00
case nil:
node.Tag = "!!null"
2020-10-20 04:33:20 +00:00
}
return &Operation{
OperationType: ValueOp,
Value: value,
StringValue: stringValue,
CandidateNode: &CandidateNode{Node: &node},
}
}
2020-10-11 23:44:33 +00:00
// debugging purposes only
2020-10-20 04:33:20 +00:00
func (p *Operation) toString() string {
2020-10-20 02:53:26 +00:00
if p.OperationType == TraversePath {
return fmt.Sprintf("%v", p.Value)
} else if p.OperationType == DocumentFilter {
return fmt.Sprintf("d%v", p.Value)
} else if p.OperationType == SelfReference {
return "SELF"
} else if p.OperationType == ValueOp {
return fmt.Sprintf("%v (%T)", p.Value, p.Value)
} else {
return fmt.Sprintf("%v", p.OperationType.Type)
2020-10-11 23:44:33 +00:00
}
}
2020-10-10 11:42:09 +00:00
//use for debugging only
2020-10-21 01:54:58 +00:00
func NodesToString(collection *list.List) string {
2020-10-10 11:42:09 +00:00
if !log.IsEnabledFor(logging.DEBUG) {
return ""
}
result := ""
for el := collection.Front(); el != nil; el = el.Next() {
result = result + "\n" + NodeToString(el.Value.(*CandidateNode))
}
return result
}
2020-10-08 23:59:03 +00:00
func NodeToString(node *CandidateNode) string {
if !log.IsEnabledFor(logging.DEBUG) {
return ""
}
value := node.Node
if value == nil {
2020-10-17 11:10:47 +00:00
return "-- nil --"
2020-10-08 23:59:03 +00:00
}
buf := new(bytes.Buffer)
encoder := yaml.NewEncoder(buf)
errorEncoding := encoder.Encode(value)
if errorEncoding != nil {
log.Error("Error debugging node, %v", errorEncoding.Error())
}
encoder.Close()
2020-10-27 05:45:16 +00:00
tag := value.Tag
if value.Kind == yaml.DocumentNode {
tag = "doc"
}
return fmt.Sprintf(`D%v, P%v, (%v)::%v`, node.Document, node.Path, tag, buf.String())
2020-10-08 23:59:03 +00:00
}
func KindString(kind yaml.Kind) string {
switch kind {
case yaml.ScalarNode:
return "ScalarNode"
case yaml.SequenceNode:
return "SequenceNode"
case yaml.MappingNode:
return "MappingNode"
case yaml.DocumentNode:
return "DocumentNode"
case yaml.AliasNode:
return "AliasNode"
default:
return "unknown!"
}
}