Skip to content

Commit

Permalink
docs/howto: use list.UniqueItems
Browse files Browse the repository at this point in the history
This adds a Commented CUE guide that demonstrates both how to use
list.UniqueItems to return a boolean, and how to use the function as a
field validator.

Field validation is shown operating on source data and on
comprehension-generated data which only contains a subset of the source
data.

The source data was selected to include a field name that evokes
Kubernetes configuration ("kind"), as list.UniqueItems has previously
been asked about within a Kubernetes context on Slack. *Actual*
Kubernetes configuration data would be inappropriate to display, here,
even as an example, as doing so would run the risk of the howto guide
being seen in some way as authoritative *for Kubernetes+CUE* - which is
not currently the intent for any page on cuelang.org.

Signed-off-by: Jonathan Matthews <[email protected]>
Preview-Path: /docs/howto/use-the-built-in-function-list-uniqueitems-to-enforce-complete-or-partial-list-item-uniqueness/
Change-Id: Ia956f9cd735d525394c3ee06d65831af4b243652
Dispatch-Trailer: {"type":"trybot","CL":1174648,"patchset":4,"ref":"refs/changes/48/1174648/4","targetBranch":"alpha"}
  • Loading branch information
jpluscplusm authored and cueckoo committed Jan 22, 2024
1 parent 2c23193 commit 66ea32d
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
title: Using the built-in function "list.UniqueItems" to enforce complete or partial list item completeness
tags:
- commented cue
authors:
- jpluscplusm
toc_hide: true
---

This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}})
demonstrates how to use the built-in function
[`list.UniqueItems`](https://pkg.go.dev/cuelang.org/go/pkg/list#UniqueItems)
to enforce that a list's items are either completely unique (taking all of each
item's fields into account), or that they're partially unique (taking only
*some* of each item's fields into account).

{{{with code "en" "cc"}}}
#location top bottom

! exec cue vet
cmp stderr out
-- file.cue --
package example

import "list"

// inline uses list.UniqueItems in its non-validator mode, returning a boolean
// value. Its value will be "true".
inline: list.UniqueItems([1, 2, "3", 4, "five"])

// iceCreams is a list of structs representing multiple brands' specific kinds
// (or flavours) of ice cream, along with optional names and prices.
// It is *not* mandatory to declare a list's schema in order to use the list
// with list.UniqueItems. We do so, here, for clarity.
iceCreams: [...{
brand!: string
kind!: string
name?: string
price?: number
}]

// Assert that iceCreams contains no duplicate items.
iceCreams: list.UniqueItems

// Assert that a synthetic list, constructed using a subset of the fields inside
// iceCreams' items, contains no duplicate items.
_flavoursByBrand: list.UniqueItems & [
for item in iceCreams {{
brand: item.brand
kind: item.kind
}},
]

// Assert that a synthetic list, constructed using a different subset of the
// fields inside iceCreams' items, contains no duplicate items.
_names: list.UniqueItems & [
for item in iceCreams
if item.name != _|_ {{
name: item.name
}},
]

iceCreams: [{
brand: "Charlie's Chocs"
kind: "vanilla"
name: "Vanilla Deluxe"
price: 5
}, {
brand: "Lola's Lollies"
kind: "strawberry"
}, {
brand: "Charlie's Chocs"
kind: "strawberry"
name: "Strawberry Delight"
price: 7.99
}, {
brand: "Charlie's Chocs"
kind: "vanilla"
name: "Vanilla Treat"
price: 5
}, {
brand: "Lola's Lollies"
kind: "vanilla"
name: "Vanilla Deluxe"
}]
-- out --
_flavoursByBrand: invalid value [{brand:"Charlie's Chocs",kind:"vanilla"},{brand:"Lola's Lollies",kind:"strawberry"},{brand:"Charlie's Chocs",kind:"strawberry"},{brand:"Charlie's Chocs",kind:"vanilla"},{brand:"Lola's Lollies",kind:"vanilla"}] (does not satisfy list.UniqueItems):
./file.cue:25:19
_names: invalid value [{name:"Vanilla Deluxe"},{name:"Strawberry Delight"},{name:"Vanilla Treat"},{name:"Vanilla Deluxe"}] (does not satisfy list.UniqueItems):
./file.cue:34:9
{{{end}}}

## Related content

- The [`list`](https://pkg.go.dev/cuelang.org/go/pkg/list) built-in package
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package site
{
content: {
docs: {
howto: {
"use-the-built-in-function-list-uniqueitems-to-enforce-complete-or-partial-list-item-uniqueness": {
page: {
cache: {
code: {
cc: "AmV/cSOME5Y8H5cRt98/Ep9rlidekNbIo+0/1jipAzM="
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package site

content: docs: howto: "use-the-built-in-function-list-uniqueitems-to-enforce-complete-or-partial-list-item-uniqueness": {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
title: Using the built-in function "list.UniqueItems" to enforce complete or partial list item completeness
tags:
- commented cue
authors:
- jpluscplusm
toc_hide: true
---

This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}})
demonstrates how to use the built-in function
[`list.UniqueItems`](https://pkg.go.dev/cuelang.org/go/pkg/list#UniqueItems)
to enforce that a list's items are either completely unique (taking all of each
item's fields into account), or that they're partially unique (taking only
*some* of each item's fields into account).

{{< code-tabs >}}
{{< code-tab name="file.cue" language="cue" area="top" >}}
package example

import "list"

// inline uses list.UniqueItems in its non-validator mode, returning a boolean
// value. Its value will be "true".
inline: list.UniqueItems([1, 2, "3", 4, "five"])

// iceCreams is a list of structs representing multiple brands' specific kinds
// (or flavours) of ice cream, along with optional names and prices.
// It is *not* mandatory to declare a list's schema in order to use the list
// with list.UniqueItems. We do so, here, for clarity.
iceCreams: [...{
brand!: string
kind!: string
name?: string
price?: number
}]

// Assert that iceCreams contains no duplicate items.
iceCreams: list.UniqueItems

// Assert that a synthetic list, constructed using a subset of the fields inside
// iceCreams' items, contains no duplicate items.
_flavoursByBrand: list.UniqueItems & [
for item in iceCreams {{
brand: item.brand
kind: item.kind
}},
]

// Assert that a synthetic list, constructed using a different subset of the
// fields inside iceCreams' items, contains no duplicate items.
_names: list.UniqueItems & [
for item in iceCreams
if item.name != _|_ {{
name: item.name
}},
]

iceCreams: [{
brand: "Charlie's Chocs"
kind: "vanilla"
name: "Vanilla Deluxe"
price: 5
}, {
brand: "Lola's Lollies"
kind: "strawberry"
}, {
brand: "Charlie's Chocs"
kind: "strawberry"
name: "Strawberry Delight"
price: 7.99
}, {
brand: "Charlie's Chocs"
kind: "vanilla"
name: "Vanilla Treat"
price: 5
}, {
brand: "Lola's Lollies"
kind: "vanilla"
name: "Vanilla Deluxe"
}]
{{< /code-tab >}}
{{< code-tab name="TERMINAL" language="" type="terminal" area="bottom" >}}
$ cue vet
_flavoursByBrand: invalid value [{brand:"Charlie's Chocs",kind:"vanilla"},{brand:"Lola's Lollies",kind:"strawberry"},{brand:"Charlie's Chocs",kind:"strawberry"},{brand:"Charlie's Chocs",kind:"vanilla"},{brand:"Lola's Lollies",kind:"vanilla"}] (does not satisfy list.UniqueItems):
./file.cue:25:19
_names: invalid value [{name:"Vanilla Deluxe"},{name:"Strawberry Delight"},{name:"Vanilla Treat"},{name:"Vanilla Deluxe"}] (does not satisfy list.UniqueItems):
./file.cue:34:9
{{< /code-tab >}}
{{< /code-tabs >}}

## Related content

- The [`list`](https://pkg.go.dev/cuelang.org/go/pkg/list) built-in package

0 comments on commit 66ea32d

Please sign in to comment.