2020-12-01 06:58:07 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
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)
|
2023-04-08 10:17:13 +00:00
|
|
|
targetNode := candidate.unwrapDocument()
|
2020-12-01 06:58:07 +00:00
|
|
|
var length int
|
|
|
|
switch targetNode.Kind {
|
2023-04-08 10:17:13 +00:00
|
|
|
case ScalarNode:
|
2021-01-25 23:21:16 +00:00
|
|
|
if targetNode.Tag == "!!null" {
|
|
|
|
length = 0
|
|
|
|
} else {
|
|
|
|
length = len(targetNode.Value)
|
|
|
|
}
|
2023-04-08 10:17:13 +00:00
|
|
|
case MappingNode:
|
2020-12-01 06:58:07 +00:00
|
|
|
length = len(targetNode.Content) / 2
|
2023-04-08 10:17:13 +00:00
|
|
|
case SequenceNode:
|
2020-12-01 06:58:07 +00:00
|
|
|
length = len(targetNode.Content)
|
|
|
|
default:
|
|
|
|
length = 0
|
|
|
|
}
|
|
|
|
|
2023-04-08 11:27:47 +00:00
|
|
|
result := candidate.CreateReplacement(ScalarNode, "!!int", fmt.Sprintf("%v", length))
|
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
|
|
|
}
|