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

Function mapContainsKeyValue

codelab/codelab.go:225–247  ·  view source on GitHub ↗

mapContainsKeyValue implements the custom function: `map.contains(key, value) bool`.

(args ...ref.Val)

Source from the content-addressed store, hash-verified

223//
224// `map.contains(key, value) bool`.
225func mapContainsKeyValue(args ...ref.Val) ref.Val {
226 // The declaration of the function ensures that only arguments which match
227 // the mapContainsKey signature will be provided to the function.
228 m := args[0].(traits.Mapper)
229
230 // CEL has many interfaces for dealing with different type abstractions.
231 // The traits.Mapper interface unifies field presence testing on proto
232 // messages and maps.
233 key := args[1]
234 v, found := m.Find(key)
235
236 // If not found and the value was non-nil, the value is an error per the
237 // `Find` contract. Propagate it accordingly.
238 if !found {
239 if v != nil {
240 return types.ValOrErr(v, "unsupported key type")
241 }
242 // Return CEL False if the key was not found.
243 return types.False
244 }
245 // Otherwise whether the value at the key equals the value provided.
246 return v.Equal(args[2])
247}
248
249// Functions for constructing CEL inputs.
250

Callers

nothing calls this directly

Calls 3

ValOrErrFunction · 0.92
FindMethod · 0.65
EqualMethod · 0.65

Tested by

no test coverage detected