Debian Codegraph @

debian/missing-sources/github.com/google/cel-go/interpreter/dispatcher.go

raw symbols SCIP
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 interpreter
16
17 import (
18 "fmt"
19
20 "github.com/google/cel-go/common/functions"
21 )
22
23 // Dispatcher resolves function calls to their appropriate overload.
24 type Dispatcher interface {
25 // Add one or more overloads, returning an error if any Overload has the same Overload#Name.
26 Add(overloads ...*functions.Overload) error
27
28 // FindOverload returns an Overload definition matching the provided name.
29 FindOverload(overload string) (*functions.Overload, bool)
30
31 // OverloadIds returns the set of all overload identifiers configured for dispatch.
32 OverloadIds() []string
33 }
34
35 // NewDispatcher returns an empty Dispatcher instance.
36 func NewDispatcher() Dispatcher {
37 return &defaultDispatcher{
38 overloads: make(map[string]*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 ExtendDispatcher(parent Dispatcher) Dispatcher {
45 return &defaultDispatcher{
46 parent: parent,
47 overloads: make(map[string]*functions.Overload)}
48 }
49
50 // overloadMap helper type for indexing overloads by function name.
51 type overloadMap map[string]*functions.Overload
52
53 // defaultDispatcher struct which contains an overload map.
54 type defaultDispatcher struct {
55 parent Dispatcher
56 overloads overloadMap
57 }
58
59 // Add implements the Dispatcher.Add interface method.
60 func (d *defaultDispatcher) Add(overloads ...*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.overloads[o.Operator]; found {
64 return fmt.Errorf("overload already exists '%s'", o.Operator)
65 }
66 // index the overload by function name.
67 d.overloads[o.Operator] = o
68 }
69 return nil
70 }
71
72 // FindOverload implements the Dispatcher.FindOverload interface method.
73 func (d *defaultDispatcher) FindOverload(overload string) (*functions.Overload, bool) {
74 o, found := d.overloads[overload]
75 // Attempt to dispatch to an overload defined in the parent.
76 if !found && d.parent != nil {
77 return d.parent.FindOverload(overload)
78 }
79 return o, found
80 }
81
82 // OverloadIds implements the Dispatcher interface method.
83 func (d *defaultDispatcher) OverloadIds() []string {
84 i := 0
85 overloads := make([]string, len(d.overloads))
86 for name := range d.overloads {
87 overloads[i] = name
88 i++
89 }
90 if d.parent == nil {
91 return overloads
92 }
93 parentOverloads := d.parent.OverloadIds()
94 for _, pName := range parentOverloads {
95 if _, found := d.overloads[pName]; !found {
96 overloads = append(overloads, pName)
97 }
98 }
99 return overloads
100 }