1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/util/qson/merge.go

36 lines
923 B
Go
Raw Normal View History

package qson
// merge merges a with b if they are either both slices
// or map[string]interface{} types. Otherwise it returns b.
func merge(a interface{}, b interface{}) interface{} {
switch aT := a.(type) {
case map[string]interface{}:
return mergeMap(aT, b.(map[string]interface{}))
case []interface{}:
return mergeSlice(aT, b.([]interface{}))
default:
return b
}
}
// mergeMap merges a with b, attempting to merge any nested
// values in nested maps but eventually overwriting anything
// in a that can't be merged with whatever is in b.
func mergeMap(a map[string]interface{}, b map[string]interface{}) map[string]interface{} {
for bK, bV := range b {
if _, ok := a[bK]; ok {
a[bK] = merge(a[bK], bV)
} else {
a[bK] = bV
}
}
2022-09-30 20:32:55 +02:00
return a
}
// mergeSlice merges a with b and returns the result.
func mergeSlice(a []interface{}, b []interface{}) []interface{} {
a = append(a, b...)
return a
}