-
Notifications
You must be signed in to change notification settings - Fork 306
Add a maps extension with maps.merge #1409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package ext | ||
|
|
||
| import ( | ||
| "github.com/google/cel-go/cel" | ||
| "github.com/google/cel-go/checker" | ||
| "github.com/google/cel-go/common" | ||
| "github.com/google/cel-go/common/types" | ||
| "github.com/google/cel-go/common/types/ref" | ||
| "github.com/google/cel-go/common/types/traits" | ||
| "github.com/google/cel-go/interpreter" | ||
| ) | ||
|
|
||
| // Maps returns a cel.EnvOption to configure namespaced map functions. | ||
| // | ||
| // CEL has no operator for combining two maps: the `+` operator concatenates | ||
| // strings, bytes, and lists, but is not defined for maps. This library provides | ||
| // map combination as a named function. | ||
| // | ||
| // # Merge | ||
| // | ||
| // Returns a new map containing the entries of both maps. When a key is | ||
| // present in both, the value from the argument wins. Neither input is | ||
| // modified. | ||
| // | ||
| // The merge is shallow: a value that is itself a map is replaced rather than | ||
| // merged recursively. | ||
| // | ||
| // <map(K, V)>.merge(<map(K, V)>) -> <map(K, V)> | ||
| // | ||
| // Examples: | ||
| // | ||
| // {}.merge({}) // {} | ||
| // {'a': 1}.merge({'b': 2}) // {'a': 1, 'b': 2} | ||
| // {'a': 1}.merge({'a': 2}) // {'a': 2} | ||
| // {'a': {'x': 1}}.merge({'a': {'y': 2}}) // {'a': {'y': 2}}, values are replaced, not merged | ||
| func Maps(options ...MapsOption) cel.EnvOption { | ||
| l := &mapsLib{} | ||
| for _, o := range options { | ||
| l = o(l) | ||
| } | ||
| return cel.Lib(l) | ||
| } | ||
|
|
||
| // MapsOption declares a functional operator for configuring map extensions. | ||
| type MapsOption func(*mapsLib) *mapsLib | ||
|
|
||
| // MapsVersion sets the library version for map extensions. | ||
| func MapsVersion(version uint32) MapsOption { | ||
| return func(lib *mapsLib) *mapsLib { | ||
| lib.version = version | ||
| return lib | ||
| } | ||
| } | ||
|
|
||
| type mapsLib struct { | ||
| version uint32 | ||
| } | ||
|
|
||
| // LibraryName implements the SingletonLibrary interface method. | ||
| func (mapsLib) LibraryName() string { | ||
| return "cel.lib.ext.maps" | ||
| } | ||
|
|
||
| // CompileOptions implements the Library interface method. | ||
| func (mapsLib) CompileOptions() []cel.EnvOption { | ||
| mapType := cel.MapType(cel.TypeParamType("K"), cel.TypeParamType("V")) | ||
| return []cel.EnvOption{ | ||
| cel.Function("merge", | ||
| cel.MemberOverload("map_merge_map", []*cel.Type{mapType, mapType}, mapType, | ||
| cel.BinaryBinding(mapsMerge))), | ||
| cel.CostEstimatorOptions( | ||
| checker.OverloadCostEstimate("map_merge_map", estimateMapsMergeCost), | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| // ProgramOptions implements the Library interface method. | ||
| func (mapsLib) ProgramOptions() []cel.ProgramOption { | ||
| return []cel.ProgramOption{ | ||
| cel.CostTrackerOptions( | ||
| interpreter.OverloadCostTracker("map_merge_map", trackMapsMergeCost), | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| // estimateMapsMergeCost charges for visiting every entry of both inputs and for | ||
| // allocating the result map. | ||
| func estimateMapsMergeCost(estimator checker.CostEstimator, target *checker.AstNode, args []checker.AstNode) *checker.CallEstimate { | ||
| if target == nil || len(args) != 1 { | ||
| return nil | ||
| } | ||
| lhsSize := estimateSize(estimator, *target) | ||
| rhsSize := estimateSize(estimator, args[0]) | ||
| entries := lhsSize.Add(rhsSize) | ||
| cost := entries.MultiplyByCostFactor(1).Add(mapAllocCost).Add(callCostEstimate) | ||
| // The result holds at least as many entries as the larger input, when every | ||
| // key collides, and at most the sum of both, when none do. | ||
| resultSize := rangedSizeEstimate(max(lhsSize.Min, rhsSize.Min), entries.Max) | ||
| return callEstimate(cost, &resultSize) | ||
| } | ||
|
|
||
| // trackMapsMergeCost mirrors estimateMapsMergeCost against the actual inputs. | ||
| func trackMapsMergeCost(args []ref.Val, _ ref.Val) *uint64 { | ||
| entries := safeAdd(actualSize(args[0]), actualSize(args[1])) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm in the process of overhauling the cost framework, so this is fine for now, but please add a |
||
| cost := safeAdd(callCost, uint64(common.MapCreateBaseCost), entries) | ||
| return &cost | ||
| } | ||
|
|
||
| // mapsMerge returns a new map holding the entries of both inputs, with the | ||
| // values of the second input taking precedence on conflicting keys. | ||
| func mapsMerge(lhs, rhs ref.Val) ref.Val { | ||
| first, ok := lhs.(traits.Mapper) | ||
| if !ok { | ||
| return types.MaybeNoSuchOverloadErr(lhs) | ||
| } | ||
| second, ok := rhs.(traits.Mapper) | ||
| if !ok { | ||
| return types.MaybeNoSuchOverloadErr(rhs) | ||
| } | ||
| merged := make(map[ref.Val]ref.Val, actualSize(first)+actualSize(second)) | ||
| if err := copyEntries(first, merged); err != nil { | ||
| return err | ||
| } | ||
| if err := copyEntries(second, merged); err != nil { | ||
| return err | ||
| } | ||
| return types.NewRefValMap(types.DefaultTypeAdapter, merged) | ||
| } | ||
|
|
||
| // copyEntries writes every entry of m into dst, overwriting entries whose keys | ||
| // are already present. It returns a non-nil ref.Val only when the map yields an | ||
| // error or unknown value. | ||
| func copyEntries(m traits.Mapper, dst map[ref.Val]ref.Val) ref.Val { | ||
| it := m.Iterator() | ||
| for it.HasNext() == types.True { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using the |
||
| key := it.Next() | ||
| if types.IsUnknownOrError(key) { | ||
| return key | ||
| } | ||
| val, _ := m.Find(key) | ||
| if types.IsUnknownOrError(val) { | ||
| return val | ||
| } | ||
| dst[key] = val | ||
| } | ||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's maybe call this something other than
merge.A couple of good examples might be
putAll(Java convention) orextend(Rust) sincemergemight imply semantics like combining the values of a colliding key.I'm inclined toward
putAllsince it's easy to document what it does, and you could have other methods like<map(K,V)>.put(K, V) -> map(K,V)if you need to update a specific key (and you might want to add<map(K,V)>.remove(K) -> map(K,V),<map(K,V)>.removeAll(<list(K)>) -> map(K,V),map(K,V).keys(),map(K,V).values()while you're at it)