| 1121 | } |
| 1122 | |
| 1123 | func ParseFunctionCallArgs(functionArguments string, functionConfig FunctionsConfig) string { |
| 1124 | // Clean up double curly braces (common issue with template engines) |
| 1125 | // Replace {{ with { and }} with } but only if they appear at the start/end |
| 1126 | // This handles cases like {{"key":"value"}} -> {"key":"value"} |
| 1127 | cleaned := functionArguments |
| 1128 | //if strings.HasPrefix(cleaned, "{{") && strings.HasSuffix(cleaned, "}}") { |
| 1129 | // Check if it's double braces at the boundaries |
| 1130 | // cleaned = strings.TrimPrefix(cleaned, "{") |
| 1131 | // cleaned = strings.TrimSuffix(cleaned, "}") |
| 1132 | //} |
| 1133 | |
| 1134 | if len(functionConfig.ArgumentRegex) == 0 { |
| 1135 | return cleaned |
| 1136 | } |
| 1137 | |
| 1138 | // We use named regexes here to extract the function argument key value pairs and convert this to valid json. |
| 1139 | // TODO: there might be responses where an object as a value is expected/required. This is currently not handled. |
| 1140 | args := make(map[string]string) |
| 1141 | |
| 1142 | agrsRegexKeyName := "key" |
| 1143 | agrsRegexValueName := "value" |
| 1144 | |
| 1145 | if functionConfig.ArgumentRegexKey != "" { |
| 1146 | agrsRegexKeyName = functionConfig.ArgumentRegexKey |
| 1147 | } |
| 1148 | if functionConfig.ArgumentRegexValue != "" { |
| 1149 | agrsRegexValueName = functionConfig.ArgumentRegexValue |
| 1150 | } |
| 1151 | |
| 1152 | for _, r := range functionConfig.ArgumentRegex { |
| 1153 | var respRegex = regexp.MustCompile(r) |
| 1154 | var nameRange []string = respRegex.SubexpNames() |
| 1155 | var keyIndex = slices.Index(nameRange, agrsRegexKeyName) |
| 1156 | var valueIndex = slices.Index(nameRange, agrsRegexValueName) |
| 1157 | matches := respRegex.FindAllStringSubmatch(functionArguments, -1) |
| 1158 | for _, match := range matches { |
| 1159 | args[match[keyIndex]] = match[valueIndex] |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | jsonBytes, _ := json.Marshal(args) |
| 1164 | |
| 1165 | return string(jsonBytes) |
| 1166 | } |