2020-12-01 06:58:07 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func lengthOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2020-12-01 06:58:07 +00:00
|
|
|
log.Debugf("-- lengthOperation")
|
|
|
|
var results = list.New()
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-12-01 06:58:07 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
2021-01-12 23:00:51 +00:00
|
|
|
targetNode := unwrapDoc(candidate.Node)
|
2020-12-01 06:58:07 +00:00
|
|
|
var length int
|
|
|
|
switch targetNode.Kind {
|
|
|
|
case yaml.ScalarNode:
|
2021-01-25 23:21:16 +00:00
|
|
|
if targetNode.Tag == "!!null" {
|
|
|
|
length = 0
|
|
|
|
} else {
|
|
|
|
length = len(targetNode.Value)
|
|
|
|
}
|
2020-12-01 06:58:07 +00:00
|
|
|
case yaml.MappingNode:
|
|
|
|
length = len(targetNode.Content) / 2
|
|
|
|
case yaml.SequenceNode:
|
|
|
|
length = len(targetNode.Content)
|
|
|
|
default:
|
|
|
|
length = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
node := &yaml.Node{Kind: yaml.ScalarNode, Value: fmt.Sprintf("%v", length), Tag: "!!int"}
|
2021-11-23 22:57:35 +00:00
|
|
|
result := candidate.CreateReplacement(node)
|
2021-01-12 08:36:28 +00:00
|
|
|
results.PushBack(result)
|
2020-12-01 06:58:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
return context.ChildContext(results), nil
|
2020-12-01 06:58:07 +00:00
|
|
|
}
|