Skip to content

Commit d93104e

Browse files
committed
feat: new cel function to check existence of a relationship to a type
1 parent 24fbde7 commit d93104e

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

query/config_traversal.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ import (
1111
"github.com/google/cel-go/common/types/ref"
1212
)
1313

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+
1420
func TraverseConfig(ctx context.Context, configID, relationType, direction string) []models.ConfigItem {
1521
var configItems []models.ConfigItem
1622

1723
ids := []string{configID}
18-
for _, typ := range strings.Split(relationType, "/") {
24+
for typ := range strings.SplitSeq(relationType, "/") {
1925
ids = getRelatedTypeConfigID(ctx, ids, typ, direction, true)
2026
}
2127

@@ -54,6 +60,28 @@ func getRelatedTypeConfigID(ctx context.Context, ids []string, relatedType, dire
5460
return allIDs
5561
}
5662

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+
5785
func traverseConfigCELFunction() func(ctx context.Context) cel.EnvOption {
5886
return func(ctx context.Context) cel.EnvOption {
5987
return cel.Function("catalog.traverse",
@@ -101,5 +129,6 @@ func traverseConfigTemplateFunction() func(ctx context.Context) any {
101129

102130
func init() {
103131
context.CelEnvFuncs["catalog.traverse"] = traverseConfigCELFunction()
132+
context.CelEnvFuncs["catalog.hasRelationToType"] = hasRelationToTypeCELFunction()
104133
context.TemplateFuncs["catalog_traverse"] = traverseConfigTemplateFunction()
105134
}

0 commit comments

Comments
 (0)