...

Source file src/cuelang.org/go/cue/examplecompile_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  
    20  	"cuelang.org/go/cue"
    21  	"cuelang.org/go/cue/cuecontext"
    22  )
    23  
    24  func ExampleContext() {
    25  	ctx := cuecontext.New()
    26  
    27  	v := ctx.CompileString(`
    28  		a: 2
    29  		b: 3
    30  		"a+b": a + b
    31  	`)
    32  
    33  	p("lookups")
    34  	p("a:     %v", v.LookupPath(cue.ParsePath("a")))
    35  	p("b:     %v", v.LookupPath(cue.ParsePath("b")))
    36  	p(`"a+b": %v`, v.LookupPath(cue.ParsePath(`"a+b"`)))
    37  	p("")
    38  	p("expressions")
    39  	p("a + b: %v", ctx.CompileString("a + b", cue.Scope(v)))
    40  	p("a * b: %v", ctx.CompileString("a * b", cue.Scope(v)))
    41  
    42  	// Output:
    43  	// lookups
    44  	// a:     2
    45  	// b:     3
    46  	// "a+b": 5
    47  	//
    48  	// expressions
    49  	// a + b: 5
    50  	// a * b: 6
    51  }
    52  
    53  func p(format string, args ...interface{}) {
    54  	fmt.Printf(format+"\n", args...)
    55  }
    56  

View as plain text