copy.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package utilx
  2. import (
  3. "database/sql"
  4. "errors"
  5. "reflect"
  6. )
  7. // Copy 复制结构体内容
  8. func Copy(toValue interface{}, fromValue interface{}) (err error) {
  9. var (
  10. isSlice bool
  11. amount = 1
  12. from = indirect(reflect.ValueOf(fromValue))
  13. to = indirect(reflect.ValueOf(toValue))
  14. )
  15. if !to.CanAddr() {
  16. return errors.New("copy to value is unaddressable")
  17. }
  18. // Return is from value is invalid
  19. if !from.IsValid() {
  20. return
  21. }
  22. fromType := indirectType(from.Type())
  23. toType := indirectType(to.Type())
  24. // Just set it if possible to assign
  25. // And need to do copy anyway if the type is struct
  26. if fromType.Kind() != reflect.Struct && from.Type().AssignableTo(to.Type()) {
  27. to.Set(from)
  28. return
  29. }
  30. if fromType.Kind() != reflect.Struct || toType.Kind() != reflect.Struct {
  31. return
  32. }
  33. if to.Kind() == reflect.Slice {
  34. isSlice = true
  35. if from.Kind() == reflect.Slice {
  36. amount = from.Len()
  37. }
  38. }
  39. for i := 0; i < amount; i++ {
  40. var dest, source reflect.Value
  41. if isSlice {
  42. // source
  43. if from.Kind() == reflect.Slice {
  44. source = indirect(from.Index(i))
  45. } else {
  46. source = indirect(from)
  47. }
  48. // dest
  49. dest = indirect(reflect.New(toType).Elem())
  50. } else {
  51. source = indirect(from)
  52. dest = indirect(to)
  53. }
  54. // check source
  55. if source.IsValid() {
  56. fromTypeFields := deepFields(fromType)
  57. //fmt.Printf("%#v", fromTypeFields)
  58. // Copy from field to field or method
  59. for _, field := range fromTypeFields {
  60. name := field.Name
  61. if fromField := source.FieldByName(name); fromField.IsValid() {
  62. // has field
  63. if toField := dest.FieldByName(name); toField.IsValid() {
  64. if toField.CanSet() {
  65. if !set(toField, fromField) {
  66. if err := Copy(toField.Addr().Interface(), fromField.Interface()); err != nil {
  67. return err
  68. }
  69. }
  70. }
  71. } else {
  72. // try to set to method
  73. var toMethod reflect.Value
  74. if dest.CanAddr() {
  75. toMethod = dest.Addr().MethodByName(name)
  76. } else {
  77. toMethod = dest.MethodByName(name)
  78. }
  79. if toMethod.IsValid() && toMethod.Type().NumIn() == 1 && fromField.Type().AssignableTo(toMethod.Type().In(0)) {
  80. toMethod.Call([]reflect.Value{fromField})
  81. }
  82. }
  83. }
  84. }
  85. // Copy from method to field
  86. for _, field := range deepFields(toType) {
  87. name := field.Name
  88. var fromMethod reflect.Value
  89. if source.CanAddr() {
  90. fromMethod = source.Addr().MethodByName(name)
  91. } else {
  92. fromMethod = source.MethodByName(name)
  93. }
  94. if fromMethod.IsValid() && fromMethod.Type().NumIn() == 0 && fromMethod.Type().NumOut() == 1 {
  95. if toField := dest.FieldByName(name); toField.IsValid() && toField.CanSet() {
  96. values := fromMethod.Call([]reflect.Value{})
  97. if len(values) >= 1 {
  98. set(toField, values[0])
  99. }
  100. }
  101. }
  102. }
  103. }
  104. if isSlice {
  105. if dest.Addr().Type().AssignableTo(to.Type().Elem()) {
  106. to.Set(reflect.Append(to, dest.Addr()))
  107. } else if dest.Type().AssignableTo(to.Type().Elem()) {
  108. to.Set(reflect.Append(to, dest))
  109. }
  110. }
  111. }
  112. return
  113. }
  114. func deepFields(reflectType reflect.Type) []reflect.StructField {
  115. var fields []reflect.StructField
  116. if reflectType = indirectType(reflectType); reflectType.Kind() == reflect.Struct {
  117. for i := 0; i < reflectType.NumField(); i++ {
  118. v := reflectType.Field(i)
  119. if v.Anonymous {
  120. fields = append(fields, deepFields(v.Type)...)
  121. } else {
  122. fields = append(fields, v)
  123. }
  124. }
  125. }
  126. return fields
  127. }
  128. func indirect(reflectValue reflect.Value) reflect.Value {
  129. for reflectValue.Kind() == reflect.Ptr {
  130. reflectValue = reflectValue.Elem()
  131. }
  132. return reflectValue
  133. }
  134. func indirectType(reflectType reflect.Type) reflect.Type {
  135. for reflectType.Kind() == reflect.Ptr || reflectType.Kind() == reflect.Slice {
  136. reflectType = reflectType.Elem()
  137. }
  138. return reflectType
  139. }
  140. func set(to, from reflect.Value) bool {
  141. if from.IsValid() {
  142. if to.Kind() == reflect.Ptr {
  143. //set `to` to nil if from is nil
  144. if from.Kind() == reflect.Ptr && from.IsNil() {
  145. to.Set(reflect.Zero(to.Type()))
  146. return true
  147. } else if to.IsNil() {
  148. to.Set(reflect.New(to.Type().Elem()))
  149. }
  150. to = to.Elem()
  151. }
  152. if from.Type().ConvertibleTo(to.Type()) {
  153. to.Set(from.Convert(to.Type()))
  154. } else if scanner, ok := to.Addr().Interface().(sql.Scanner); ok {
  155. err := scanner.Scan(from.Interface())
  156. if err != nil {
  157. return false
  158. }
  159. } else if from.Kind() == reflect.Ptr {
  160. return set(to, from.Elem())
  161. } else {
  162. return false
  163. }
  164. }
  165. return true
  166. }