cuelang.org/go@v0.10.1/cue/examples_test.go (about) 1 // Copyright 2021 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cue_test 16 17 import ( 18 "fmt" 19 "log" 20 "os" 21 22 "cuelang.org/go/cue" 23 "cuelang.org/go/cue/cuecontext" 24 "cuelang.org/go/internal/cuetxtar" 25 "golang.org/x/tools/txtar" 26 ) 27 28 func load(file string) *cue.Instance { 29 dir, _ := os.MkdirTemp("", "*") 30 defer os.RemoveAll(dir) 31 32 inst := cue.Build(cuetxtar.Load(txtar.Parse([]byte(file)), dir))[0] 33 if err := inst.Err; err != nil { 34 panic(err) 35 } 36 return inst 37 } 38 39 func ExampleHid() { 40 const file = ` 41 -- cue.mod/module.cue -- 42 module: "mod.test" 43 language: version: "v0.9.0" 44 45 -- main.cue -- 46 import "mod.test/foo:bar" 47 48 bar 49 _foo: int // scoped in main (anonymous) package 50 baz: _foo 51 52 -- foo/bar.cue -- 53 package bar 54 55 _foo: int // scoped within imported package 56 bar: _foo 57 ` 58 59 v := load(file).Value() 60 61 v = v.FillPath(cue.MakePath(cue.Hid("_foo", "mod.test/foo:bar")), 1) 62 v = v.FillPath(cue.MakePath(cue.Hid("_foo", "_")), 2) 63 fmt.Println(v.LookupPath(cue.ParsePath("bar")).Int64()) 64 fmt.Println(v.LookupPath(cue.ParsePath("baz")).Int64()) 65 66 // Output: 67 // 1 <nil> 68 // 2 <nil> 69 } 70 71 func ExampleValue_Allows() { 72 ctx := cuecontext.New() 73 74 const file = ` 75 a: [1, 2, ...int] 76 77 b: #Point 78 #Point: { 79 x: int 80 y: int 81 z?: int 82 } 83 84 c: [string]: int 85 86 d: #C 87 #C: [>"m"]: int 88 ` 89 90 v := ctx.CompileString(file) 91 if err := v.Err(); err != nil { 92 log.Fatal(err) 93 } 94 95 a := v.LookupPath(cue.ParsePath("a")) 96 fmt.Println("a allows:") 97 fmt.Println(" index 4: ", a.Allows(cue.Index(4))) 98 fmt.Println(" any index: ", a.Allows(cue.AnyIndex)) 99 fmt.Println(" any string: ", a.Allows(cue.AnyString)) 100 101 b := v.LookupPath(cue.ParsePath("b")) 102 fmt.Println("b allows:") 103 fmt.Println(" field x: ", b.Allows(cue.Str("x"))) 104 fmt.Println(" field z: ", b.Allows(cue.Str("z"))) 105 fmt.Println(" field foo: ", b.Allows(cue.Str("foo"))) 106 fmt.Println(" index 4: ", b.Allows(cue.Index(4))) 107 fmt.Println(" any string: ", b.Allows(cue.AnyString)) 108 109 c := v.LookupPath(cue.ParsePath("c")) 110 fmt.Println("c allows:") 111 fmt.Println(" field z: ", c.Allows(cue.Str("z"))) 112 fmt.Println(" field foo: ", c.Allows(cue.Str("foo"))) 113 fmt.Println(" index 4: ", c.Allows(cue.Index(4))) 114 fmt.Println(" any string: ", c.Allows(cue.AnyString)) 115 116 d := v.LookupPath(cue.ParsePath("d")) 117 fmt.Println("d allows:") 118 fmt.Println(" field z: ", d.Allows(cue.Str("z"))) 119 fmt.Println(" field foo: ", d.Allows(cue.Str("foo"))) 120 fmt.Println(" index 4: ", d.Allows(cue.Index(4))) 121 fmt.Println(" any string: ", d.Allows(cue.AnyString)) 122 123 // Output: 124 // a allows: 125 // index 4: true 126 // any index: true 127 // any string: false 128 // b allows: 129 // field x: true 130 // field z: true 131 // field foo: false 132 // index 4: false 133 // any string: false 134 // c allows: 135 // field z: true 136 // field foo: true 137 // index 4: false 138 // any string: true 139 // d allows: 140 // field z: true 141 // field foo: false 142 // index 4: false 143 // any string: false 144 }