Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

string_valid.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package valid
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. )
  8. // ValidateAndConvert 验证并转换配置值(纯函数模式)
  9. func ValidateAndConvert(fieldType, yamlValue string) (string, error) {
  10. if fieldType == "" {
  11. // 如果没有指定类型,按字符串处理
  12. return yamlValue, nil
  13. }
  14. switch fieldType {
  15. case "bool":
  16. return ValidateBool(yamlValue)
  17. case "int":
  18. return ValidateInt(yamlValue)
  19. case "string":
  20. return ValidateString(yamlValue)
  21. case "uint64":
  22. return ValidateUint64(yamlValue)
  23. default:
  24. // 检查是否是 map 类型
  25. if strings.HasPrefix(fieldType, "map[") {
  26. return ValidateMap(fieldType, yamlValue)
  27. }
  28. // 未知类型,按原样返回(可能是自定义类型)
  29. return yamlValue, nil
  30. }
  31. }
  32. // validateBool 验证布尔值(纯函数模式)
  33. func ValidateBool(value string) (string, error) {
  34. if value == "" {
  35. return "false", nil // 空值默认为 false
  36. }
  37. // 支持多种布尔表示形式
  38. switch strings.ToLower(value) {
  39. case "true", "1", "yes", "y", "on":
  40. return "true", nil
  41. case "false", "0", "no", "n", "off":
  42. return "false", nil
  43. default:
  44. // 尝试解析
  45. if b, err := strconv.ParseBool(value); err == nil {
  46. return strconv.FormatBool(b), nil
  47. }
  48. return "", fmt.Errorf("无效的布尔值: %s,可接受值: true/false, 1/0, yes/no, on/off", value)
  49. }
  50. }
  51. // validateInt 验证整数值(纯函数模式)
  52. func ValidateInt(value string) (string, error) {
  53. if value == "" {
  54. return "0", nil // 空值默认为 0
  55. }
  56. // 尝试解析为整数
  57. _, err := strconv.Atoi(value)
  58. if err != nil {
  59. // 也尝试解析为浮点数然后取整
  60. if f, err := strconv.ParseFloat(value, 64); err == nil {
  61. return strconv.Itoa(int(f)), nil
  62. }
  63. return "", fmt.Errorf("无效的整数值: %s", value)
  64. }
  65. return value, nil
  66. }
  67. // validateUint64 验证无符号64位整数(纯函数模式)
  68. func ValidateUint64(value string) (string, error) {
  69. if value == "" {
  70. return "0", nil // 空值默认为 0
  71. }
  72. // 尝试解析为 uint64
  73. _, err := strconv.ParseUint(value, 10, 64)
  74. if err != nil {
  75. return "", fmt.Errorf("无效的uint64值: %s", value)
  76. }
  77. return value, nil
  78. }
  79. // validateString 验证字符串(纯函数模式)
  80. func ValidateString(value string) (string, error) {
  81. // 字符串类型不需要特殊验证
  82. return value, nil
  83. }
  84. // validateMap 验证 map 类型(纯函数模式)
  85. func ValidateMap(fieldType, value string) (string, error) {
  86. if value == "" {
  87. return "{}", nil // 空值返回空对象
  88. }
  89. // 检查是否是期望的 map[string]*subconfigs.DbConfig 类型
  90. if fieldType != "map[string]*subconfigs.DbConfig" {
  91. return "", fmt.Errorf("不支持的map类型: %s", fieldType)
  92. }
  93. // 验证是否为有效的 JSON
  94. if !IsValidJSON(value) {
  95. return "", fmt.Errorf("map类型需要有效的JSON格式")
  96. }
  97. return value, nil
  98. }
  99. // isValidJSON 检查是否为有效的 JSON(纯函数模式)
  100. func IsValidJSON(s string) bool {
  101. var js interface{}
  102. return json.Unmarshal([]byte(s), &js) == nil
  103. }