MCPcopy Create free account
hub / github.com/cel-expr/cel-go / exercise1

Function exercise1

codelab/solution/codelab.go:56–89  ·  view source on GitHub ↗

exercise1 evaluates a simple literal expression: "Hello, World!" Compile, eval, profit!

()

Source from the content-addressed store, hash-verified

54//
55// Compile, eval, profit!
56func exercise1() {
57 fmt.Println("=== Exercise 1: Hello World ===\n")
58 // Create the standard environment.
59 env, err := cel.NewEnv()
60 if err != nil {
61 glog.Exitf("env error: %v", err)
62 }
63 // Check that the expression compiles and returns a String.
64 ast, iss := env.Parse(`"Hello, World!"`)
65 // Report syntactic errors, if present.
66 if iss.Err() != nil {
67 glog.Exit(iss.Err())
68 }
69 // Type-check the expression for correctness.
70 checked, iss := env.Check(ast)
71 // Report semantic errors, if present.
72 if iss.Err() != nil {
73 glog.Exit(iss.Err())
74 }
75 // Check the output type is a string.
76 if checked.OutputType() != cel.StringType {
77 glog.Exitf(
78 "Got %v, wanted %v result type",
79 checked.OutputType(), cel.StringType)
80 }
81 // Plan the program.
82 program, err := env.Program(checked)
83 if err != nil {
84 glog.Exitf("program error: %v", err)
85 }
86 // Evaluate the program without any additional arguments.
87 eval(program, cel.NoVars())
88 fmt.Println()
89}
90
91// exercise2 shows how to declare and use variables in expressions.
92//

Callers 1

mainFunction · 0.70

Calls 8

ParseMethod · 0.95
CheckMethod · 0.95
ProgramMethod · 0.95
NewEnvFunction · 0.92
NoVarsFunction · 0.92
ErrMethod · 0.80
evalFunction · 0.70
OutputTypeMethod · 0.45

Tested by

no test coverage detected