2023-11-03 19:15:47 +00:00
|
|
|
package merge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2023-11-03 19:15:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Merge recursively merges the specified values.
|
|
|
|
//
|
|
|
|
// Semantics are as follows:
|
|
|
|
// * Merging x with nil or nil with x always yields x.
|
|
|
|
// * Merging maps a and b means entries from map b take precedence.
|
|
|
|
// * Merging sequences a and b means concatenating them.
|
2023-12-22 13:20:45 +00:00
|
|
|
func Merge(a, b dyn.Value) (dyn.Value, error) {
|
2023-11-03 19:15:47 +00:00
|
|
|
return merge(a, b)
|
|
|
|
}
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
func merge(a, b dyn.Value) (dyn.Value, error) {
|
2023-11-03 19:15:47 +00:00
|
|
|
ak := a.Kind()
|
|
|
|
bk := b.Kind()
|
|
|
|
|
|
|
|
// If a is nil, return b.
|
2023-12-22 13:20:45 +00:00
|
|
|
if ak == dyn.KindNil {
|
2023-11-03 19:15:47 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If b is nil, return a.
|
2023-12-22 13:20:45 +00:00
|
|
|
if bk == dyn.KindNil {
|
2023-11-03 19:15:47 +00:00
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the appropriate merge function based on the kind of a and b.
|
|
|
|
switch ak {
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindMap:
|
|
|
|
if bk != dyn.KindMap {
|
|
|
|
return dyn.NilValue, fmt.Errorf("cannot merge map with %s", bk)
|
2023-11-03 19:15:47 +00:00
|
|
|
}
|
|
|
|
return mergeMap(a, b)
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindSequence:
|
|
|
|
if bk != dyn.KindSequence {
|
|
|
|
return dyn.NilValue, fmt.Errorf("cannot merge sequence with %s", bk)
|
2023-11-03 19:15:47 +00:00
|
|
|
}
|
|
|
|
return mergeSequence(a, b)
|
|
|
|
default:
|
|
|
|
if ak != bk {
|
2023-12-22 13:20:45 +00:00
|
|
|
return dyn.NilValue, fmt.Errorf("cannot merge %s with %s", ak, bk)
|
2023-11-03 19:15:47 +00:00
|
|
|
}
|
|
|
|
return mergePrimitive(a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
func mergeMap(a, b dyn.Value) (dyn.Value, error) {
|
|
|
|
out := make(map[string]dyn.Value)
|
2023-11-03 19:15:47 +00:00
|
|
|
am := a.MustMap()
|
|
|
|
bm := b.MustMap()
|
|
|
|
|
|
|
|
// Add the values from a into the output map.
|
|
|
|
for k, v := range am {
|
|
|
|
out[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the values from b into the output map.
|
|
|
|
for k, v := range bm {
|
|
|
|
if _, ok := out[k]; ok {
|
|
|
|
// If the key already exists, merge the values.
|
|
|
|
merged, err := merge(out[k], v)
|
|
|
|
if err != nil {
|
2023-12-22 13:20:45 +00:00
|
|
|
return dyn.NilValue, err
|
2023-11-03 19:15:47 +00:00
|
|
|
}
|
|
|
|
out[k] = merged
|
|
|
|
} else {
|
|
|
|
// Otherwise, just set the value.
|
|
|
|
out[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preserve the location of the first value.
|
2023-12-22 13:20:45 +00:00
|
|
|
return dyn.NewValue(out, a.Location()), nil
|
2023-11-03 19:15:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
func mergeSequence(a, b dyn.Value) (dyn.Value, error) {
|
2023-11-03 19:15:47 +00:00
|
|
|
as := a.MustSequence()
|
|
|
|
bs := b.MustSequence()
|
|
|
|
|
|
|
|
// Merging sequences means concatenating them.
|
2023-12-22 13:20:45 +00:00
|
|
|
out := make([]dyn.Value, len(as)+len(bs))
|
2023-11-03 19:15:47 +00:00
|
|
|
copy(out[:], as)
|
|
|
|
copy(out[len(as):], bs)
|
|
|
|
|
|
|
|
// Preserve the location of the first value.
|
2023-12-22 13:20:45 +00:00
|
|
|
return dyn.NewValue(out, a.Location()), nil
|
2023-11-03 19:15:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
func mergePrimitive(a, b dyn.Value) (dyn.Value, error) {
|
2023-11-03 19:15:47 +00:00
|
|
|
// Merging primitive values means using the incoming value.
|
|
|
|
return b, nil
|
|
|
|
}
|