| 1 | // Copyright 2018 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 interpreterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/" |
| 19 | |
| 20 | "github.com/google/cel-go/common/functionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/" |
| 21 | ) |
| 22 | |
| 23 | // Dispatcher resolves function calls to their appropriate overload. |
| 24 | type Dispatcherscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Dispatcher# interface { |
| 25 | // Add one or more overloads, returning an error if any Overload has the same Overload#Name. |
| 26 | Addscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Dispatcher#Add.(overloads ...*functionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/.Overloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/Overload#) error |
| 27 | |
| 28 | // FindOverload returns an Overload definition matching the provided name. |
| 29 | FindOverloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Dispatcher#FindOverload.(overload string) (*functionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/.Overloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/Overload#, bool) |
| 30 | |
| 31 | // OverloadIds returns the set of all overload identifiers configured for dispatch. |
| 32 | OverloadIdsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Dispatcher#OverloadIds.() []string |
| 33 | } |
| 34 | |
| 35 | // NewDispatcher returns an empty Dispatcher instance. |
| 36 | func NewDispatcherscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/NewDispatcher().() Dispatcherscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Dispatcher# { |
| 37 | return &defaultDispatcherscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#{ |
| 38 | overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#overloads.: make(map[string]*functionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/.Overloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/Overload#)} |
| 39 | } |
| 40 | |
| 41 | // ExtendDispatcher returns a Dispatcher which inherits the overloads of its parent, and |
| 42 | // provides an isolation layer between built-ins and extension functions which is useful |
| 43 | // for forward compatibility. |
| 44 | func ExtendDispatcherscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/ExtendDispatcher().(parent Dispatcherscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Dispatcher#) Dispatcherscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Dispatcher# { |
| 45 | return &defaultDispatcherscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#{ |
| 46 | parentscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#parent.: parent, |
| 47 | overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#overloads.: make(map[string]*functionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/.Overloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/Overload#)} |
| 48 | } |
| 49 | |
| 50 | // overloadMap helper type for indexing overloads by function name. |
| 51 | type overloadMapscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/overloadMap# map[string]*functionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/.Overloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/Overload# |
| 52 | |
| 53 | // defaultDispatcher struct which contains an overload map. |
| 54 | type defaultDispatcherscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher# struct { |
| 55 | parentscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#parent. Dispatcherscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Dispatcher# |
| 56 | overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#overloads. overloadMapscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/overloadMap# |
| 57 | } |
| 58 | |
| 59 | // Add implements the Dispatcher.Add interface method. |
| 60 | func (d *defaultDispatcherscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#) Addscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#Add().(overloads ...*functionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/.Overloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/Overload#) error { |
| 61 | for _, o := range overloads { |
| 62 | // add the overload unless an overload of the same name has already been provided. |
| 63 | if _, found := d.overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#overloads.[o.Operatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/Overload#Operator.]; found { |
| 64 | return fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Errorf().("overload already exists '%s'", o.Operatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/Overload#Operator.) |
| 65 | } |
| 66 | // index the overload by function name. |
| 67 | d.overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#overloads.[o.Operatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/Overload#Operator.] = o |
| 68 | } |
| 69 | return nil |
| 70 | } |
| 71 | |
| 72 | // FindOverload implements the Dispatcher.FindOverload interface method. |
| 73 | func (d *defaultDispatcherscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#) FindOverloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#FindOverload().(overload string) (*functionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/.Overloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/functions`/Overload#, bool) { |
| 74 | o, found := d.overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#overloads.[overload] |
| 75 | // Attempt to dispatch to an overload defined in the parent. |
| 76 | if !found && d.parentscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#parent. != nil { |
| 77 | return d.parentscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#parent..FindOverloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Dispatcher#FindOverload.(overload) |
| 78 | } |
| 79 | return o, found |
| 80 | } |
| 81 | |
| 82 | // OverloadIds implements the Dispatcher interface method. |
| 83 | func (d *defaultDispatcherscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#) OverloadIdsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#OverloadIds().() []string { |
| 84 | i := 0 |
| 85 | overloads := make([]string, len(d.overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#overloads.)) |
| 86 | for name := range d.overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#overloads. { |
| 87 | overloads[i] = name |
| 88 | i++ |
| 89 | } |
| 90 | if d.parentscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#parent. == nil { |
| 91 | return overloads |
| 92 | } |
| 93 | parentOverloads := d.parentscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#parent..OverloadIdsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Dispatcher#OverloadIds.() |
| 94 | for _, pName := range parentOverloads { |
| 95 | if _, found := d.overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/defaultDispatcher#overloads.[pName]; !found { |
| 96 | overloads = append(overloads, pName) |
| 97 | } |
| 98 | } |
| 99 | return overloads |
| 100 | } |