value.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package sql_tpl
  2. import (
  3. "errors"
  4. "git.sxidc.com/go-tools/utils/encoding"
  5. "reflect"
  6. "strconv"
  7. "time"
  8. )
  9. const (
  10. timeWriteFormat = time.DateTime + ".000000 +08:00"
  11. )
  12. type AfterParsedStrValueOption func(strValue string) (string, error)
  13. func WithAESKey(aesKey string) AfterParsedStrValueOption {
  14. return func(strValue string) (string, error) {
  15. encrypted, err := encoding.AESEncrypt(strValue, aesKey)
  16. if err != nil {
  17. return "", err
  18. }
  19. return "'" + encrypted + "'", nil
  20. }
  21. }
  22. func parseValue(value any, opts ...AfterParsedStrValueOption) (string, error) {
  23. valueValue := reflect.ValueOf(value)
  24. if !valueValue.IsValid() {
  25. return "", errors.New("无效值")
  26. }
  27. if valueValue.Kind() == reflect.Ptr && valueValue.IsNil() {
  28. return "", errors.New("空值")
  29. }
  30. if valueValue.Kind() == reflect.Ptr {
  31. valueValue = valueValue.Elem()
  32. }
  33. var parsedValue string
  34. switch v := valueValue.Interface().(type) {
  35. case string:
  36. parsedValue = v
  37. if opts == nil || len(opts) == 0 {
  38. return "'" + parsedValue + "'", nil
  39. }
  40. case bool:
  41. parsedValue = strconv.FormatBool(v)
  42. if opts == nil || len(opts) == 0 {
  43. return parsedValue, nil
  44. }
  45. case time.Time:
  46. parsedValue = v.Format(timeWriteFormat)
  47. if opts == nil || len(opts) == 0 {
  48. return "'" + parsedValue + "'", nil
  49. }
  50. case int:
  51. parsedValue = strconv.Itoa(v)
  52. if opts == nil || len(opts) == 0 {
  53. return parsedValue, nil
  54. }
  55. case int8:
  56. parsedValue = strconv.FormatInt(int64(v), 10)
  57. if opts == nil || len(opts) == 0 {
  58. return parsedValue, nil
  59. }
  60. case int16:
  61. parsedValue = strconv.FormatInt(int64(v), 10)
  62. if opts == nil || len(opts) == 0 {
  63. return parsedValue, nil
  64. }
  65. case int32:
  66. parsedValue = strconv.FormatInt(int64(v), 10)
  67. if opts == nil || len(opts) == 0 {
  68. return parsedValue, nil
  69. }
  70. case int64:
  71. parsedValue = strconv.FormatInt(v, 10)
  72. if opts == nil || len(opts) == 0 {
  73. return parsedValue, nil
  74. }
  75. case uint:
  76. parsedValue = strconv.FormatUint(uint64(v), 10)
  77. if opts == nil || len(opts) == 0 {
  78. return parsedValue, nil
  79. }
  80. case uint8:
  81. parsedValue = strconv.FormatUint(uint64(v), 10)
  82. if opts == nil || len(opts) == 0 {
  83. return parsedValue, nil
  84. }
  85. case uint16:
  86. parsedValue = strconv.FormatUint(uint64(v), 10)
  87. if opts == nil || len(opts) == 0 {
  88. return parsedValue, nil
  89. }
  90. case uint32:
  91. parsedValue = strconv.FormatUint(uint64(v), 10)
  92. if opts == nil || len(opts) == 0 {
  93. return parsedValue, nil
  94. }
  95. case uint64:
  96. parsedValue = strconv.FormatUint(v, 10)
  97. if opts == nil || len(opts) == 0 {
  98. return parsedValue, nil
  99. }
  100. default:
  101. return "", errors.New("不支持的类型")
  102. }
  103. for _, opt := range opts {
  104. innerParsedValue, err := opt(parsedValue)
  105. if err != nil {
  106. return "", err
  107. }
  108. parsedValue = innerParsedValue
  109. }
  110. return parsedValue, nil
  111. }