Can assign-update tag

This commit is contained in:
Mike Farah 2021-01-06 20:44:28 +11:00
parent 5a1b81cbfc
commit 2a6e423d2d
3 changed files with 51 additions and 11 deletions

View File

@ -22,7 +22,21 @@ will output
!!seq !!seq
``` ```
## Convert numbers to strings ## Set custom tag
Given a sample.yml file of:
```yaml
a: str
```
then
```bash
yq eval '.a tag = "!!mikefarah"' sample.yml
```
will output
```yaml
a: !!mikefarah str
```
## Find numbers and convert them to strings
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat

View File

@ -9,15 +9,17 @@ import (
func AssignTagOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { func AssignTagOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
log.Debugf("AssignTagOperator: %v") log.Debugf("AssignTagOperator: %v")
rhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs)
if err != nil {
return nil, err
}
tag := "" tag := ""
if rhs.Front() != nil { if !pathNode.Operation.UpdateAssign {
tag = rhs.Front().Value.(*CandidateNode).Node.Value rhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs)
if err != nil {
return nil, err
}
if rhs.Front() != nil {
tag = rhs.Front().Value.(*CandidateNode).Node.Value
}
} }
lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs) lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
@ -26,11 +28,19 @@ func AssignTagOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode
return nil, err return nil, err
} }
// FAIL HERE
for el := lhs.Front(); el != nil; el = el.Next() { for el := lhs.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
log.Debugf("Setting tag of : %v", candidate.GetKey()) log.Debugf("Setting tag of : %v", candidate.GetKey())
if pathNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs)
if err != nil {
return nil, err
}
if rhs.Front() != nil {
tag = rhs.Front().Value.(*CandidateNode).Node.Value
}
}
candidate.Node.Tag = tag candidate.Node.Tag = tag
} }

View File

@ -27,13 +27,29 @@ var tagOperatorScenarios = []expressionScenario{
}, },
}, },
{ {
description: "Convert numbers to strings", description: "Set custom tag",
document: `{a: str}`,
expression: `.a tag = "!!mikefarah"`,
expected: []string{
"D0, P[], (doc)::{a: !!mikefarah str}\n",
},
},
{
description: "Find numbers and convert them to strings",
document: `{a: cat, b: 5, c: 3.2, e: true}`, document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `(.. | select(tag == "!!int")) tag= "!!str"`, expression: `(.. | select(tag == "!!int")) tag= "!!str"`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::{a: cat, b: \"5\", c: 3.2, e: true}\n", "D0, P[], (!!map)::{a: cat, b: \"5\", c: 3.2, e: true}\n",
}, },
}, },
{
skipDoc: true,
document: `{a: "!!frog", b: "!!customTag"}`,
expression: `.[] tag |= .`,
expected: []string{
"D0, P[], (doc)::{a: !!frog \"!!frog\", b: !!customTag \"!!customTag\"}\n",
},
},
} }
func TestTagOperatorScenarios(t *testing.T) { func TestTagOperatorScenarios(t *testing.T) {