Skip to content

Commit

Permalink
docs/howto: simplify list-injection example
Browse files Browse the repository at this point in the history
This changes the example used to demonstrate injecting a list value into
an evaluation from using cue-cmd to cue-eval.

This avoids demonstrating the technique via a command which has racy,
unstable output, which would either need to be explained beforehand
(despite the technique being unrelated to the race), or glossed over
without comment. Changing the demonstration doesn't require either of
these two suboptimal solutions to be used.

Preview-Path: /docs/howto/inject-value-into-evaluation-using-tag-attribute/
Signed-off-by: Jonathan Matthews <github@hello.jonathanmatthews.com>
Change-Id: Idcd5bb0c56567682b8b6a98486fd26e36ebee3eb
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cuelang.org/+/1198564
Reviewed-by: Paul Jolly <paul@myitcv.io>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
jpluscplusm committed Jul 29, 2024
1 parent abe2d44 commit fccf480
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ into an evaluation that might cause the output to vary. As described in the
[`cue help injection`]({{< relref "/docs/reference/command/cue-help-injection" >}})
reference, CUE allows information to be introduced, or *injected*, by using tag
attributes and tag variables. This guide demonstrates tag attributes.
<!-- TODO(jcm): include a cue-cmd in the set of commands demonstrated on this page -->

## Injecting a value

Expand Down Expand Up @@ -86,33 +87,27 @@ A: conflicting values false and true:
## Injecting lists of values

{{{with code "en" "list"}}}
exec cue cmd -t a='["foo", "bar"]' printList
exec cue eval -t a='["foo", "bar"]'
cmp stdout out
-- list_tool.cue --
-- list.cue --
package example

import (
"encoding/json"
"tool/cli"
)
import "encoding/json"

A: string @tag(a)
asString: string @tag(a)

// Using a disjunction with the empty list ensures
// that an evaluation can succeed even if no value
// is provided at the command line.
aList: *json.Unmarshal(A) | []

command: printList: {
for i, v in aList {
"print-\(v)": cli.Print & {
text: "List element #\(i) is \(v)"
}
}
}
// is provided for the tag value.
asList: *json.Unmarshal(asString) | []

A: [
for e in asList {"\(e)-with-suffix"},
]
-- out --
List element #1 is bar
List element #0 is foo
asString: "[\"foo\", \"bar\"]"
asList: ["foo", "bar"]
A: ["foo-with-suffix", "bar-with-suffix"]
{{{end}}}

## Related content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package site
string: "Fgu2zlyFFwS+sCrmSmuXtkB/LTOchaH5Flpu23s69O4="
number: "s7puBEoBCz+uoEjPDSEBbA8w9cDKX/Gg5qj56WfnrMI="
boolean: "ZgAOoyHTyyOcziY5fKuVgVvgdo5zwMPDzxq50USYyHg="
list: "l8Ox6QCk/ZxNZdRzPcKU1sE1aapkl8gxyMOT6nTJlMc="
list: "/bCSo6uYYB/vkwWsfvYemt8w3OHQBed5Y4IPO/lafCE="
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
package site

content: docs: howto: "inject-value-into-evaluation-using-tag-attribute": {
page: {
comparators: [{
kind: "unstableLineOrderComparator"
commandPrefix: "cue cmd"
}]
}
}
content: docs: howto: "inject-value-into-evaluation-using-tag-attribute": page: _
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ into an evaluation that might cause the output to vary. As described in the
[`cue help injection`]({{< relref "/docs/reference/command/cue-help-injection" >}})
reference, CUE allows information to be introduced, or *injected*, by using tag
attributes and tag variables. This guide demonstrates tag attributes.
<!-- TODO(jcm): include a cue-cmd in the set of commands demonstrated on this page -->

## Injecting a value

Expand Down Expand Up @@ -89,33 +90,27 @@ A: conflicting values false and true:
## Injecting lists of values

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

import (
"encoding/json"
"tool/cli"
)
import "encoding/json"

A: string @tag(a)
asString: string @tag(a)

// Using a disjunction with the empty list ensures
// that an evaluation can succeed even if no value
// is provided at the command line.
aList: *json.Unmarshal(A) | []

command: printList: {
for i, v in aList {
"print-\(v)": cli.Print & {
text: "List element #\(i) is \(v)"
}
}
}
// is provided for the tag value.
asList: *json.Unmarshal(asString) | []

A: [
for e in asList {"\(e)-with-suffix"},
]
{{< /code-tab >}}
{{< code-tab name="TERMINAL" language="" area="top-right" type="terminal" codetocopy="Y3VlIGNtZCAtdCBhPSdbImZvbyIsICJiYXIiXScgcHJpbnRMaXN0" >}}
$ cue cmd -t a='["foo", "bar"]' printList
List element #1 is bar
List element #0 is foo
{{< code-tab name="TERMINAL" language="" area="top-right" type="terminal" codetocopy="Y3VlIGV2YWwgLXQgYT0nWyJmb28iLCAiYmFyIl0n" >}}
$ cue eval -t a='["foo", "bar"]'
asString: "[\"foo\", \"bar\"]"
asList: ["foo", "bar"]
A: ["foo-with-suffix", "bar-with-suffix"]
{{< /code-tab >}}
{{< /code-tabs >}}

Expand Down

0 comments on commit fccf480

Please sign in to comment.