usage.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package assign
  2. import (
  3. "git.sxidc.com/go-tools/utils/reflectutils"
  4. "git.sxidc.com/go-tools/utils/strutils"
  5. "reflect"
  6. "strings"
  7. "time"
  8. )
  9. func DefaultUsage(from any, to any) error {
  10. return UseTag(from, to,
  11. func(fromFieldName string, fromFieldElemValue reflect.Value, retFieldElementValue reflect.Value, tag *Tag) error {
  12. if fromFieldElemValue.IsZero() {
  13. return nil
  14. }
  15. return defaultCallback(fromFieldElemValue, retFieldElementValue, tag)
  16. })
  17. }
  18. func defaultCallback(fromFieldElemValue reflect.Value, toFieldElemValue reflect.Value, tag *Tag) error {
  19. fromKind := reflectutils.GroupValueKind(fromFieldElemValue)
  20. toKind := reflectutils.GroupValueKind(toFieldElemValue)
  21. var fromAny any
  22. switch fromKind {
  23. case reflect.Struct:
  24. // time.Time类型的结构,接收字段是string类型,使用FormatTime的格式转换
  25. if reflectutils.IsValueTime(fromFieldElemValue) && toKind == reflect.String {
  26. fromString := fromFieldElemValue.Interface().(time.Time).Format(tag.TimeLayout)
  27. fromAny = trimFromString(fromString, tag)
  28. break
  29. }
  30. // 不是time.Time类型的结构,接收字段是结构,执行结构到结构字段的赋值
  31. if !reflectutils.IsValueTime(fromFieldElemValue) && toKind == reflect.Struct {
  32. return parseTag(fromFieldElemValue, &toFieldElemValue, func(fromFieldName string, fromFieldElemValue reflect.Value, toFieldElementValue reflect.Value, tag *Tag) error {
  33. return defaultCallback(fromFieldElemValue, toFieldElementValue, tag)
  34. })
  35. }
  36. // 直接将整个结构进行字段赋值
  37. fromAny = fromFieldElemValue.Interface()
  38. case reflect.Slice:
  39. if reflectutils.IsSliceValueOf(fromFieldElemValue, reflect.String) && toKind == reflect.String {
  40. fromString := strings.Join(fromFieldElemValue.Interface().([]string), tag.JoinWith)
  41. fromAny = trimFromString(fromString, tag)
  42. break
  43. }
  44. fromAny = fromFieldElemValue.Interface()
  45. case reflect.String:
  46. fromString := fromFieldElemValue.String()
  47. if reflectutils.IsValueTime(toFieldElemValue) {
  48. toTimeField, err := time.ParseInLocation(tag.TimeLayout, fromString, time.Local)
  49. if err != nil {
  50. return err
  51. }
  52. fromAny = toTimeField
  53. break
  54. }
  55. if reflectutils.IsSliceValueOf(toFieldElemValue, reflect.String) {
  56. fromAny = strings.Split(fromString, tag.SplitWith)
  57. break
  58. }
  59. fromAny = trimFromString(fromString, tag)
  60. default:
  61. fromAny = fromFieldElemValue.Interface()
  62. }
  63. switch toKind {
  64. case reflect.Int64:
  65. return reflectutils.AssignInt64Value(fromAny, toFieldElemValue)
  66. case reflect.Uint64:
  67. return reflectutils.AssignUint64Value(fromAny, toFieldElemValue)
  68. case reflect.Float64:
  69. return reflectutils.AssignFloat64Value(fromAny, toFieldElemValue)
  70. case reflect.Bool:
  71. return reflectutils.AssignBoolValue(fromAny, toFieldElemValue)
  72. case reflect.String:
  73. return reflectutils.AssignStringValue(fromAny, toFieldElemValue)
  74. default:
  75. toFieldElemValue.Set(reflect.ValueOf(fromAny))
  76. return nil
  77. }
  78. }
  79. func trimFromString(fromString string, tag *Tag) string {
  80. if strutils.IsStringNotEmpty(tag.Trim) {
  81. return strings.Trim(fromString, tag.Trim)
  82. } else {
  83. if strutils.IsStringNotEmpty(tag.TrimPrefix) {
  84. return strings.TrimPrefix(fromString, tag.TrimPrefix)
  85. }
  86. if strutils.IsStringNotEmpty(tag.TrimSuffix) {
  87. return strings.TrimSuffix(fromString, tag.TrimSuffix)
  88. }
  89. }
  90. return fromString
  91. }