port.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package apijson
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. // Port copies over values from one struct to another struct.
  7. func Port(from any, to any) error {
  8. toVal := reflect.ValueOf(to)
  9. fromVal := reflect.ValueOf(from)
  10. if toVal.Kind() != reflect.Ptr || toVal.IsNil() {
  11. return fmt.Errorf("destination must be a non-nil pointer")
  12. }
  13. for toVal.Kind() == reflect.Ptr {
  14. toVal = toVal.Elem()
  15. }
  16. toType := toVal.Type()
  17. for fromVal.Kind() == reflect.Ptr {
  18. fromVal = fromVal.Elem()
  19. }
  20. fromType := fromVal.Type()
  21. if toType.Kind() != reflect.Struct {
  22. return fmt.Errorf("destination must be a non-nil pointer to a struct (%v %v)", toType, toType.Kind())
  23. }
  24. values := map[string]reflect.Value{}
  25. fields := map[string]reflect.Value{}
  26. fromJSON := fromVal.FieldByName("JSON")
  27. toJSON := toVal.FieldByName("JSON")
  28. // Iterate through the fields of v and load all the "normal" fields in the struct to the map of
  29. // string to reflect.Value, as well as their raw .JSON.Foo counterpart indicated by j.
  30. var getFields func(t reflect.Type, v reflect.Value)
  31. getFields = func(t reflect.Type, v reflect.Value) {
  32. j := v.FieldByName("JSON")
  33. // Recurse into anonymous fields first, since the fields on the object should win over the fields in the
  34. // embedded object.
  35. for i := 0; i < t.NumField(); i++ {
  36. field := t.Field(i)
  37. if field.Anonymous {
  38. getFields(field.Type, v.Field(i))
  39. continue
  40. }
  41. }
  42. for i := 0; i < t.NumField(); i++ {
  43. field := t.Field(i)
  44. ptag, ok := parseJSONStructTag(field)
  45. if !ok || ptag.name == "-" {
  46. continue
  47. }
  48. values[ptag.name] = v.Field(i)
  49. if j.IsValid() {
  50. fields[ptag.name] = j.FieldByName(field.Name)
  51. }
  52. }
  53. }
  54. getFields(fromType, fromVal)
  55. // Use the values from the previous step to populate the 'to' struct.
  56. for i := 0; i < toType.NumField(); i++ {
  57. field := toType.Field(i)
  58. ptag, ok := parseJSONStructTag(field)
  59. if !ok {
  60. continue
  61. }
  62. if ptag.name == "-" {
  63. continue
  64. }
  65. if value, ok := values[ptag.name]; ok {
  66. delete(values, ptag.name)
  67. if field.Type.Kind() == reflect.Interface {
  68. toVal.Field(i).Set(value)
  69. } else {
  70. switch value.Kind() {
  71. case reflect.String:
  72. toVal.Field(i).SetString(value.String())
  73. case reflect.Bool:
  74. toVal.Field(i).SetBool(value.Bool())
  75. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  76. toVal.Field(i).SetInt(value.Int())
  77. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  78. toVal.Field(i).SetUint(value.Uint())
  79. case reflect.Float32, reflect.Float64:
  80. toVal.Field(i).SetFloat(value.Float())
  81. default:
  82. toVal.Field(i).Set(value)
  83. }
  84. }
  85. }
  86. if fromJSONField, ok := fields[ptag.name]; ok {
  87. if toJSONField := toJSON.FieldByName(field.Name); toJSONField.IsValid() {
  88. toJSONField.Set(fromJSONField)
  89. }
  90. }
  91. }
  92. // Finally, copy over the .JSON.raw and .JSON.ExtraFields
  93. if toJSON.IsValid() {
  94. if raw := toJSON.FieldByName("raw"); raw.IsValid() {
  95. setUnexportedField(raw, fromJSON.Interface().(interface{ RawJSON() string }).RawJSON())
  96. }
  97. if toExtraFields := toJSON.FieldByName("ExtraFields"); toExtraFields.IsValid() {
  98. if fromExtraFields := fromJSON.FieldByName("ExtraFields"); fromExtraFields.IsValid() {
  99. setUnexportedField(toExtraFields, fromExtraFields.Interface())
  100. }
  101. }
  102. }
  103. return nil
  104. }