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.
6 // Based on src/pkg/reflect/deepequal.go from official
16 // Errors reported by Mergo when it finds invalid arguments.
18 ErrNilArguments = errors.New("src and dst must not be nil")
19 ErrDifferentArgumentsTypes = errors.New("src and dst must be of same type")
20 ErrNotSupported = errors.New("only structs and maps are supported")
21 ErrExpectedMapAsDestination = errors.New("dst was expected to be a map")
22 ErrExpectedStructAsDestination = errors.New("dst was expected to be a struct")
25 // During deepMerge, must keep track of checks that are
26 // in progress. The comparison algorithm assumes that all
27 // checks in progress are true when it reencounters them.
28 // Visited are stored in a map indexed by 17 * a1 + a2;
35 // From src/pkg/encoding/json.
36 func isEmptyValue(v reflect.Value) bool {
38 case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
42 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
44 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
46 case reflect.Float32, reflect.Float64:
48 case reflect.Interface, reflect.Ptr:
54 func resolveValues(dst, src interface{}) (vDst, vSrc reflect.Value, err error) {
55 if dst == nil || src == nil {
59 vDst = reflect.ValueOf(dst).Elem()
60 if vDst.Kind() != reflect.Struct && vDst.Kind() != reflect.Map {
64 vSrc = reflect.ValueOf(src)
65 // We check if vSrc is a pointer to dereference it.
66 if vSrc.Kind() == reflect.Ptr {
72 // Traverses recursively both values, assigning src's fields values to dst.
73 // The map argument tracks comparisons that have already been seen, which allows
74 // short circuiting on recursive types.
75 func deeper(dst, src reflect.Value, visited map[uintptr]*visit, depth int) (err error) {
77 addr := dst.UnsafeAddr()
81 for p := seen; p != nil; p = p.next {
82 if p.ptr == addr && p.typ == typ {
86 // Remember, remember...
87 visited[h] = &visit{addr, typ, seen}
89 return // TODO refactor