@@ -11,11 +11,17 @@ import (
11
11
"github.com/google/cel-go/common/types/ref"
12
12
)
13
13
14
+ func hasRelationToType (ctx context.Context , configID , relatedConfigType , direction string ) bool {
15
+ ids := []string {configID }
16
+ output := getRelatedTypeConfigID (ctx , ids , relatedConfigType , direction , true )
17
+ return len (output ) > 0
18
+ }
19
+
14
20
func TraverseConfig (ctx context.Context , configID , relationType , direction string ) []models.ConfigItem {
15
21
var configItems []models.ConfigItem
16
22
17
23
ids := []string {configID }
18
- for _ , typ := range strings .Split (relationType , "/" ) {
24
+ for typ := range strings .SplitSeq (relationType , "/" ) {
19
25
ids = getRelatedTypeConfigID (ctx , ids , typ , direction , true )
20
26
}
21
27
@@ -54,6 +60,28 @@ func getRelatedTypeConfigID(ctx context.Context, ids []string, relatedType, dire
54
60
return allIDs
55
61
}
56
62
63
+ func hasRelationToTypeCELFunction () func (ctx context.Context ) cel.EnvOption {
64
+ return func (ctx context.Context ) cel.EnvOption {
65
+ return cel .Function ("catalog.hasRelationToType" ,
66
+ cel .Overload ("catalog.hasRelationToType_string_string" ,
67
+ []* cel.Type {cel .StringType , cel .StringType },
68
+ cel .BoolType ,
69
+ cel .FunctionBinding (func (args ... ref.Val ) ref.Val {
70
+ configID := conv .ToString (args [0 ])
71
+ typ := conv .ToString (args [1 ])
72
+ direction := "incoming"
73
+ if len (args ) == 3 {
74
+ direction = conv .ToString (args [2 ])
75
+ }
76
+
77
+ result := hasRelationToType (ctx , configID , typ , direction )
78
+ return types .Bool (result )
79
+ }),
80
+ ),
81
+ )
82
+ }
83
+ }
84
+
57
85
func traverseConfigCELFunction () func (ctx context.Context ) cel.EnvOption {
58
86
return func (ctx context.Context ) cel.EnvOption {
59
87
return cel .Function ("catalog.traverse" ,
@@ -101,5 +129,6 @@ func traverseConfigTemplateFunction() func(ctx context.Context) any {
101
129
102
130
func init () {
103
131
context .CelEnvFuncs ["catalog.traverse" ] = traverseConfigCELFunction ()
132
+ context .CelEnvFuncs ["catalog.hasRelationToType" ] = hasRelationToTypeCELFunction ()
104
133
context .TemplateFuncs ["catalog_traverse" ] = traverseConfigTemplateFunction ()
105
134
}
0 commit comments