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

Function mapContainsKeyValue

codelab/solution/codelab.go:466–488  ·  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

464//
465// `map.contains(key, value) bool`.
466func mapContainsKeyValue(args ...ref.Val) ref.Val {
467 // The declaration of the function ensures that only arguments which match
468 // the mapContainsKey signature will be provided to the function.
469 m := args[0].(traits.Mapper)
470
471 // CEL has many interfaces for dealing with different type abstractions.
472 // The traits.Mapper interface unifies field presence testing on proto
473 // messages and maps.
474 key := args[1]
475 v, found := m.Find(key)
476
477 // If not found and the value was non-nil, the value is an error per the
478 // `Find` contract. Propagate it accordingly.
479 if !found {
480 if v != nil {
481 return types.ValOrErr(v, "unsupported key type")
482 }
483 // Return CEL False if the key was not found.
484 return types.False
485 }
486 // Otherwise whether the value at the key equals the value provided.
487 return v.Equal(args[2])
488}
489
490// Functions for constructing CEL inputs.
491

Callers

nothing calls this directly

Calls 3

ValOrErrFunction · 0.92
FindMethod · 0.65
EqualMethod · 0.65

Tested by

no test coverage detected