github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/caveats/types/map.go (about)

     1  package types
     2  
     3  import (
     4  	"github.com/authzed/cel-go/cel"
     5  	"github.com/authzed/cel-go/common/types"
     6  	"github.com/authzed/cel-go/common/types/ref"
     7  )
     8  
     9  func init() {
    10  	registerMethodOnDefinedType(cel.MapType(cel.StringType, cel.DynType),
    11  		"isSubtreeOf",
    12  		[]*cel.Type{cel.MapType(cel.StringType, cel.DynType)},
    13  		cel.BoolType,
    14  		func(arg ...ref.Val) ref.Val {
    15  			map0 := arg[0].Value().(map[string]any)
    16  			map1 := arg[1].Value().(map[string]any)
    17  			return types.Bool(subtree(map0, map1))
    18  		},
    19  	)
    20  }
    21  
    22  func subtree(map0 map[string]any, map1 map[string]any) bool {
    23  	for k, v := range map0 {
    24  		val, ok := map1[k]
    25  		if !ok {
    26  			return false
    27  		}
    28  		nestedMap0, ok := v.(map[string]any)
    29  		if ok {
    30  			nestedMap1, ok := val.(map[string]any)
    31  			if !ok {
    32  				return false
    33  			}
    34  			nestedResult := subtree(nestedMap0, nestedMap1)
    35  			if !nestedResult {
    36  				return false
    37  			}
    38  		} else {
    39  			if v != val {
    40  				return false
    41  			}
    42  		}
    43  	}
    44  	return true
    45  }