github.com/annwntech/go-micro/v2@v2.9.5/util/qson/merge.go (about)

     1  package qson
     2  
     3  // merge merges a with b if they are either both slices
     4  // or map[string]interface{} types. Otherwise it returns b.
     5  func merge(a interface{}, b interface{}) interface{} {
     6  	switch aT := a.(type) {
     7  	case map[string]interface{}:
     8  		return mergeMap(aT, b.(map[string]interface{}))
     9  	case []interface{}:
    10  		return mergeSlice(aT, b.([]interface{}))
    11  	default:
    12  		return b
    13  	}
    14  }
    15  
    16  // mergeMap merges a with b, attempting to merge any nested
    17  // values in nested maps but eventually overwriting anything
    18  // in a that can't be merged with whatever is in b.
    19  func mergeMap(a map[string]interface{}, b map[string]interface{}) map[string]interface{} {
    20  	for bK, bV := range b {
    21  		if _, ok := a[bK]; ok {
    22  			a[bK] = merge(a[bK], bV)
    23  		} else {
    24  			a[bK] = bV
    25  		}
    26  	}
    27  	return a
    28  }
    29  
    30  // mergeSlice merges a with b and returns the result.
    31  func mergeSlice(a []interface{}, b []interface{}) []interface{} {
    32  	a = append(a, b...)
    33  	return a
    34  }