merge.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2013 Dario Castañé. All rights reserved.
  2. // Copyright 2009 The Go Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. // Based on src/pkg/reflect/deepequal.go from official
  6. // golang's stdlib.
  7. package mergo
  8. import (
  9. "reflect"
  10. )
  11. // Traverses recursively both values, assigning src's fields values to dst.
  12. // The map argument tracks comparisons that have already been seen, which allows
  13. // short circuiting on recursive types.
  14. func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, overwrite bool) (err error) {
  15. if !src.IsValid() {
  16. return
  17. }
  18. if dst.CanAddr() {
  19. addr := dst.UnsafeAddr()
  20. h := 17 * addr
  21. seen := visited[h]
  22. typ := dst.Type()
  23. for p := seen; p != nil; p = p.next {
  24. if p.ptr == addr && p.typ == typ {
  25. return nil
  26. }
  27. }
  28. // Remember, remember...
  29. visited[h] = &visit{addr, typ, seen}
  30. }
  31. switch dst.Kind() {
  32. case reflect.Struct:
  33. for i, n := 0, dst.NumField(); i < n; i++ {
  34. if err = deepMerge(dst.Field(i), src.Field(i), visited, depth+1, overwrite); err != nil {
  35. return
  36. }
  37. }
  38. case reflect.Map:
  39. for _, key := range src.MapKeys() {
  40. srcElement := src.MapIndex(key)
  41. if !srcElement.IsValid() {
  42. continue
  43. }
  44. dstElement := dst.MapIndex(key)
  45. switch srcElement.Kind() {
  46. case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Slice:
  47. if srcElement.IsNil() {
  48. continue
  49. }
  50. fallthrough
  51. default:
  52. switch reflect.TypeOf(srcElement.Interface()).Kind() {
  53. case reflect.Struct:
  54. fallthrough
  55. case reflect.Ptr:
  56. fallthrough
  57. case reflect.Map:
  58. if err = deepMerge(dstElement, srcElement, visited, depth+1, overwrite); err != nil {
  59. return
  60. }
  61. }
  62. }
  63. if !isEmptyValue(srcElement) && (overwrite || (!dstElement.IsValid() || isEmptyValue(dst))) {
  64. if dst.IsNil() {
  65. dst.Set(reflect.MakeMap(dst.Type()))
  66. }
  67. dst.SetMapIndex(key, srcElement)
  68. }
  69. }
  70. case reflect.Ptr:
  71. fallthrough
  72. case reflect.Interface:
  73. if src.IsNil() {
  74. break
  75. } else if dst.IsNil() {
  76. if dst.CanSet() && (overwrite || isEmptyValue(dst)) {
  77. dst.Set(src)
  78. }
  79. } else if err = deepMerge(dst.Elem(), src.Elem(), visited, depth+1, overwrite); err != nil {
  80. return
  81. }
  82. default:
  83. if dst.CanSet() && !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) {
  84. dst.Set(src)
  85. }
  86. }
  87. return
  88. }
  89. // Merge sets fields' values in dst from src if they have a zero
  90. // value of their type.
  91. // dst and src must be valid same-type structs and dst must be
  92. // a pointer to struct.
  93. // It won't merge unexported (private) fields and will do recursively
  94. // any exported field.
  95. func Merge(dst, src interface{}) error {
  96. return merge(dst, src, false)
  97. }
  98. func MergeWithOverwrite(dst, src interface{}) error {
  99. return merge(dst, src, true)
  100. }
  101. func merge(dst, src interface{}, overwrite bool) error {
  102. var (
  103. vDst, vSrc reflect.Value
  104. err error
  105. )
  106. if vDst, vSrc, err = resolveValues(dst, src); err != nil {
  107. return err
  108. }
  109. if vDst.Type() != vSrc.Type() {
  110. return ErrDifferentArgumentsTypes
  111. }
  112. return deepMerge(vDst, vSrc, make(map[uintptr]*visit), 0, overwrite)
  113. }