优化结构
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
bizpayment "kra/internal/biz/payment"
|
||||
"net"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -285,6 +286,44 @@ func validateCommunicationIntegrationConfig(kind, provider string, values map[st
|
||||
if integrationInt64(values, "reconnect_interval", 0) <= 0 {
|
||||
return errors.New("rabbitmq reconnect_interval 必须大于 0")
|
||||
}
|
||||
case IntegrationKindMQ + "/kafka":
|
||||
brokers := integrationStrings(values, "brokers")
|
||||
if len(brokers) == 0 {
|
||||
return errors.New("kafka brokers 不能为空")
|
||||
}
|
||||
for _, broker := range brokers {
|
||||
host, portText, err := net.SplitHostPort(broker)
|
||||
port, parseErr := strconv.Atoi(portText)
|
||||
if err != nil || parseErr != nil || strings.TrimSpace(host) == "" || port < 1 || port > 65535 {
|
||||
return fmt.Errorf("kafka broker %q 必须是有效的 host:port 地址", broker)
|
||||
}
|
||||
}
|
||||
username := integrationText(values, "username")
|
||||
password := integrationText(values, "password")
|
||||
if (username == "") != (password == "") {
|
||||
return errors.New("kafka username 和 password 必须同时配置")
|
||||
}
|
||||
startOffset := strings.ToLower(integrationText(values, "start_offset"))
|
||||
if startOffset != "earliest" && startOffset != "latest" {
|
||||
return errors.New("kafka start_offset 必须是 earliest 或 latest")
|
||||
}
|
||||
minBytes := integrationInt64(values, "min_bytes", 0)
|
||||
maxBytes := integrationInt64(values, "max_bytes", 0)
|
||||
if minBytes <= 0 || maxBytes < minBytes {
|
||||
return errors.New("kafka max_bytes 必须大于等于 min_bytes,且二者必须大于 0")
|
||||
}
|
||||
if integrationInt64(values, "max_wait", 0) <= 0 {
|
||||
return errors.New("kafka max_wait 必须大于 0")
|
||||
}
|
||||
if integrationInt64(values, "connect_timeout", 0) <= 0 {
|
||||
return errors.New("kafka connect_timeout 必须大于 0")
|
||||
}
|
||||
if integrationInt64(values, "reconnect_interval", 0) <= 0 {
|
||||
return errors.New("kafka reconnect_interval 必须大于 0")
|
||||
}
|
||||
if integrationBool(values, "tls_skip_verify") && !integrationBool(values, "tls") {
|
||||
return errors.New("kafka tls_skip_verify 仅能在启用 TLS 时使用")
|
||||
}
|
||||
case IntegrationKindWebSocket + "/melody":
|
||||
path := integrationText(values, "path")
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
@@ -391,6 +430,34 @@ func integrationText(values map[string]any, key string) string {
|
||||
return strings.TrimSpace(fmt.Sprint(value))
|
||||
}
|
||||
|
||||
func integrationStrings(values map[string]any, key string) []string {
|
||||
switch items := values[key].(type) {
|
||||
case []string:
|
||||
result := make([]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
if item = strings.TrimSpace(item); item != "" {
|
||||
result = append(result, item)
|
||||
}
|
||||
}
|
||||
return result
|
||||
case []any:
|
||||
result := make([]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
if value := strings.TrimSpace(fmt.Sprint(item)); value != "" {
|
||||
result = append(result, value)
|
||||
}
|
||||
}
|
||||
return result
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func integrationBool(values map[string]any, key string) bool {
|
||||
value, _ := values[key].(bool)
|
||||
return value
|
||||
}
|
||||
|
||||
func integrationFirst(values map[string]any, keys ...string) string {
|
||||
for _, key := range keys {
|
||||
if value := integrationText(values, key); value != "" {
|
||||
|
||||
Reference in New Issue
Block a user