mergo.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "errors"
  10. "reflect"
  11. )
  12. // Errors reported by Mergo when it finds invalid arguments.
  13. var (
  14. ErrNilArguments = errors.New("src and dst must not be nil")
  15. ErrDifferentArgumentsTypes = errors.New("src and dst must be of same type")
  16. ErrNotSupported = errors.New("only structs, maps, and slices are supported")
  17. ErrExpectedMapAsDestination = errors.New("dst was expected to be a map")
  18. ErrExpectedStructAsDestination = errors.New("dst was expected to be a struct")
  19. ErrNonPointerAgument = errors.New("dst must be a pointer")
  20. )
  21. // During deepMerge, must keep track of checks that are
  22. // in progress. The comparison algorithm assumes that all
  23. // checks in progress are true when it reencounters them.
  24. // Visited are stored in a map indexed by 17 * a1 + a2;
  25. type visit struct {
  26. ptr uintptr
  27. typ reflect.Type
  28. next *visit
  29. }
  30. // From src/pkg/encoding/json/encode.go.
  31. func isEmptyValue(v reflect.Value) bool {
  32. switch v.Kind() {
  33. case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
  34. return v.Len() == 0
  35. case reflect.Bool:
  36. return !v.Bool()
  37. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  38. return v.Int() == 0
  39. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  40. return v.Uint() == 0
  41. case reflect.Float32, reflect.Float64:
  42. return v.Float() == 0
  43. case reflect.Interface, reflect.Ptr:
  44. if v.IsNil() {
  45. return true
  46. }
  47. return isEmptyValue(v.Elem())
  48. case reflect.Func:
  49. return v.IsNil()
  50. case reflect.Invalid:
  51. return true
  52. }
  53. return false
  54. }
  55. func resolveValues(dst, src interface{}) (vDst, vSrc reflect.Value, err error) {
  56. if dst == nil || src == nil {
  57. err = ErrNilArguments
  58. return
  59. }
  60. vDst = reflect.ValueOf(dst).Elem()
  61. if vDst.Kind() != reflect.Struct && vDst.Kind() != reflect.Map && vDst.Kind() != reflect.Slice {
  62. err = ErrNotSupported
  63. return
  64. }
  65. vSrc = reflect.ValueOf(src)
  66. // We check if vSrc is a pointer to dereference it.
  67. if vSrc.Kind() == reflect.Ptr {
  68. vSrc = vSrc.Elem()
  69. }
  70. return
  71. }