yq/pkg/yqlib/operator_length.go
Mike Farah e4dd94248b wip
2023-04-08 21:27:47 +10:00

37 lines
853 B
Go

package yqlib
import (
"container/list"
"fmt"
)
func lengthOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("-- lengthOperation")
var results = list.New()
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
targetNode := candidate.unwrapDocument()
var length int
switch targetNode.Kind {
case ScalarNode:
if targetNode.Tag == "!!null" {
length = 0
} else {
length = len(targetNode.Value)
}
case MappingNode:
length = len(targetNode.Content) / 2
case SequenceNode:
length = len(targetNode.Content)
default:
length = 0
}
result := candidate.CreateReplacement(ScalarNode, "!!int", fmt.Sprintf("%v", length))
results.PushBack(result)
}
return context.ChildContext(results), nil
}