feat: simplify SOP to immediate-effect configuration
This commit is contained in:
93
internal/run/mapping.go
Normal file
93
internal/run/mapping.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package run
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.iwork-ai.com/xdc/iqudo-top1/internal/model"
|
||||
)
|
||||
|
||||
var pathTokenPattern = regexp.MustCompile(`^([A-Za-z][A-Za-z0-9_]*)(?:\[(\d+|\*)\])?$`)
|
||||
|
||||
func mapScenarioInput(fields []model.ScenarioField, input map[string]interface{}) map[string]interface{} {
|
||||
mapped := make(map[string]interface{})
|
||||
for _, field := range fields {
|
||||
if field.SourcePath == "" {
|
||||
continue
|
||||
}
|
||||
if value, ok := extractPath(input, field.SourcePath); ok {
|
||||
mapped[field.FieldKey] = value
|
||||
}
|
||||
}
|
||||
return mapped
|
||||
}
|
||||
|
||||
func extractPath(root map[string]interface{}, path string) (interface{}, bool) {
|
||||
parts := strings.Split(path, ".")
|
||||
return walkPath(root, parts)
|
||||
}
|
||||
|
||||
func walkPath(current interface{}, parts []string) (interface{}, bool) {
|
||||
if len(parts) == 0 {
|
||||
return current, true
|
||||
}
|
||||
match := pathTokenPattern.FindStringSubmatch(parts[0])
|
||||
if match == nil {
|
||||
return nil, false
|
||||
}
|
||||
object, ok := current.(map[string]interface{})
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
value, ok := object[match[1]]
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
if match[2] == "" {
|
||||
return walkPath(value, parts[1:])
|
||||
}
|
||||
items, ok := value.([]interface{})
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
if match[2] == "*" {
|
||||
values := make([]interface{}, 0)
|
||||
for _, item := range items {
|
||||
resolved, found := walkPath(item, parts[1:])
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
if nested, ok := resolved.([]interface{}); ok {
|
||||
values = append(values, nested...)
|
||||
} else {
|
||||
values = append(values, resolved)
|
||||
}
|
||||
}
|
||||
return values, len(values) > 0
|
||||
}
|
||||
index, err := strconv.Atoi(match[2])
|
||||
if err != nil || index < 0 || index >= len(items) {
|
||||
return nil, false
|
||||
}
|
||||
return walkPath(items[index], parts[1:])
|
||||
}
|
||||
|
||||
func mergeValues(base map[string]interface{}, overrides map[string]interface{}) map[string]interface{} {
|
||||
result := make(map[string]interface{}, len(base)+len(overrides))
|
||||
for key, value := range base {
|
||||
result[key] = value
|
||||
}
|
||||
for key, value := range overrides {
|
||||
result[key] = value
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func validateExternalRef(value string) error {
|
||||
if len(value) > 191 {
|
||||
return fmt.Errorf("external_ref 不能超过191个字符")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user