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

Method CompileOptions

cel/library.go:408–588  ·  view source on GitHub ↗

CompileOptions implements the Library interface method.

()

Source from the content-addressed store, hash-verified

406
407// CompileOptions implements the Library interface method.
408func (lib *optionalLib) CompileOptions() []EnvOption {
409 paramTypeK := TypeParamType("K")
410 paramTypeV := TypeParamType("V")
411 optionalTypeV := OptionalType(paramTypeV)
412 listTypeV := ListType(paramTypeV)
413 mapTypeKV := MapType(paramTypeK, paramTypeV)
414 listOptionalTypeV := ListType(optionalTypeV)
415
416 opts := []EnvOption{
417 // Enable the optional syntax in the parser.
418 enableOptionalSyntax(),
419
420 // Introduce the optional type.
421 Types(types.OptionalType),
422
423 // Configure the optMap and optFlatMap macros.
424 Macros(ReceiverMacro(optMapMacro, 2, optMap,
425 MacroDocs(`perform computation on the value if present and return the result as an optional`),
426 MacroExamples(
427 common.MultilineDescription(
428 `// sub with the prefix 'dev.cel' or optional.none()`,
429 `request.auth.tokens.?sub.optMap(id, 'dev.cel.' + id)`),
430 `optional.none().optMap(i, i * 2) // optional.none()`))),
431
432 // Global and member functions for working with optional values.
433 Function(optionalOfFunc,
434 FunctionDocs(`create a new optional_type(T) with a value where any value is considered valid`),
435 Overload("optional_of", []*Type{paramTypeV}, optionalTypeV,
436 OverloadExamples(`optional.of(1) // optional(1)`),
437 UnaryBinding(func(value ref.Val) ref.Val {
438 return types.OptionalOf(value)
439 }))),
440 Function(optionalOfNonZeroValueFunc,
441 FunctionDocs(`create a new optional_type(T) with a value, if the value is not a zero or empty value`),
442 Overload("optional_ofNonZeroValue", []*Type{paramTypeV}, optionalTypeV,
443 OverloadExamples(
444 `optional.ofNonZeroValue(null) // optional.none()`,
445 `optional.ofNonZeroValue("") // optional.none()`,
446 `optional.ofNonZeroValue("hello") // optional.of('hello')`),
447 UnaryBinding(func(value ref.Val) ref.Val {
448 v, isZeroer := value.(traits.Zeroer)
449 if !isZeroer || !v.IsZeroValue() {
450 return types.OptionalOf(value)
451 }
452 return types.OptionalNone
453 }))),
454 Function(optionalNoneFunc,
455 FunctionDocs(`singleton value representing an optional without a value`),
456 Overload("optional_none", []*Type{}, optionalTypeV,
457 OverloadExamples(`optional.none()`),
458 FunctionBinding(func(values ...ref.Val) ref.Val {
459 return types.OptionalNone
460 }))),
461 Function(valueFunc,
462 FunctionDocs(`obtain the value contained by the optional, error if optional.none()`),
463 MemberOverload("optional_value", []*Type{optionalTypeV}, paramTypeV,
464 OverloadExamples(
465 `optional.of(1).value() // 1`,

Callers

nothing calls this directly

Calls 15

MultilineDescriptionFunction · 0.92
OptionalOfFunction · 0.92
BoolTypeAlias · 0.92
IntTypeAlias · 0.92
enableOptionalSyntaxFunction · 0.85
ReceiverMacroFunction · 0.85
GetValueMethod · 0.80
HasValueMethod · 0.80
TypesFunction · 0.70
MacrosFunction · 0.70
MacroDocsFunction · 0.70
MacroExamplesFunction · 0.70

Tested by

no test coverage detected