Compare commits

...

5 Commits

Author SHA1 Message Date
Tony
2252ed9f50
Merge b6285c5922 into e2f1d5ccf7 2026-07-02 09:56:37 -04:00
dependabot[bot]
e2f1d5ccf7
Bump go.yaml.in/yaml/v4 from 4.0.0-rc.5 to 4.0.0-rc.6 (#2759)
Bumps [go.yaml.in/yaml/v4](https://github.com/yaml/go-yaml) from 4.0.0-rc.5 to 4.0.0-rc.6.
- [Commits](https://github.com/yaml/go-yaml/compare/v4.0.0-rc.5...v4.0.0-rc.6)

---
updated-dependencies:
- dependency-name: go.yaml.in/yaml/v4
  dependency-version: 4.0.0-rc.6
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-06-29 16:54:05 +10:00
dependabot[bot]
16f149b351
Bump github.com/pelletier/go-toml/v2 from 2.4.0 to 2.4.2 (#2762)
Bumps [github.com/pelletier/go-toml/v2](https://github.com/pelletier/go-toml) from 2.4.0 to 2.4.2.
- [Release notes](https://github.com/pelletier/go-toml/releases)
- [Commits](https://github.com/pelletier/go-toml/compare/v2.4.0...v2.4.2)

---
updated-dependencies:
- dependency-name: github.com/pelletier/go-toml/v2
  dependency-version: 2.4.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-06-29 16:53:39 +10:00
dependabot[bot]
5da9215306
Bump actions/setup-go from 6.4.0 to 6.5.0 (#2763)
Bumps [actions/setup-go](https://github.com/actions/setup-go) from 6.4.0 to 6.5.0.
- [Release notes](https://github.com/actions/setup-go/releases)
- [Commits](4a3601121d...924ae3a1cd)

---
updated-dependencies:
- dependency-name: actions/setup-go
  dependency-version: 6.5.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-06-29 16:53:18 +10:00
Tony
b6285c5922 feat: add in operator (inverse of has)
Add the `in` operator to check if a value is present in a mapping (as a
key) or sequence (as an element). This mirrors jq's `in` operator which
is defined as the inverse of `has`.

Usage:
  . as $m | "key" | in($m)        -- check map key existence
  . as $m | "value" | in($m)      -- check array value membership
  .[] | select(.type | in(["a","b"])) -- filter by membership

Fixes #2322
2026-05-27 16:33:32 +08:00
9 changed files with 295 additions and 8 deletions

View File

@ -37,7 +37,7 @@ jobs:
steps:
- name: Set up Go
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0
with:
go-version: '^1.20'
id: go

View File

@ -15,7 +15,7 @@ jobs:
id-token: write
steps:
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
- uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
- uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0
with:
go-version: '^1.20'
check-latest: true

4
go.mod
View File

@ -13,13 +13,13 @@ require (
github.com/hashicorp/hcl/v2 v2.24.0
github.com/jinzhu/copier v0.4.0
github.com/magiconair/properties v1.8.10
github.com/pelletier/go-toml/v2 v2.4.0
github.com/pelletier/go-toml/v2 v2.4.2
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
github.com/yuin/gopher-lua v1.1.2
github.com/zclconf/go-cty v1.18.1
go.yaml.in/yaml/v4 v4.0.0-rc.5
go.yaml.in/yaml/v4 v4.0.0-rc.6
golang.org/x/mod v0.37.0
golang.org/x/net v0.56.0
golang.org/x/text v0.38.0

8
go.sum
View File

@ -46,8 +46,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/pelletier/go-toml/v2 v2.4.0 h1:Mwu0mAkUKbittDs3/ADDWXqMmq3EOK2VHiuCkV00Row=
github.com/pelletier/go-toml/v2 v2.4.0/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/pelletier/go-toml/v2 v2.4.2 h1:M2fKKbmyvI+hGId/D0W64qDBMVhJnNR10O5gIbMc//Q=
github.com/pelletier/go-toml/v2 v2.4.2/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@ -68,8 +68,8 @@ github.com/zclconf/go-cty v1.18.1/go.mod h1:qpnV6EDNgC1sns/AleL1fvatHw72j+S+nS+M
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6ZMSMNJFMOjqrGHynW3DIBuR2H9j0ug+Mo=
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
go.yaml.in/yaml/v4 v4.0.0-rc.5 h1:JVliQq9EGOYaTgMi+k8BhUJyqcGk4ZqeuiN1Cirba9c=
go.yaml.in/yaml/v4 v4.0.0-rc.5/go.mod h1:aZqd9kCMsGL7AuUv/m/PvWLdg5sjJsZ4oHDEnfPPfY0=
go.yaml.in/yaml/v4 v4.0.0-rc.6 h1:1h7H1ohdUh93/FyE4YaDa1Zh64K6VVbjF4K6WUxMtH4=
go.yaml.in/yaml/v4 v4.0.0-rc.6/go.mod h1:aZqd9kCMsGL7AuUv/m/PvWLdg5sjJsZ4oHDEnfPPfY0=
golang.org/x/mod v0.37.0 h1:vF1DjpVEshcIqoEaauuHebaLk1O1forxjxBaVn884JQ=
golang.org/x/mod v0.37.0/go.mod h1:m8S8VeM9r4dzDwjrKO0a1sZP3YjeMamRRlD+fmR2Q/0=
golang.org/x/net v0.56.0 h1:Rw8j/hFzGvJUZwNBXnAtf5sVDVt+65SK2C7IxCxZt5o=

View File

@ -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
```

View File

@ -102,6 +102,7 @@ var participleYqRules = []*participleYqRule{
simpleOp("select", selectOpType),
simpleOp("has", hasOpType),
simpleOp("in", inOpType),
simpleOp("unique_?by", uniqueByOpType),
simpleOp("unique", uniqueOpType),

View File

@ -191,6 +191,7 @@ var recursiveDescentOpType = &operationType{Type: "RECURSIVE_DESCENT", NumArgs:
var selectOpType = &operationType{Type: "SELECT", NumArgs: 1, Precedence: 52, Handler: selectOperator, CheckForPostTraverse: true}
var hasOpType = &operationType{Type: "HAS", NumArgs: 1, Precedence: 50, Handler: hasOperator}
var inOpType = &operationType{Type: "IN", NumArgs: 1, Precedence: 50, Handler: inOperator}
var uniqueOpType = &operationType{Type: "UNIQUE", NumArgs: 0, Precedence: 52, Handler: unique, CheckForPostTraverse: true}
var uniqueByOpType = &operationType{Type: "UNIQUE_BY", NumArgs: 1, Precedence: 52, Handler: uniqueBy, CheckForPostTraverse: true}
var groupByOpType = &operationType{Type: "GROUP_BY", NumArgs: 1, Precedence: 52, Handler: groupBy, CheckForPostTraverse: true}

59
pkg/yqlib/operator_in.go Normal file
View File

@ -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 {
isIn = true
break
}
}
default:
isIn = false
}
results.PushBack(createBooleanCandidate(candidate, isIn))
}
return context.ChildContext(results), nil
}

View File

@ -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",
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",
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)
}