1
0
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:
3timeslazy
2019-08-21 05:00:15 +03:00
committed by Tim Voronov
parent 1c2096ae7e
commit f87fe1e669
5 changed files with 237 additions and 39 deletions

View File

@ -1,12 +1,14 @@
package values_test
import (
"context"
"encoding/json"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
"testing"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
. "github.com/smartystreets/goconvey/convey"
)
@ -561,4 +563,90 @@ func TestArray(t *testing.T) {
So(nestedInArr.Compare(nestedInClone), ShouldNotEqual, 0)
})
})
Convey(".GetIn", t, func() {
ctx := context.Background()
Convey("Should return the same as .Get when input is correct", func() {
Convey("Should return item by key", func() {
key := values.NewInt(0)
arr := values.NewArrayWith(
values.NewInt(0),
)
el, err := arr.GetIn(ctx, []core.Value{key})
elGet := arr.Get(key)
So(err, ShouldBeNil)
So(el.Compare(elGet), ShouldEqual, 0)
})
Convey("Should return None when no items", func() {
key := values.NewInt(0)
arr := values.NewArray(0)
el, err := arr.GetIn(ctx, []core.Value{key})
elGet := arr.Get(key)
So(err, ShouldBeNil)
So(el.Compare(elGet), ShouldEqual, 0)
})
})
Convey("Should error when input is not correct", func() {
Convey("Should error when path[0] is not an int", func() {
arr := values.NewArray(0)
path := []core.Value{values.NewString("")}
el, err := arr.GetIn(ctx, path)
So(err, ShouldBeError)
So(el.Compare(values.None), ShouldEqual, 0)
})
Convey("Should error when first received item is not a Getter and len(path) > 1", func() {
key := values.NewInt(0)
arr := values.NewArrayWith(
values.NewInt(1),
)
path := []core.Value{key, key}
el, err := arr.GetIn(ctx, path)
So(err, ShouldBeError)
So(el.Compare(values.None), ShouldEqual, 0)
})
})
Convey("Should return None when len(path) == 0", func() {
arr := values.NewArrayWith(
values.NewInt(1),
)
el, err := arr.GetIn(ctx, nil)
So(err, ShouldBeNil)
So(el.Compare(values.None), ShouldEqual, 0)
})
Convey("Should call the nested Getter", func() {
key := values.NewInt(0)
arr := values.NewArrayWith(
values.NewObjectWith(
values.NewObjectProperty("foo", key),
),
)
el, err := arr.GetIn(ctx, []core.Value{
key, // obj[0]
values.NewString("foo"), // obj[0].foo
})
So(err, ShouldBeNil)
So(el.Compare(key), ShouldEqual, 0)
})
})
}