Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: add another example
Browse files Browse the repository at this point in the history
Change-Id: I5a3fb5ab471cd0ada6382e63085865bea9145be3
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2877
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Aug 21, 2019
1 parent 29a11be commit d208ff1
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions cue/examples_test.go
Expand Up @@ -23,23 +23,44 @@ import (
func ExampleRuntime_Parse() {
const config = `
msg: "Hello \(place)!"
place: "world"
place: string | *"world"
`

var r cue.Runtime

instance, err := r.Parse("test", config)
instance, err := r.Compile("test", config)
if err != nil {
// handle error
}

str, err := instance.Lookup("msg").String()
if err != nil {
// handle error
}
str, _ := instance.Lookup("msg").String()
fmt.Println(str)

instance, _ = instance.Fill("you", "place")
str, _ = instance.Lookup("msg").String()
fmt.Println(str)

// Output:
// Hello world!
// Hello you!
}

func ExampleValue_Decode() {
type ab struct{ A, B int }

var x ab

var r cue.Runtime

i, _ := r.Compile("test", `{A: 2, B: 4}`)
_ = i.Value().Decode(&x)
fmt.Println(x)

i, _ = r.Compile("test", `{B: "foo"}`)
err := i.Value().Decode(&x)
fmt.Println(err)

// Output:
// {2 4}
// json: cannot unmarshal string into Go struct field ab.B of type int
}

0 comments on commit d208ff1

Please sign in to comment.