feat: complete SOP execution workflow
This commit is contained in:
@@ -11,30 +11,43 @@ func matchCondition(raw json.RawMessage, answers map[string]interface{}) (bool,
|
||||
if len(raw) == 0 || string(raw) == "{}" || string(raw) == "null" {
|
||||
return true, nil
|
||||
}
|
||||
var rule map[string]interface{}
|
||||
var rule interface{}
|
||||
if err := json.Unmarshal(raw, &rule); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if all, ok := rule["all"].([]interface{}); ok {
|
||||
for _, item := range all {
|
||||
object, ok := item.(map[string]interface{})
|
||||
if !ok {
|
||||
return false, fmt.Errorf("invalid all condition")
|
||||
}
|
||||
matched, err := matchRule(object, answers)
|
||||
return matchConditionValue(rule, answers)
|
||||
}
|
||||
|
||||
func matchConditionValue(value interface{}, answers map[string]interface{}) (bool, error) {
|
||||
rule, ok := value.(map[string]interface{})
|
||||
if !ok {
|
||||
return false, fmt.Errorf("invalid condition")
|
||||
}
|
||||
all, hasAll := rule["all"]
|
||||
any, hasAny := rule["any"]
|
||||
if hasAll && hasAny {
|
||||
return false, fmt.Errorf("condition cannot contain both all and any")
|
||||
}
|
||||
if hasAll {
|
||||
items, ok := all.([]interface{})
|
||||
if !ok || len(items) == 0 {
|
||||
return false, fmt.Errorf("invalid all condition")
|
||||
}
|
||||
for _, item := range items {
|
||||
matched, err := matchConditionValue(item, answers)
|
||||
if err != nil || !matched {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
if any, ok := rule["any"].([]interface{}); ok {
|
||||
for _, item := range any {
|
||||
object, ok := item.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
matched, err := matchRule(object, answers)
|
||||
if hasAny {
|
||||
items, ok := any.([]interface{})
|
||||
if !ok || len(items) == 0 {
|
||||
return false, fmt.Errorf("invalid any condition")
|
||||
}
|
||||
for _, item := range items {
|
||||
matched, err := matchConditionValue(item, answers)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user