1package yqlib
2
3import (
4	"container/list"
5
6	yaml "gopkg.in/yaml.v3"
7)
8
9func assignTagOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
10
11	log.Debugf("AssignTagOperator: %v")
12	tag := ""
13
14	if !expressionNode.Operation.UpdateAssign {
15		rhs, err := d.GetMatchingNodes(context, expressionNode.Rhs)
16		if err != nil {
17			return Context{}, err
18		}
19
20		if rhs.MatchingNodes.Front() != nil {
21			tag = rhs.MatchingNodes.Front().Value.(*CandidateNode).Node.Value
22		}
23	}
24
25	lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs)
26
27	if err != nil {
28		return Context{}, err
29	}
30
31	for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
32		candidate := el.Value.(*CandidateNode)
33		log.Debugf("Setting tag of : %v", candidate.GetKey())
34		if expressionNode.Operation.UpdateAssign {
35			rhs, err := d.GetMatchingNodes(context.SingleChildContext(candidate), expressionNode.Rhs)
36			if err != nil {
37				return Context{}, err
38			}
39
40			if rhs.MatchingNodes.Front() != nil {
41				tag = rhs.MatchingNodes.Front().Value.(*CandidateNode).Node.Value
42			}
43		}
44		unwrapDoc(candidate.Node).Tag = tag
45	}
46
47	return context, nil
48}
49
50func getTagOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
51	log.Debugf("GetTagOperator")
52
53	var results = list.New()
54
55	for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
56		candidate := el.Value.(*CandidateNode)
57		node := &yaml.Node{Kind: yaml.ScalarNode, Value: unwrapDoc(candidate.Node).Tag, Tag: "!!str"}
58		result := candidate.CreateChild(nil, node)
59		results.PushBack(result)
60	}
61
62	return context.ChildContext(results), nil
63}
64