-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs/howto: ensure a list's values are in another
This adds a guide demonstrating how to use CUE's built-in "or" function to ensure one list contains only values found in another list. It's also possible to use a "matchN"-based approach; cue-lang/docs-and-content#169 is left open to track this. For cue-lang/docs-and-content#169. Signed-off-by: Jonathan Matthews <[email protected]> Preview-Path: /docs/howto/ensure-list-values-present-in-another-list/ Change-Id: Idc7a60d0290f26a2376189bdd4af56257ac3c8b2 Dispatch-Trailer: {"type":"trybot","CL":1206329,"patchset":3,"ref":"refs/changes/29/1206329/3","targetBranch":"master"}
- Loading branch information
1 parent
203970d
commit 72b2f04
Showing
4 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
content/docs/howto/ensure-list-values-present-in-another-list/en.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
title: Ensuring a list's values are present in another list | ||
tags: [commented cue] | ||
authors: [jpluscplusm] | ||
toc_hide: true | ||
--- | ||
|
||
This [Commented CUE]({{< relref "docs/howto/about-commented-cue-guides" >}}) | ||
demonstrates how to make sure that a list of concrete, simple values only | ||
contains elements that are *also* present in another list. | ||
In other words, how to ensure that one list is a "subset" of another list (even | ||
though a list isn't a set, strictly speaking). | ||
|
||
{{{with code "en" "cc"}}} | ||
! exec cue vet | ||
cmp stderr out | ||
-- file.cue -- | ||
package example | ||
|
||
_X: ["a", "b", "c"] | ||
|
||
A: ["a", "a", "b", "a"] | ||
// A must contain only values in _X. | ||
A: [...or(_X)] | ||
|
||
B: ["a", "b", "b", "E"] | ||
// B must be a subset of _X. | ||
B: [...or(_X)] | ||
-- out -- | ||
B.3: 3 errors in empty disjunction: | ||
B.3: conflicting values "a" and "E": | ||
./file.cue:3:6 | ||
./file.cue:9:20 | ||
./file.cue:11:5 | ||
B.3: conflicting values "b" and "E": | ||
./file.cue:3:11 | ||
./file.cue:9:20 | ||
./file.cue:11:5 | ||
B.3: conflicting values "c" and "E": | ||
./file.cue:3:16 | ||
./file.cue:9:20 | ||
./file.cue:11:5 | ||
{{{end}}} | ||
|
||
{{<info>}} | ||
This guide shows some lists of concrete and simple values being validated against another list. | ||
|
||
The technique it demonstrates can also be used to validate incomplete | ||
(non-concrete) and composite (struct and list) values, but the rules around its | ||
use are nuanced and evolving. | ||
|
||
{{<issue 2583>}}Issue #2583{{</issue>}} tracks some open questions about | ||
comparability in CUE that are worth considering before using this technique | ||
to validate more complex values. | ||
{{</info>}} | ||
|
||
## Related content | ||
|
||
- {{<issue 2583>}}Issue #2583{{</issue>}} | ||
- Reference: [The CUE Language Specification: Comparison operators]({{< relref "docs/reference/spec" >}}#comparison-operators) | ||
-- CUE's comparability rules |
18 changes: 18 additions & 0 deletions
18
content/docs/howto/ensure-list-values-present-in-another-list/gen_cache.cue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package site | ||
{ | ||
content: { | ||
docs: { | ||
howto: { | ||
"ensure-list-values-present-in-another-list": { | ||
page: { | ||
cache: { | ||
code: { | ||
cc: "p1xfu/V/M+aXC1NquQmtqUOdWXDr1/ytKbuX6Oxh/2I=" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
content/docs/howto/ensure-list-values-present-in-another-list/page.cue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package site | ||
|
||
content: docs: howto: "ensure-list-values-present-in-another-list": page: _ |
62 changes: 62 additions & 0 deletions
62
hugo/content/en/docs/howto/ensure-list-values-present-in-another-list/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
title: Ensuring a list's values are present in another list | ||
tags: [commented cue] | ||
authors: [jpluscplusm] | ||
toc_hide: true | ||
--- | ||
|
||
This [Commented CUE]({{< relref "docs/howto/about-commented-cue-guides" >}}) | ||
demonstrates how to make sure that a list of concrete, simple values only | ||
contains elements that are *also* present in another list. | ||
In other words, how to ensure that one list is a "subset" of another list (even | ||
though a list isn't a set, strictly speaking). | ||
|
||
{{< code-tabs >}} | ||
{{< code-tab name="file.cue" language="cue" area="top-left" >}} | ||
package example | ||
|
||
_X: ["a", "b", "c"] | ||
|
||
A: ["a", "a", "b", "a"] | ||
// A must contain only values in _X. | ||
A: [...or(_X)] | ||
|
||
B: ["a", "b", "b", "E"] | ||
// B must be a subset of _X. | ||
B: [...or(_X)] | ||
{{< /code-tab >}} | ||
{{< code-tab name="TERMINAL" language="" area="top-right" type="terminal" codetocopy="Y3VlIHZldA==" >}} | ||
$ cue vet | ||
B.3: 3 errors in empty disjunction: | ||
B.3: conflicting values "a" and "E": | ||
./file.cue:3:6 | ||
./file.cue:9:20 | ||
./file.cue:11:5 | ||
B.3: conflicting values "b" and "E": | ||
./file.cue:3:11 | ||
./file.cue:9:20 | ||
./file.cue:11:5 | ||
B.3: conflicting values "c" and "E": | ||
./file.cue:3:16 | ||
./file.cue:9:20 | ||
./file.cue:11:5 | ||
{{< /code-tab >}} | ||
{{< /code-tabs >}} | ||
|
||
{{<info>}} | ||
This guide shows some lists of concrete and simple values being validated against another list. | ||
|
||
The technique it demonstrates can also be used to validate incomplete | ||
(non-concrete) and composite (struct and list) values, but the rules around its | ||
use are nuanced and evolving. | ||
|
||
{{<issue 2583>}}Issue #2583{{</issue>}} tracks some open questions about | ||
comparability in CUE that are worth considering before using this technique | ||
to validate more complex values. | ||
{{</info>}} | ||
|
||
## Related content | ||
|
||
- {{<issue 2583>}}Issue #2583{{</issue>}} | ||
- Reference: [The CUE Language Specification: Comparison operators]({{< relref "docs/reference/spec" >}}#comparison-operators) | ||
-- CUE's comparability rules |