Skip to content

Commit d208ff1

Browse files
committed
cue: add another example
Change-Id: I5a3fb5ab471cd0ada6382e63085865bea9145be3 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2877 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 29a11be commit d208ff1

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

cue/examples_test.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,44 @@ import (
2323
func ExampleRuntime_Parse() {
2424
const config = `
2525
msg: "Hello \(place)!"
26-
place: "world"
26+
place: string | *"world"
2727
`
2828

2929
var r cue.Runtime
3030

31-
instance, err := r.Parse("test", config)
31+
instance, err := r.Compile("test", config)
3232
if err != nil {
3333
// handle error
3434
}
3535

36-
str, err := instance.Lookup("msg").String()
37-
if err != nil {
38-
// handle error
39-
}
36+
str, _ := instance.Lookup("msg").String()
37+
fmt.Println(str)
4038

39+
instance, _ = instance.Fill("you", "place")
40+
str, _ = instance.Lookup("msg").String()
4141
fmt.Println(str)
4242

4343
// Output:
4444
// Hello world!
45+
// Hello you!
46+
}
47+
48+
func ExampleValue_Decode() {
49+
type ab struct{ A, B int }
50+
51+
var x ab
52+
53+
var r cue.Runtime
54+
55+
i, _ := r.Compile("test", `{A: 2, B: 4}`)
56+
_ = i.Value().Decode(&x)
57+
fmt.Println(x)
58+
59+
i, _ = r.Compile("test", `{B: "foo"}`)
60+
err := i.Value().Decode(&x)
61+
fmt.Println(err)
62+
63+
// Output:
64+
// {2 4}
65+
// json: cannot unmarshal string into Go struct field ab.B of type int
4566
}

0 commit comments

Comments
 (0)