mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-23 14:16:10 +00:00
Can assign-update tag
This commit is contained in:
parent
5a1b81cbfc
commit
2a6e423d2d
@ -22,7 +22,21 @@ will output
|
||||
!!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:
|
||||
```yaml
|
||||
a: cat
|
||||
|
@ -9,16 +9,18 @@ import (
|
||||
func AssignTagOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
|
||||
|
||||
log.Debugf("AssignTagOperator: %v")
|
||||
tag := ""
|
||||
|
||||
if !pathNode.Operation.UpdateAssign {
|
||||
rhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tag := ""
|
||||
|
||||
if rhs.Front() != nil {
|
||||
tag = rhs.Front().Value.(*CandidateNode).Node.Value
|
||||
}
|
||||
}
|
||||
|
||||
lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
|
||||
|
||||
@ -26,11 +28,19 @@ func AssignTagOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// FAIL HERE
|
||||
|
||||
for el := lhs.Front(); el != nil; el = el.Next() {
|
||||
candidate := el.Value.(*CandidateNode)
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -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}`,
|
||||
expression: `(.. | select(tag == "!!int")) tag= "!!str"`,
|
||||
expected: []string{
|
||||
"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) {
|
||||
|
Loading…
Reference in New Issue
Block a user