Add functionality to visit values in `dyn.Value` tree (#1142)
## Changes
This change adds the following functions:
* `dyn.Get(value, "foo.bar") -> (dyn.Value, error)`
* `dyn.Set(value, "foo.bar", newValue) -> (dyn.Value, error)`
* `dyn.Map(value, "foo.bar", func) -> (dyn.Value, error)`
And equivalent functions that take a previously constructed `dyn.Path`:
* `dyn.GetByPath(value, dyn.Path) -> (dyn.Value, error)`
* `dyn.SetByPath(value, dyn.Path, newValue) -> (dyn.Value, error)`
* `dyn.MapByPath(value, dyn.Path, func) -> (dyn.Value, error)`
Changes made by the "set" and "map" functions are never reflected in the
input argument; they return new `dyn.Value` instances for all nodes in
the path leading up to the changed value.
## Tests
New unit tests cover all critical paths.
2024-01-24 18:38:46 +00:00
|
|
|
package dyn
|
|
|
|
|
2024-03-07 14:13:04 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"maps"
|
|
|
|
"slices"
|
|
|
|
)
|
|
|
|
|
Add functionality to visit values in `dyn.Value` tree (#1142)
## Changes
This change adds the following functions:
* `dyn.Get(value, "foo.bar") -> (dyn.Value, error)`
* `dyn.Set(value, "foo.bar", newValue) -> (dyn.Value, error)`
* `dyn.Map(value, "foo.bar", func) -> (dyn.Value, error)`
And equivalent functions that take a previously constructed `dyn.Path`:
* `dyn.GetByPath(value, dyn.Path) -> (dyn.Value, error)`
* `dyn.SetByPath(value, dyn.Path, newValue) -> (dyn.Value, error)`
* `dyn.MapByPath(value, dyn.Path, func) -> (dyn.Value, error)`
Changes made by the "set" and "map" functions are never reflected in the
input argument; they return new `dyn.Value` instances for all nodes in
the path leading up to the changed value.
## Tests
New unit tests cover all critical paths.
2024-01-24 18:38:46 +00:00
|
|
|
// Set assigns a new value at the specified path in the specified value.
|
|
|
|
// It is identical to [SetByPath], except that it takes a string path instead of a [Path].
|
|
|
|
func Set(v Value, path string, nv Value) (Value, error) {
|
|
|
|
p, err := NewPathFromString(path)
|
|
|
|
if err != nil {
|
|
|
|
return InvalidValue, err
|
|
|
|
}
|
|
|
|
return SetByPath(v, p, nv)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetByPath assigns the given value at the specified path in the specified value.
|
|
|
|
// If successful, it returns the new value with all intermediate values copied and updated.
|
|
|
|
// If the path doesn't exist, it returns InvalidValue and an error.
|
|
|
|
func SetByPath(v Value, p Path, nv Value) (Value, error) {
|
2024-03-07 14:13:04 +00:00
|
|
|
lp := len(p)
|
|
|
|
if lp == 0 {
|
|
|
|
return nv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
component := p[lp-1]
|
2024-03-08 14:33:01 +00:00
|
|
|
p = p[:lp-1]
|
2024-03-07 14:13:04 +00:00
|
|
|
|
2024-03-08 14:33:01 +00:00
|
|
|
return visit(v, EmptyPath, NewPatternFromPath(p), visitOptions{
|
2024-03-07 14:13:04 +00:00
|
|
|
fn: func(prefix Path, v Value) (Value, error) {
|
|
|
|
path := prefix.Append(component)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case component.isKey():
|
|
|
|
// Expect a map to be set if this is a key.
|
|
|
|
m, ok := v.AsMap()
|
|
|
|
if !ok {
|
|
|
|
return InvalidValue, fmt.Errorf("expected a map to index %q, found %s", path, v.Kind())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an updated map value.
|
|
|
|
m = maps.Clone(m)
|
|
|
|
m[component.key] = nv
|
|
|
|
return Value{
|
|
|
|
v: m,
|
|
|
|
k: KindMap,
|
|
|
|
l: v.l,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
case component.isIndex():
|
|
|
|
// Expect a sequence to be set if this is an index.
|
|
|
|
s, ok := v.AsSequence()
|
|
|
|
if !ok {
|
|
|
|
return InvalidValue, fmt.Errorf("expected a sequence to index %q, found %s", path, v.Kind())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup current value in the sequence.
|
|
|
|
if component.index < 0 || component.index >= len(s) {
|
|
|
|
return InvalidValue, indexOutOfBoundsError{prefix}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an updated sequence value.
|
|
|
|
s = slices.Clone(s)
|
|
|
|
s[component.index] = nv
|
|
|
|
return Value{
|
|
|
|
v: s,
|
|
|
|
k: KindSequence,
|
|
|
|
l: v.l,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic("invalid component")
|
|
|
|
}
|
Add functionality to visit values in `dyn.Value` tree (#1142)
## Changes
This change adds the following functions:
* `dyn.Get(value, "foo.bar") -> (dyn.Value, error)`
* `dyn.Set(value, "foo.bar", newValue) -> (dyn.Value, error)`
* `dyn.Map(value, "foo.bar", func) -> (dyn.Value, error)`
And equivalent functions that take a previously constructed `dyn.Path`:
* `dyn.GetByPath(value, dyn.Path) -> (dyn.Value, error)`
* `dyn.SetByPath(value, dyn.Path, newValue) -> (dyn.Value, error)`
* `dyn.MapByPath(value, dyn.Path, func) -> (dyn.Value, error)`
Changes made by the "set" and "map" functions are never reflected in the
input argument; they return new `dyn.Value` instances for all nodes in
the path leading up to the changed value.
## Tests
New unit tests cover all critical paths.
2024-01-24 18:38:46 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|