Skip to content

Commit

Permalink
cue: use Value.Err in examples using Compile APIs
Browse files Browse the repository at this point in the history
The Context.CompileString docs say:

    The returned Value will represent an error, accessible through Err,
    if any error occurred.

Our two examples involving these APIs did not check for an error.
This can be OK if the user is sure an error won't happen,
but in general that's not a safe assumption to make.
Do the safer and recommended error check in the examples.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I1dd758d275f457db9eadc9aa6bf0faf7805fdb2b
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1197696
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Matthew Sackman <matthew@cue.works>
  • Loading branch information
mvdan committed Jul 12, 2024
1 parent 3a379b7 commit 719893f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cue/examplecompile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cue_test

import (
"fmt"
"log"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
Expand All @@ -29,6 +30,9 @@ func ExampleContext() {
b: 3
"a+b": a + b
`)
if err := v.Err(); err != nil {
log.Fatal(err)
}

p("lookups")
p("a: %v", v.LookupPath(cue.ParsePath("a")))
Expand Down
4 changes: 4 additions & 0 deletions cue/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cue_test

import (
"fmt"
"log"
"os"

"cuelang.org/go/cue"
Expand Down Expand Up @@ -87,6 +88,9 @@ d: #C
`

v := ctx.CompileString(file)
if err := v.Err(); err != nil {
log.Fatal(err)
}

a := v.LookupPath(cue.ParsePath("a"))
fmt.Println("a allows:")
Expand Down

0 comments on commit 719893f

Please sign in to comment.