-
Notifications
You must be signed in to change notification settings - Fork 362
feat: add custom key for cel expression support #961
Conversation
TzlilSwimmer123
commented
Jul 18, 2023
•
edited
Loading
edited

return ctx.Error(CustomKeyValidationErrorKeyPath, err.Error()) | ||
} | ||
// wrap dataValue (the resource that should be validated) inside a struct with parent object key | ||
dataValueObjectWrapper := make(map[string]interface{}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
naming maybe resourceWithParentKey
?
} | ||
|
||
func getCELEnvInputs(dataValue map[string]interface{}) ([]cel.EnvOption, error) { | ||
var inputs map[string]any |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var inputs map[string]interface{}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that it suppose to be any because it can be any type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any
is an alias for interface{}
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to know:)
@@ -0,0 +1,140 @@ | |||
// This file defines a custom key to implement the logic for rego rule: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// This file defines a custom key to implement the logic for rego rule: | |
// This file defines a custom key to implement the logic for a CEL rule: |
if customKeyCELRule, ok := m[CELDefinitionCustomKey]; ok { | ||
customKeyCELRuleObj, validObject := customKeyCELRule.([]interface{}) | ||
if !validObject { | ||
return nil, fmt.Errorf("CELDefinition must be an object") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return nil, fmt.Errorf("CELDefinition must be an object") | |
return nil, fmt.Errorf("CELDefinition must be an array") |
return ctx.Error(CustomKeyValidationErrorKeyPath, "cel expression needs to return a boolean") | ||
} | ||
if !celReturnValue { | ||
return ctx.Error(CustomKeyValidationErrorKeyPath, "cel expression failure message: %s", celExpression.Message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"cel expression failure message:"
does @adifayer want this prefix for every CEL failure? how do users experience it?
maybe include a screenshot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will show it to Adi
var CELDefinition CELDefinition | ||
for _, CELExpressionFromSchema := range CELDefinitionSchema { | ||
var CELExpression CELExpression | ||
b, err := json.Marshal(CELExpressionFromSchema) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why Marshal
and then Unmarshal
? if you just need a type conversion then use Go's type conversion. Am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok let's see, what is your suggestion?
func getCELEnvInputs(dataValue map[string]interface{}) ([]cel.EnvOption, error) { | ||
var inputs map[string]any | ||
dataValueBytes, _ := json.Marshal(dataValue) | ||
if err := yaml.Unmarshal(dataValueBytes, &inputs); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's see if I understand this properly.
- CEL expects the input to be a yaml
- you get it as a Go object
- therefore, you need to convert the object to a yaml
is that correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your comment made me understand it was redundant! thanks