-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implements "isSubtreeOf" function for maps
- Loading branch information
1 parent
31dd8d0
commit b5b68d9
Showing
4 changed files
with
110 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package types | ||
|
||
import ( | ||
"github.com/google/cel-go/cel" | ||
"github.com/google/cel-go/common/types" | ||
"github.com/google/cel-go/common/types/ref" | ||
) | ||
|
||
func init() { | ||
subtreeFunction := cel.Function("isSubtreeOf", | ||
cel.MemberOverload("subtree_maps", | ||
[]*cel.Type{cel.MapType(cel.StringType, cel.AnyType), cel.MapType(cel.StringType, cel.AnyType)}, | ||
cel.BoolType, | ||
cel.FunctionBinding(func(arg ...ref.Val) ref.Val { | ||
map0 := arg[0].Value().(map[string]any) | ||
map1 := arg[1].Value().(map[string]any) | ||
return types.Bool(subtree(map0, map1)) | ||
}))) | ||
|
||
CustomTypes["__map_subtree"] = []cel.EnvOption{subtreeFunction} | ||
} | ||
|
||
func subtree(map0 map[string]any, map1 map[string]any) bool { | ||
for k, v := range map0 { | ||
val, ok := map1[k] | ||
if !ok { | ||
return false | ||
} | ||
nestedMap0, ok := v.(map[string]any) | ||
if ok { | ||
nestedMap1, ok := val.(map[string]any) | ||
if !ok { | ||
return false | ||
} | ||
nestedResult := subtree(nestedMap0, nestedMap1) | ||
if !nestedResult { | ||
return false | ||
} | ||
} else { | ||
if v != val { | ||
return false | ||
} | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMapSubtree(t *testing.T) { | ||
tcs := []struct { | ||
map1 map[string]any | ||
map2 map[string]any | ||
subtree bool | ||
}{ | ||
{ | ||
map[string]any{"a": 1}, | ||
map[string]any{"a": 1}, | ||
true, | ||
}, | ||
{ | ||
map[string]any{"a": 1, "b": 2}, | ||
map[string]any{"a": 1}, | ||
false, | ||
}, | ||
{ | ||
map[string]any{"a": 1}, | ||
map[string]any{"a": 1, "b": 1}, | ||
true, | ||
}, | ||
{ | ||
map[string]any{"a": 1, "b": map[string]any{"a": 1}}, | ||
map[string]any{"a": 1}, | ||
false, | ||
}, | ||
{ | ||
map[string]any{"a": 1, "b": map[string]any{"a": 1}}, | ||
map[string]any{"a": 1, "b": map[string]any{"a": 1}}, | ||
true, | ||
}, | ||
{ | ||
map[string]any{"a": 1, "b": map[string]any{"a": 1}}, | ||
map[string]any{"a": 1, "b": map[string]any{"a": 1, "b": 1}}, | ||
true, | ||
}, | ||
{ | ||
map[string]any{"a": 1, "b": map[string]any{"a": 1}}, | ||
map[string]any{"a": 1, "b": map[string]any{"a": "1", "b": 1}}, | ||
false, | ||
}, | ||
{ | ||
map[string]any{"a": 1, "b": map[string]any{"a": 1}}, | ||
map[string]any{"a": 1, "b": map[string]any{"a": 1, "b": map[string]any{}}}, | ||
true, | ||
}, | ||
} | ||
for _, tt := range tcs { | ||
t.Run("", func(t *testing.T) { | ||
require.Equal(t, tt.subtree, subtree(tt.map1, tt.map2)) | ||
}) | ||
} | ||
} |