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

Method Merge

common/decls/decls.go:143–191  ·  view source on GitHub ↗

Merge combines an existing function declaration with another. If a function is extended, by say adding new overloads to an existing function, then it is merged with the prior definition of the function at which point its overloads must not collide with pre-existing overloads and its bindings (singl

(other *FunctionDecl)

Source from the content-addressed store, hash-verified

141// prior definition of the function at which point its overloads must not collide with pre-existing overloads
142// and its bindings (singleton, or per-overload) must not conflict with previous definitions either.
143func (f *FunctionDecl) Merge(other *FunctionDecl) (*FunctionDecl, error) {
144 if f == other {
145 return f, nil
146 }
147 if f == nil || other == nil || f.Name() != other.Name() {
148 return nil, fmt.Errorf("cannot merge unrelated functions. %q and %q", f.Name(), other.Name())
149 }
150 merged := &FunctionDecl{
151 name: f.Name(),
152 overloads: make(map[string]*OverloadDecl, len(f.overloads)),
153 singleton: f.singleton,
154 overloadOrdinals: make([]string, len(f.overloads)),
155 // if one function is expecting type-guards and the other is not, then they
156 // must not be disabled.
157 disableTypeGuards: f.disableTypeGuards && other.disableTypeGuards,
158 // default to the current functions declaration state.
159 state: f.state,
160 doc: f.doc,
161 }
162 // If the other state indicates that the declaration should be explicitly enabled or
163 // disabled, then update the merged state with the most recent value.
164 if other.state != declarationStateUnset {
165 merged.state = other.state
166 }
167 // Allow for non-empty overrides of documentation
168 if len(other.doc) != 0 && f.doc != other.doc {
169 merged.doc = other.doc
170 }
171 // baseline copy of the overloads and their ordinals
172 copy(merged.overloadOrdinals, f.overloadOrdinals)
173 for oID, o := range f.overloads {
174 merged.overloads[oID] = o
175 }
176 // overloads and their ordinals are added from the left
177 for _, oID := range other.overloadOrdinals {
178 o := other.overloads[oID]
179 err := merged.AddOverload(o)
180 if err != nil {
181 return nil, fmt.Errorf("function declaration merge failed: %v", err)
182 }
183 }
184 if other.singleton != nil {
185 if merged.singleton != nil && merged.singleton != other.singleton {
186 return nil, fmt.Errorf("function already has a singleton binding: %s", f.Name())
187 }
188 merged.singleton = other.singleton
189 }
190 return merged, nil
191}
192
193// FunctionSubsetter subsets a function declaration or returns nil and false if the function
194// subset was empty.

Callers 15

TestNilFunctionFunction · 0.95
setFunctionMethod · 0.80
CompileOptionsMethod · 0.80
FunctionDeclsFunction · 0.80
MergeFunction · 0.80
dynMsgFunction · 0.80
TestFunctionBindingsFunction · 0.80
TestFunctionMergeFunction · 0.80

Calls 3

NameMethod · 0.95
AddOverloadMethod · 0.95
NameMethod · 0.65