tag.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package sql_result
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
  4. "git.sxidc.com/go-tools/utils/reflectutils"
  5. "git.sxidc.com/go-tools/utils/strutils"
  6. "git.sxidc.com/go-tools/utils/template"
  7. "github.com/iancoleman/strcase"
  8. "github.com/pkg/errors"
  9. "reflect"
  10. "strings"
  11. "time"
  12. )
  13. const (
  14. defaultSplitWith = "::"
  15. tagPartSeparator = ";"
  16. tagPartKeyValueSeparator = ":"
  17. )
  18. const (
  19. tagKey = "sqlresult"
  20. tagPartIgnore = "-"
  21. tagPartColumn = "column"
  22. tagPartTimeLayout = "timeLayout"
  23. tagPartAes = "aes"
  24. tagPartSplitWith = "splitWith"
  25. tagPartTrim = "trim"
  26. tagPartTrimPrefix = "trimPrefix"
  27. tagPartTrimSuffix = "trimSuffix"
  28. )
  29. type Tag struct {
  30. Name string
  31. TimeLayout string
  32. AESKey string
  33. SplitWith string
  34. Trim string
  35. TrimPrefix string
  36. TrimSuffix string
  37. }
  38. func parseTag(entityElemValue reflect.Value, onParsedFieldTagFunc OnParsedFieldTagFunc) error {
  39. for i := 0; i < entityElemValue.NumField(); i++ {
  40. entityField := entityElemValue.Type().Field(i)
  41. entityFieldValue := entityElemValue.Field(i)
  42. // 无效值,不进行映射
  43. if !entityFieldValue.IsValid() {
  44. continue
  45. }
  46. if entityFieldValue.Kind() == reflect.Pointer && entityFieldValue.IsNil() {
  47. entityFieldValue.Set(reflect.New(entityField.Type.Elem()))
  48. }
  49. entityFieldElemValue := reflectutils.PointerValueElem(entityFieldValue)
  50. tagStr := entityField.Tag.Get(tagKey)
  51. tag, err := parseFieldTag(entityField, tagStr)
  52. if err != nil {
  53. return err
  54. }
  55. if tag == nil {
  56. continue
  57. }
  58. // 结构类型的字段,解析结构内部
  59. if entityFieldElemValue.Kind() == reflect.Struct &&
  60. !reflectutils.IsValueTime(entityFieldElemValue) {
  61. err := parseTag(entityFieldElemValue, onParsedFieldTagFunc)
  62. if err != nil {
  63. return err
  64. }
  65. continue
  66. }
  67. err = onParsedFieldTagFunc(entityField.Name, entityFieldElemValue, tag)
  68. if err != nil {
  69. return err
  70. }
  71. }
  72. return nil
  73. }
  74. func parseFieldTag(field reflect.StructField, tagStr string) (*Tag, error) {
  75. if tagStr == tagPartIgnore {
  76. return nil, nil
  77. }
  78. tag := &Tag{
  79. Name: strcase.ToSnake(template.Id(field.Name)),
  80. TimeLayout: time.DateTime,
  81. AESKey: "",
  82. SplitWith: defaultSplitWith,
  83. }
  84. if strutils.IsStringEmpty(tagStr) {
  85. return tag, nil
  86. }
  87. tagParts := strings.Split(tagStr, tagPartSeparator)
  88. if tagParts != nil || len(tagParts) != 0 {
  89. for _, tagPart := range tagParts {
  90. tagPartKeyValue := strings.SplitN(strings.TrimSpace(tagPart), tagPartKeyValueSeparator, 2)
  91. if tagPartKeyValue != nil && len(tagPartKeyValue) == 2 && strutils.IsStringNotEmpty(tagPartKeyValue[1]) {
  92. tagPartKeyValue[1] = strings.Trim(tagPartKeyValue[1], "'")
  93. }
  94. if strutils.IsStringEmpty(tagPartKeyValue[0]) {
  95. continue
  96. }
  97. switch tagPartKeyValue[0] {
  98. case tagPartColumn:
  99. tag.Name = tagPartKeyValue[1]
  100. case tagPartTimeLayout:
  101. tag.TimeLayout = tagPartKeyValue[1]
  102. case tagPartAes:
  103. if len(tagPartKeyValue[1]) != 32 {
  104. return nil, errors.New("AES密钥长度应该为32个字节")
  105. }
  106. tag.AESKey = tagPartKeyValue[1]
  107. case tagPartSplitWith:
  108. if strutils.IsStringEmpty(tagPartKeyValue[1]) {
  109. return nil, errors.New(defaultSplitWith + "没有赋值分隔符")
  110. }
  111. tag.SplitWith = tagPartKeyValue[1]
  112. case tagPartTrim:
  113. tag.Trim = tagPartKeyValue[1]
  114. case tagPartTrimPrefix:
  115. tag.TrimPrefix = tagPartKeyValue[1]
  116. case tagPartTrimSuffix:
  117. tag.TrimSuffix = tagPartKeyValue[1]
  118. default:
  119. err := errors.New(tagKey + "不支持的tag: " + tagPartKeyValue[0])
  120. logger.GetInstance().Error(err)
  121. continue
  122. }
  123. }
  124. }
  125. return tag, nil
  126. }