...

Source file src/dario.cat/mergo/mergo.go

Documentation: dario.cat/mergo

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

View as plain text