-
-
Notifications
You must be signed in to change notification settings - Fork 786
feat: add in operator (inverse of has)
#2721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
|
|
||
| ## Check key exists in map using variable binding | ||
| Given a sample.yml file of: | ||
| ```yaml | ||
| a: 1 | ||
| b: 2 | ||
| c: 3 | ||
| ``` | ||
| then | ||
| ```bash | ||
| yq '. as $m | "a" | in($m)' sample.yml | ||
| ``` | ||
| will output | ||
| ```yaml | ||
| true | ||
| ``` | ||
|
|
||
| ## Check key does not exist in map | ||
| Given a sample.yml file of: | ||
| ```yaml | ||
| a: 1 | ||
| b: 2 | ||
| c: 3 | ||
| ``` | ||
| then | ||
| ```bash | ||
| yq '. as $m | "d" | in($m)' sample.yml | ||
| ``` | ||
| will output | ||
| ```yaml | ||
| false | ||
| ``` | ||
|
|
||
| ## Check value exists in array | ||
| Given a sample.yml file of: | ||
| ```yaml | ||
| - Tool | ||
| - Food | ||
| - Flower | ||
| ``` | ||
| then | ||
| ```bash | ||
| yq '. as $m | "Food" | in($m)' sample.yml | ||
| ``` | ||
| will output | ||
| ```yaml | ||
| true | ||
| ``` | ||
|
|
||
| ## Check value does not exist in array | ||
| Given a sample.yml file of: | ||
| ```yaml | ||
| - Tool | ||
| - Food | ||
| - Flower | ||
| ``` | ||
| then | ||
| ```bash | ||
| yq '. as $m | "Animal" | in($m)' sample.yml | ||
| ``` | ||
| will output | ||
| ```yaml | ||
| false | ||
| ``` | ||
|
|
||
| ## Check in with select on array elements | ||
| Filter items whose type is in the given list | ||
|
|
||
| Given a sample.yml file of: | ||
| ```yaml | ||
| - item: Pizza | ||
| type: Food | ||
| - item: Rose | ||
| type: Flower | ||
| - item: Hammer | ||
| type: Tool | ||
| ``` | ||
| then | ||
| ```bash | ||
| yq '.[] | select(.type | in(["Tool", "Food"]))' sample.yml | ||
| ``` | ||
| will output | ||
| ```yaml | ||
| item: Pizza | ||
| type: Food | ||
| item: Hammer | ||
| type: Tool | ||
| ``` | ||
|
|
||
| ## In with variable binding - found | ||
| Given a sample.yml file of: | ||
| ```yaml | ||
| a: 1 | ||
| b: 2 | ||
| c: 3 | ||
| ``` | ||
| then | ||
| ```bash | ||
| yq '. as $m | "b" | in($m)' sample.yml | ||
| ``` | ||
| will output | ||
| ```yaml | ||
| true | ||
| ``` | ||
|
|
||
| ## In with variable binding - not found | ||
| Given a sample.yml file of: | ||
| ```yaml | ||
| a: 1 | ||
| b: 2 | ||
| c: 3 | ||
| ``` | ||
| then | ||
| ```bash | ||
| yq '. as $m | "z" | in($m)' sample.yml | ||
| ``` | ||
| will output | ||
| ```yaml | ||
| false | ||
| ``` | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package yqlib | ||
|
|
||
| import ( | ||
| "container/list" | ||
| ) | ||
|
|
||
| func inOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { | ||
|
|
||
| log.Debugf("inOperation") | ||
| var results = list.New() | ||
|
|
||
| rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS) | ||
|
|
||
| if err != nil { | ||
| return Context{}, err | ||
| } | ||
|
|
||
| var collection *CandidateNode | ||
| if rhs.MatchingNodes.Len() != 0 { | ||
| collection = rhs.MatchingNodes.Front().Value.(*CandidateNode) | ||
| } else { | ||
| // no collection provided, return false for all | ||
| for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { | ||
| candidate := el.Value.(*CandidateNode) | ||
| results.PushBack(createBooleanCandidate(candidate, false)) | ||
| } | ||
| return context.ChildContext(results), nil | ||
| } | ||
|
|
||
| for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { | ||
| candidate := el.Value.(*CandidateNode) | ||
| isIn := false | ||
|
|
||
| switch collection.Kind { | ||
| case MappingNode: | ||
| // Check if candidate value exists as a key in the mapping | ||
| for index := 0; index < len(collection.Content); index = index + 2 { | ||
| key := collection.Content[index] | ||
| if key.Value == candidate.Value { | ||
| isIn = true | ||
| break | ||
| } | ||
| } | ||
| case SequenceNode: | ||
| // Check if candidate value is present in the sequence (value membership) | ||
| for _, element := range collection.Content { | ||
| if element.Value == candidate.Value { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Blocking: Comparing only yq -n '{"x": 99} | in([{"a": 1}, {"b": 2}])' # true — should be falsePlease use |
||
| isIn = true | ||
| break | ||
| } | ||
| } | ||
| default: | ||
| isIn = false | ||
| } | ||
|
|
||
| results.PushBack(createBooleanCandidate(candidate, isIn)) | ||
| } | ||
| return context.ChildContext(results), nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package yqlib | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| var inOperatorScenarios = []expressionScenario{ | ||
| { | ||
| description: "Check key exists in map using variable binding", | ||
| document: "a: 1\nb: 2\nc: 3\n", | ||
| expression: `. as $m | "a" | in($m)`, | ||
| expected: []string{ | ||
| "D0, P[], (!!bool)::true\n", | ||
| }, | ||
| }, | ||
| { | ||
| description: "Check key does not exist in map", | ||
| document: "a: 1\nb: 2\nc: 3\n", | ||
| expression: `. as $m | "d" | in($m)`, | ||
| expected: []string{ | ||
| "D0, P[], (!!bool)::false\n", | ||
| }, | ||
| }, | ||
| { | ||
| description: "Check value exists in array", | ||
| document: "- Tool\n- Food\n- Flower\n", | ||
| expression: `. as $m | "Food" | in($m)`, | ||
| expected: []string{ | ||
| "D0, P[], (!!bool)::true\n", | ||
| }, | ||
| }, | ||
| { | ||
| description: "Check value does not exist in array", | ||
| document: "- Tool\n- Food\n- Flower\n", | ||
| expression: `. as $m | "Animal" | in($m)`, | ||
| expected: []string{ | ||
| "D0, P[], (!!bool)::false\n", | ||
| }, | ||
| }, | ||
| { | ||
| description: "Check in with select on array elements", | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Please add test coverage for the non-scalar array bug, e.g.: {
skipDoc: true,
expression: `{"x": 99} | in([{"a": 1}, {"b": 2}])`,
expected: []string{"D0, P[], (!!bool)::false\n"},
},Also consider tests for tag mismatch ( |
||
| subdescription: "Filter items whose type is in the given list", | ||
| document: "- {item: Pizza, type: Food}\n- {item: Rose, type: Flower}\n- {item: Hammer, type: Tool}\n", | ||
| expression: `.[] | select(.type | in(["Tool", "Food"]))`, | ||
| expected: []string{ | ||
| "D0, P[0], (!!map)::{item: Pizza, type: Food}\n", | ||
| "D0, P[2], (!!map)::{item: Hammer, type: Tool}\n", | ||
| }, | ||
| }, | ||
| { | ||
| description: "In with variable binding - found", | ||
| document: "a: 1\nb: 2\nc: 3\n", | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
These "In with variable binding" scenarios duplicate "Check key exists/does not exist in map" above (lines 9–23). Consider removing the redundant pair to keep the test file lean. |
||
| expression: `. as $m | "b" | in($m)`, | ||
| expected: []string{ | ||
| "D0, P[], (!!bool)::true\n", | ||
| }, | ||
| }, | ||
| { | ||
| description: "In with variable binding - not found", | ||
| document: "a: 1\nb: 2\nc: 3\n", | ||
| expression: `. as $m | "z" | in($m)`, | ||
| expected: []string{ | ||
| "D0, P[], (!!bool)::false\n", | ||
| }, | ||
| }, | ||
| { | ||
| skipDoc: true, | ||
| document: "- one\n- two\n- three\n", | ||
| expression: `. as $m | "one" | in($m)`, | ||
| expected: []string{ | ||
| "D0, P[], (!!bool)::true\n", | ||
| }, | ||
| }, | ||
| { | ||
| skipDoc: true, | ||
| document: "- one\n- two\n- three\n", | ||
| expression: `. as $m | "five" | in($m)`, | ||
| expected: []string{ | ||
| "D0, P[], (!!bool)::false\n", | ||
| }, | ||
| }, | ||
| { | ||
| skipDoc: true, | ||
| document: "key: value\nother: stuff\n", | ||
| expression: `. as $m | "key" | in($m)`, | ||
| expected: []string{ | ||
| "D0, P[], (!!bool)::true\n", | ||
| }, | ||
| }, | ||
| { | ||
| skipDoc: true, | ||
| document: "key: value\nother: stuff\n", | ||
| expression: `. as $m | "missing" | in($m)`, | ||
| expected: []string{ | ||
| "D0, P[], (!!bool)::false\n", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| func TestInOperatorScenarios(t *testing.T) { | ||
| for _, tt := range inOperatorScenarios { | ||
| testScenario(t, &tt) | ||
| } | ||
| documentOperatorScenarios(t, "in", inOperatorScenarios) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.