From 5af062a86f477f89b73bb1ba99d56d28f69ac27a Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 1 Nov 2022 09:29:05 +1100 Subject: [PATCH] Added subset example --- examples/data1.yaml | 6 +---- pkg/yqlib/doc/operators/contains.md | 28 ++++++++++++++++++++- pkg/yqlib/doc/operators/headers/contains.md | 10 +++++++- pkg/yqlib/operator_contains_test.go | 9 +++++++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/examples/data1.yaml b/examples/data1.yaml index f1011d30..850ab075 100644 --- a/examples/data1.yaml +++ b/examples/data1.yaml @@ -1,5 +1 @@ -volumes: - beehive-conf: - driver: local -services: - - beehive \ No newline at end of file +["foobar", "foobaz", "blarp"] \ No newline at end of file diff --git a/pkg/yqlib/doc/operators/contains.md b/pkg/yqlib/doc/operators/contains.md index 046b4ca2..dfdd5a04 100644 --- a/pkg/yqlib/doc/operators/contains.md +++ b/pkg/yqlib/doc/operators/contains.md @@ -1,6 +1,14 @@ # Contains -This returns `true` if the context contains the passed in parameter, and false otherwise. +This returns `true` if the context contains the passed in parameter, and false otherwise. For arrays, this will return true if the passed in array is contained within the array. For strings, it will return true if the string is a substring. + +{% hint style="warning" %} + +_Note_ that, just like jq, when checking if an array of strings `contains` another, this will use `contains` and _not_ equals to check each string. This means an array like `contains(["cat"])` will return true for an array `["cats"]`. + +See the "Array has a subset array" example below on how to check for a subset. + +{% endhint %} ## Array contains array Array is equal or subset of @@ -20,6 +28,24 @@ will output true ``` +## Array has a subset array +Subtract the superset array from the subset, if there's anything left, it's not a subset + +Given a sample.yml file of: +```yaml +- foobar +- foobaz +- blarp +``` +then +```bash +yq '["baz", "bar"] - . | length == 0' sample.yml +``` +will output +```yaml +false +``` + ## Object included in array Given a sample.yml file of: ```yaml diff --git a/pkg/yqlib/doc/operators/headers/contains.md b/pkg/yqlib/doc/operators/headers/contains.md index 9d549cbc..fad851e6 100644 --- a/pkg/yqlib/doc/operators/headers/contains.md +++ b/pkg/yqlib/doc/operators/headers/contains.md @@ -1,3 +1,11 @@ # Contains -This returns `true` if the context contains the passed in parameter, and false otherwise. +This returns `true` if the context contains the passed in parameter, and false otherwise. For arrays, this will return true if the passed in array is contained within the array. For strings, it will return true if the string is a substring. + +{% hint style="warning" %} + +_Note_ that, just like jq, when checking if an array of strings `contains` another, this will use `contains` and _not_ equals to check each string. This means an array like `contains(["cat"])` will return true for an array `["cats"]`. + +See the "Array has a subset array" example below on how to check for a subset. + +{% endhint %} diff --git a/pkg/yqlib/operator_contains_test.go b/pkg/yqlib/operator_contains_test.go index 5cef9d7c..f714ace2 100644 --- a/pkg/yqlib/operator_contains_test.go +++ b/pkg/yqlib/operator_contains_test.go @@ -33,6 +33,15 @@ var containsOperatorScenarios = []expressionScenario{ "D0, P[], (!!bool)::true\n", }, }, + { + description: "Array has a subset array", + subdescription: "Subtract the superset array from the subset, if there's anything left, it's not a subset", + document: `["foobar", "foobaz", "blarp"]`, + expression: `["baz", "bar"] - . | length == 0`, + expected: []string{ + "D0, P[], (!!bool)::false\n", + }, + }, { skipDoc: true, expression: `["dog", "cat", "giraffe"] | contains(["camel"])`,