...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package cue_test
16
17 import (
18 "fmt"
19 "os"
20
21 "cuelang.org/go/cue"
22 "cuelang.org/go/cue/cuecontext"
23 "cuelang.org/go/internal/cuetxtar"
24 "golang.org/x/tools/txtar"
25 )
26
27 func load(file string) *cue.Instance {
28 dir, _ := os.MkdirTemp("", "*")
29 defer os.RemoveAll(dir)
30
31 inst := cue.Build(cuetxtar.Load(txtar.Parse([]byte(file)), dir))[0]
32 if err := inst.Err; err != nil {
33 panic(err)
34 }
35 return inst
36 }
37
38 func ExampleHid() {
39 const file = `
40 -- cue.mod/module.cue --
41 module: "mod.test"
42
43 -- main.cue --
44 import "mod.test/foo:bar"
45
46 bar
47 _foo: int // scoped in main (anonymous) package
48 baz: _foo
49
50 -- foo/bar.cue --
51 package bar
52
53 _foo: int // scoped within imported package
54 bar: _foo
55 `
56
57 v := load(file).Value()
58
59 v = v.FillPath(cue.MakePath(cue.Hid("_foo", "mod.test/foo:bar")), 1)
60 v = v.FillPath(cue.MakePath(cue.Hid("_foo", "_")), 2)
61 fmt.Println(v.LookupPath(cue.ParsePath("bar")).Int64())
62 fmt.Println(v.LookupPath(cue.ParsePath("baz")).Int64())
63
64
65
66
67 }
68
69 func ExampleValue_Allows() {
70 ctx := cuecontext.New()
71
72 const file = `
73 a: [1, 2, ...int]
74
75 b: #Point
76 #Point: {
77 x: int
78 y: int
79 z?: int
80 }
81
82 c: [string]: int
83
84 d: #C
85 #C: [>"m"]: int
86 `
87
88 v := ctx.CompileString(file)
89
90 a := v.LookupPath(cue.ParsePath("a"))
91 fmt.Println("a allows:")
92 fmt.Println(" index 4: ", a.Allows(cue.Index(4)))
93 fmt.Println(" any index: ", a.Allows(cue.AnyIndex))
94 fmt.Println(" any string: ", a.Allows(cue.AnyString))
95
96 b := v.LookupPath(cue.ParsePath("b"))
97 fmt.Println("b allows:")
98 fmt.Println(" field x: ", b.Allows(cue.Str("x")))
99 fmt.Println(" field z: ", b.Allows(cue.Str("z")))
100 fmt.Println(" field foo: ", b.Allows(cue.Str("foo")))
101 fmt.Println(" index 4: ", b.Allows(cue.Index(4)))
102 fmt.Println(" any string: ", b.Allows(cue.AnyString))
103
104 c := v.LookupPath(cue.ParsePath("c"))
105 fmt.Println("c allows:")
106 fmt.Println(" field z: ", c.Allows(cue.Str("z")))
107 fmt.Println(" field foo: ", c.Allows(cue.Str("foo")))
108 fmt.Println(" index 4: ", c.Allows(cue.Index(4)))
109 fmt.Println(" any string: ", c.Allows(cue.AnyString))
110
111 d := v.LookupPath(cue.ParsePath("d"))
112 fmt.Println("d allows:")
113 fmt.Println(" field z: ", d.Allows(cue.Str("z")))
114 fmt.Println(" field foo: ", d.Allows(cue.Str("foo")))
115 fmt.Println(" index 4: ", d.Allows(cue.Index(4)))
116 fmt.Println(" any string: ", d.Allows(cue.AnyString))
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 }
140
View as plain text