MCPcopy Create free account
hub / github.com/Permify/permify / Rule

Function Rule

pkg/schema/builder.go:79–120  ·  view source on GitHub ↗

Rule is a function that generates a rule definition given a name, a map of argument names to attribute types, and an expression string. The expression string is compiled and transformed to a checked expression.

(name string, arguments map[string]base.AttributeType, expression string)

Source from the content-addressed store, hash-verified

77// a map of argument names to attribute types, and an expression string.
78// The expression string is compiled and transformed to a checked expression.
79func Rule(name string, arguments map[string]base.AttributeType, expression string) *base.RuleDefinition {
80 // Initialize an empty slice of environment options.
81 var envOptions []cel.EnvOption
82 envOptions = append(envOptions, cel.Variable("context", cel.DynType))
83
84 // Iterate through each argument.
85 for name, ty := range arguments {
86 // Convert the attribute type to CEL type.
87 cType, err := utils.GetCelType(ty)
88 if err != nil {
89 return nil
90 }
91
92 // Append a new environment option which represents a variable and its type.
93 envOptions = append(envOptions, cel.Variable(name, cType))
94 }
95
96 // Create a new CEL environment with the environment options.
97 env, err := cel.NewEnv(envOptions...)
98 if err != nil {
99 return nil
100 }
101
102 // Compile the given expression string.
103 compiledExp, issues := env.Compile(expression)
104 if issues != nil && issues.Err() != nil {
105 return nil
106 }
107
108 // Convert the compiled expression to a checked expression.
109 expr, err := cel.AstToCheckedExpr(compiledExp)
110 if err != nil {
111 return nil
112 }
113
114 // Return a new rule definition with the given name, arguments, and the checked expression.
115 return &base.RuleDefinition{
116 Name: name,
117 Arguments: arguments,
118 Expression: expr,
119 }
120}
121
122// Rules - Rules builder
123func Rules(defs ...*base.RuleDefinition) []*base.RuleDefinition {

Callers 1

builder_test.goFile · 0.85

Calls 2

GetCelTypeFunction · 0.92
CompileMethod · 0.80

Tested by

no test coverage detected