CompileOptions implements the Library interface method.
()
| 509 | |
| 510 | // CompileOptions implements the Library interface method. |
| 511 | func (lib *optionalLib) CompileOptions() []EnvOption { |
| 512 | paramTypeK := TypeParamType("K") |
| 513 | paramTypeV := TypeParamType("V") |
| 514 | optionalTypeV := OptionalType(paramTypeV) |
| 515 | listTypeV := ListType(paramTypeV) |
| 516 | mapTypeKV := MapType(paramTypeK, paramTypeV) |
| 517 | listOptionalTypeV := ListType(optionalTypeV) |
| 518 | |
| 519 | optMapMacroExpander := optMap |
| 520 | if lib.version >= 4 { |
| 521 | optMapMacroExpander = optMapV2 |
| 522 | } |
| 523 | |
| 524 | opts := []EnvOption{ |
| 525 | // Enable the optional syntax in the parser. |
| 526 | enableOptionalSyntax(), |
| 527 | |
| 528 | // Introduce the optional type. |
| 529 | Types(types.OptionalType), |
| 530 | |
| 531 | // Configure the optMap and optFlatMap macros. |
| 532 | Macros(ReceiverMacro(optMapMacro, 2, optMapMacroExpander, |
| 533 | MacroDocs(`perform computation on the value if present and return the result as an optional`), |
| 534 | MacroExamples( |
| 535 | common.MultilineDescription( |
| 536 | `// sub with the prefix 'dev.cel' or optional.none()`, |
| 537 | `request.auth.tokens.?sub.optMap(id, 'dev.cel.' + id)`), |
| 538 | `optional.none().optMap(i, i * 2) // optional.none()`))), |
| 539 | |
| 540 | // Global and member functions for working with optional values. |
| 541 | Function(optionalOfFunc, |
| 542 | FunctionDocs(`create a new optional_type(T) with a value where any value is considered valid`), |
| 543 | Overload("optional_of", []*Type{paramTypeV}, optionalTypeV, |
| 544 | OverloadExamples(`optional.of(1) // optional(1)`), |
| 545 | UnaryBinding(func(value ref.Val) ref.Val { |
| 546 | return types.OptionalOf(value) |
| 547 | }))), |
| 548 | Function(optionalOfNonZeroValueFunc, |
| 549 | FunctionDocs(`create a new optional_type(T) with a value, if the value is not a zero or empty value`), |
| 550 | Overload("optional_ofNonZeroValue", []*Type{paramTypeV}, optionalTypeV, |
| 551 | OverloadExamples( |
| 552 | `optional.ofNonZeroValue(null) // optional.none()`, |
| 553 | `optional.ofNonZeroValue("") // optional.none()`, |
| 554 | `optional.ofNonZeroValue("hello") // optional.of('hello')`), |
| 555 | UnaryBinding(func(value ref.Val) ref.Val { |
| 556 | v, isZeroer := value.(traits.Zeroer) |
| 557 | if !isZeroer || !v.IsZeroValue() { |
| 558 | return types.OptionalOf(value) |
| 559 | } |
| 560 | return types.OptionalNone |
| 561 | }))), |
| 562 | Function(optionalNoneFunc, |
| 563 | FunctionDocs(`singleton value representing an optional without a value`), |
| 564 | Overload(overloads.OptionalNone, []*Type{}, optionalTypeV, |
| 565 | OverloadExamples(`optional.none()`), |
| 566 | FunctionBinding(func(values ...ref.Val) ref.Val { |
| 567 | return types.OptionalNone |
| 568 | }))), |
nothing calls this directly
no test coverage detected