mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Added more tests
This commit is contained in:
parent
b44fecdfa5
commit
733e63d1fb
@ -34,6 +34,49 @@ will output
|
||||
- 1
|
||||
```
|
||||
|
||||
## Retrieve array key
|
||||
Given a sample.yml file of:
|
||||
```yaml
|
||||
- 1
|
||||
- 2
|
||||
- 3
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq eval '.[1] | key' sample.yml
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
1
|
||||
```
|
||||
|
||||
## Retrieve map key
|
||||
Given a sample.yml file of:
|
||||
```yaml
|
||||
a: thing
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq eval '.a | key' sample.yml
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
a
|
||||
```
|
||||
|
||||
## No key
|
||||
Given a sample.yml file of:
|
||||
```yaml
|
||||
{}
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq eval 'key' sample.yml
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
```
|
||||
|
||||
## Update map key
|
||||
Given a sample.yml file of:
|
||||
```yaml
|
||||
|
46
pkg/yqlib/doc/parent.md
Normal file
46
pkg/yqlib/doc/parent.md
Normal file
@ -0,0 +1,46 @@
|
||||
|
||||
## Simple example
|
||||
Given a sample.yml file of:
|
||||
```yaml
|
||||
a:
|
||||
nested: cat
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq eval '.a.nested | parent' sample.yml
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
nested: cat
|
||||
```
|
||||
|
||||
## Show parent
|
||||
Given a sample.yml file of:
|
||||
```yaml
|
||||
a:
|
||||
fruit: apple
|
||||
b:
|
||||
fruit: banana
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq eval '.. | select(. == "banana") | parent' sample.yml
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
fruit: banana
|
||||
```
|
||||
|
||||
## No parent
|
||||
Given a sample.yml file of:
|
||||
```yaml
|
||||
{}
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq eval 'parent' sample.yml
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
```
|
||||
|
@ -369,6 +369,7 @@ func initLexer() (*lex.Lexer, error) {
|
||||
|
||||
lexer.Add([]byte(`split`), opToken(splitStringOpType))
|
||||
|
||||
lexer.Add([]byte(`parent`), opToken(getParentOpType))
|
||||
lexer.Add([]byte(`key`), opToken(getKeyOpType))
|
||||
lexer.Add([]byte(`keys`), opToken(keysOpType))
|
||||
|
||||
|
@ -80,6 +80,7 @@ var getStyleOpType = &operationType{Type: "GET_STYLE", NumArgs: 0, Precedence: 5
|
||||
var getTagOpType = &operationType{Type: "GET_TAG", NumArgs: 0, Precedence: 50, Handler: getTagOperator}
|
||||
|
||||
var getKeyOpType = &operationType{Type: "GET_KEY", NumArgs: 0, Precedence: 50, Handler: getKeyOperator}
|
||||
var getParentOpType = &operationType{Type: "GET_PARENT", NumArgs: 0, Precedence: 50, Handler: getParentOperator}
|
||||
|
||||
var getCommentOpType = &operationType{Type: "GET_COMMENT", NumArgs: 0, Precedence: 50, Handler: getCommentsOperator}
|
||||
var getAnchorOpType = &operationType{Type: "GET_ANCHOR", NumArgs: 0, Precedence: 50, Handler: getAnchorOperator}
|
||||
|
@ -15,7 +15,9 @@ func getKeyOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
|
||||
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
||||
candidate := el.Value.(*CandidateNode)
|
||||
|
||||
results.PushBack(candidate.CreateReplacement(candidate.Key))
|
||||
if candidate.Key != nil {
|
||||
results.PushBack(candidate.CreateReplacement(candidate.Key))
|
||||
}
|
||||
}
|
||||
|
||||
return context.ChildContext(results), nil
|
||||
|
@ -37,6 +37,28 @@ var keysOperatorScenarios = []expressionScenario{
|
||||
"D0, P[], (!!seq)::[]\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "Retrieve array key",
|
||||
document: "[1,2,3]",
|
||||
expression: `.[1] | key`,
|
||||
expected: []string{
|
||||
"D0, P[1], (!!int)::1\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "Retrieve map key",
|
||||
document: "a: thing",
|
||||
expression: `.a | key`,
|
||||
expected: []string{
|
||||
"D0, P[a], (!!str)::a\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "No key",
|
||||
document: "{}",
|
||||
expression: `key`,
|
||||
expected: []string{},
|
||||
},
|
||||
{
|
||||
description: "Update map key",
|
||||
document: "a:\n x: 3\n y: 4",
|
||||
|
19
pkg/yqlib/operator_parent.go
Normal file
19
pkg/yqlib/operator_parent.go
Normal file
@ -0,0 +1,19 @@
|
||||
package yqlib
|
||||
|
||||
import "container/list"
|
||||
|
||||
func getParentOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
||||
log.Debugf("-- getParentOperator")
|
||||
|
||||
var results = list.New()
|
||||
|
||||
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
||||
candidate := el.Value.(*CandidateNode)
|
||||
if candidate.Parent != nil {
|
||||
results.PushBack(candidate.Parent)
|
||||
}
|
||||
}
|
||||
|
||||
return context.ChildContext(results), nil
|
||||
|
||||
}
|
37
pkg/yqlib/operator_parent_test.go
Normal file
37
pkg/yqlib/operator_parent_test.go
Normal file
@ -0,0 +1,37 @@
|
||||
package yqlib
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var parentOperatorScenarios = []expressionScenario{
|
||||
{
|
||||
description: "Simple example",
|
||||
document: `a: {nested: cat}`,
|
||||
expression: `.a.nested | parent`,
|
||||
expected: []string{
|
||||
"D0, P[a], (!!map)::{nested: cat}\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "Show parent",
|
||||
document: `{a: {fruit: apple}, b: {fruit: banana}}`,
|
||||
expression: `.. | select(. == "banana") | parent`,
|
||||
expected: []string{
|
||||
"D0, P[b], (!!map)::{fruit: banana}\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "No parent",
|
||||
document: `{}`,
|
||||
expression: `parent`,
|
||||
expected: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
func TestParentOperatorScenarios(t *testing.T) {
|
||||
for _, tt := range parentOperatorScenarios {
|
||||
testScenario(t, &tt)
|
||||
}
|
||||
documentScenarios(t, "parent", parentOperatorScenarios)
|
||||
}
|
Loading…
Reference in New Issue
Block a user