databricks-cli/libs/dyn/merge/elements_by_key.go

68 lines
1.7 KiB
Go
Raw Normal View History

Use dynamic configuration model in bundles (#1098) ## Changes This is a fundamental change to how we load and process bundle configuration. We now depend on the configuration being represented as a `dyn.Value`. This representation is functionally equivalent to Go's `any` (it is variadic) and allows us to capture metadata associated with a value, such as where it was defined (e.g. file, line, and column). It also allows us to represent Go's zero values properly (e.g. empty string, integer equal to 0, or boolean false). Using this representation allows us to let the configuration model deviate from the typed structure we have been relying on so far (`config.Root`). We need to deviate from these types when using variables for fields that are not a string themselves. For example, using `${var.num_workers}` for an integer `workers` field was impossible until now (though not implemented in this change). The loader for a `dyn.Value` includes functionality to capture any and all type mismatches between the user-defined configuration and the expected types. These mismatches can be surfaced as validation errors in future PRs. Given that many mutators expect the typed struct to be the source of truth, this change converts between the dynamic representation and the typed representation on mutator entry and exit. Existing mutators can continue to modify the typed representation and these modifications are reflected in the dynamic representation (see `MarkMutatorEntry` and `MarkMutatorExit` in `bundle/config/root.go`). Required changes included in this change: * The existing interpolation package is removed in favor of `libs/dyn/dynvar`. * Functionality to merge job clusters, job tasks, and pipeline clusters are now all broken out into their own mutators. To be implemented later: * Allow variable references for non-string types. * Surface diagnostics about the configuration provided by the user in the validation output. * Some mutators use a resource's configuration file path to resolve related relative paths. These depend on `bundle/config/paths.Path` being set and populated through `ConfigureConfigFilePath`. Instead, they should interact with the dynamically typed configuration directly. Doing this also unlocks being able to differentiate different base paths used within a job (e.g. a task override with a relative path defined in a directory other than the base job). ## Tests * Existing unit tests pass (some have been modified to accommodate) * Integration tests pass
2024-02-16 19:41:58 +00:00
package merge
import "github.com/databricks/cli/libs/dyn"
type elementsByKey struct {
key string
keyFunc func(dyn.Value) string
}
func (e elementsByKey) Map(v dyn.Value) (dyn.Value, error) {
// We know the type of this value is a sequence.
// For additional defence, return self if it is not.
elements, ok := v.AsSequence()
if !ok {
return v, nil
}
seen := make(map[string]dyn.Value, len(elements))
keys := make([]string, 0, len(elements))
// Iterate in natural order. For a given key, we first see the
// base definition and merge instances that come after it.
for i := range elements {
kv := elements[i].Get(e.key)
key := e.keyFunc(kv)
// Register element with key if not yet seen before.
ref, ok := seen[key]
if !ok {
keys = append(keys, key)
seen[key] = elements[i]
continue
}
// Merge this instance into the reference.
nv, err := Merge(ref, elements[i])
if err != nil {
return v, err
}
// Overwrite reference.
seen[key] = nv
}
// Gather resulting elements in natural order.
out := make([]dyn.Value, 0, len(keys))
for _, key := range keys {
nv, err := dyn.Set(seen[key], e.key, dyn.V(key))
if err != nil {
return dyn.InvalidValue, err
}
out = append(out, nv)
}
return dyn.NewValue(out, v.Location()), nil
}
// ElementsByKey returns a [dyn.MapFunc] that operates on a sequence
// where each element is a map. It groups elements by a key and merges
// elements with the same key.
//
// The function that extracts the key from an element is provided as
// a parameter. The resulting elements get their key field overwritten
// with the value as returned by the key function.
func ElementsByKey(key string, keyFunc func(dyn.Value) string) dyn.MapFunc {
return elementsByKey{key, keyFunc}.Map
}