value.go 2.8 KB

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