mirror of
https://github.com/MontFerret/ferret.git
synced 2025-07-17 01:32:22 +02:00
refactoring: .GetIn (#353)
* sync with MontFerret/ferret * fix --param handling When params is converted to map it uses strings.Split, which slices a string into all substrings separated by :. * implement GetIn for values.Object * implement GetIn for values.Array * rewrite GetIn because values.Object and values.Array implement core.Getter now * fix bug when GetIn return nil instead of None * add tests for Object and Array .GetIn * add GetIn comment and remove 'len(byPath)' check * fix GetIn comment
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package values
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"hash/fnv"
|
||||
@ -258,3 +259,29 @@ func (t *Array) SortWith(sorter ArraySorter) *Array {
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func (t *Array) GetIn(ctx context.Context, path []core.Value) (core.Value, error) {
|
||||
if len(path) == 0 {
|
||||
return None, nil
|
||||
}
|
||||
|
||||
if typ := path[0].Type(); typ != types.Int {
|
||||
return None, core.TypeError(typ, types.Int)
|
||||
}
|
||||
|
||||
first := t.Get(path[0].(Int))
|
||||
|
||||
if len(path) == 1 {
|
||||
return first, nil
|
||||
}
|
||||
|
||||
getter, ok := first.(core.Getter)
|
||||
if !ok {
|
||||
return None, core.TypeError(
|
||||
first.Type(),
|
||||
core.NewType("Getter"),
|
||||
)
|
||||
}
|
||||
|
||||
return getter.GetIn(ctx, path[1:])
|
||||
}
|
||||
|
Reference in New Issue
Block a user