| 1 | // Copyright 2023 Google LLC |
| 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 functions defines the standard builtin functions supported by the interpreter |
| 16 | package functions |
| 17 | |
| 18 | import "github.com/google/cel-go/common/types/ref" |
| 19 | |
| 20 | // Overload defines a named overload of a function, indicating an operand trait |
| 21 | // which must be present on the first argument to the overload as well as one |
| 22 | // of either a unary, binary, or function implementation. |
| 23 | // |
| 24 | // The majority of operators within the expression language are unary or binary |
| 25 | // and the specializations simplify the call contract for implementers of |
| 26 | // types with operator overloads. Any added complexity is assumed to be handled |
| 27 | // by the generic FunctionOp. |
| 28 | type Overload struct { |
| 29 | // Operator name as written in an expression or defined within |
| 30 | // operators.go. |
| 31 | Operator string |
| 32 | |
| 33 | // Operand trait used to dispatch the call. The zero-value indicates a |
| 34 | // global function overload or that one of the Unary / Binary / Function |
| 35 | // definitions should be used to execute the call. |
| 36 | OperandTrait int |
| 37 | |
| 38 | // Unary defines the overload with a UnaryOp implementation. May be nil. |
| 39 | Unary UnaryOp |
| 40 | |
| 41 | // Binary defines the overload with a BinaryOp implementation. May be nil. |
| 42 | Binary BinaryOp |
| 43 | |
| 44 | // Function defines the overload with a FunctionOp implementation. May be |
| 45 | // nil. |
| 46 | Function FunctionOp |
| 47 | |
| 48 | // NonStrict specifies whether the Overload will tolerate arguments that |
| 49 | // are types.Err or types.Unknown. |
| 50 | NonStrict bool |
| 51 | } |
| 52 | |
| 53 | // UnaryOp is a function that takes a single value and produces an output. |
| 54 | type UnaryOp func(value ref.Val) ref.Val |
| 55 | |
| 56 | // BinaryOp is a function that takes two values and produces an output. |
| 57 | type BinaryOp func(lhs ref.Val, rhs ref.Val) ref.Val |
| 58 | |
| 59 | // FunctionOp is a function with accepts zero or more arguments and produces |
| 60 | // a value or error as a result. |
| 61 | type FunctionOp func(values ...ref.Val) ref.Val |