...

Source file src/cuelang.org/go/cue/examples_test.go

Documentation: cuelang.org/go/cue

     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  	"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  	// Output:
    65  	// 1 <nil>
    66  	// 2 <nil>
    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  	// Output:
   119  	// a allows:
   120  	//   index 4:        true
   121  	//   any index:      true
   122  	//   any string:     false
   123  	// b allows:
   124  	//   field x:        true
   125  	//   field z:        true
   126  	//   field foo:      false
   127  	//   index 4:        false
   128  	//   any string:     false
   129  	// c allows:
   130  	//   field z:        true
   131  	//   field foo:      true
   132  	//   index 4:        false
   133  	//   any string:     true
   134  	// d allows:
   135  	//   field z:        true
   136  	//   field foo:      false
   137  	//   index 4:        false
   138  	//   any string:     false
   139  }
   140  

View as plain text