Skip to content

Commit

Permalink
add tests, comments and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chilagrow committed Jul 21, 2023
1 parent 1ef1ef9 commit bee2816
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion internal/handlers/common/aggregations/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (e *Expression) Evaluate(doc *types.Document) (any, error) {
}
}

vals, err := commonpath.FindValues(doc, path, commonpath.FindValuesOpts{
vals, err := commonpath.FindValues(doc, path, &commonpath.FindValuesOpts{
IgnoreArrayIndex: true,
IgnoreArrayElement: e.IgnoreArrays,
})
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/common/distinct.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func FilterDistinctValues(iter types.DocumentsIterator, key string) (*types.Arra
}

// vals contains all values exist at the suffix of the path
vals, err := commonpath.FindValues(doc, path, commonpath.FindValuesOpts{})
vals, err := commonpath.FindValues(doc, path, nil)
if err != nil {
return nil, lazyerrors.Error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/common/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func filterDocumentPair(doc *types.Document, filterKey string, filterValue any)
}
} else {
filterSuffix = path.Suffix()
vals, err = commonpath.FindValues(doc, path, commonpath.FindValuesOpts{})
vals, err = commonpath.FindValues(doc, path, nil)
if err != nil {
return false, lazyerrors.Error(err)
}
Expand Down
6 changes: 5 additions & 1 deletion internal/handlers/commonpath/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ type FindValuesOpts struct {
// - if the array contains one or more documents which have the key.
//
// It returns a slice of values and an empty array is returned if no value was found.
func FindValues(doc *types.Document, path types.Path, opts FindValuesOpts) ([]any, error) {
func FindValues(doc *types.Document, path types.Path, opts *FindValuesOpts) ([]any, error) {
if opts == nil {
opts = new(FindValuesOpts)
}

keys := path.Slice()
inputs := []any{doc}
var values []any
Expand Down
44 changes: 25 additions & 19 deletions internal/handlers/commonpath/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,63 +26,69 @@ import (
func TestGetPathValue(t *testing.T) {
t.Parallel()

emptyDoc := new(types.Document)
docDoc := must.NotFail(types.NewDocument("foo", must.NotFail(types.NewDocument("bar", 1))))
docArrayOne := must.NotFail(types.NewDocument("foo", must.NotFail(types.NewArray(
empty := new(types.Document)
doc := must.NotFail(types.NewDocument("foo", must.NotFail(types.NewDocument("bar", 1))))
arrayDocOne := must.NotFail(types.NewDocument("foo", must.NotFail(types.NewArray(
must.NotFail(types.NewDocument("bar", 1)),
))))
docArrayTwo := must.NotFail(types.NewDocument("foo", must.NotFail(types.NewArray(
arrayDocTwo := must.NotFail(types.NewDocument("foo", must.NotFail(types.NewArray(
must.NotFail(types.NewDocument("bar", 1)),
must.NotFail(types.NewDocument("bar", 2)),
))))
arrayScalarThree := must.NotFail(types.NewDocument("foo", must.NotFail(types.NewArray(0, 1, 2))))

for name, tc := range map[string]struct {
res []any
doc *types.Document
path types.Path
opts FindValuesOpts
opts *FindValuesOpts
res []any
}{
"EmptyDocument": {
doc: emptyDoc,
"Empty": {
doc: empty,
path: types.NewStaticPath("foo", "bar"),
res: []any{},
},
"Document": {
doc: docDoc,
doc: doc,
path: types.NewStaticPath("foo"),
res: []any{must.NotFail(types.NewDocument("bar", 1))},
},
"DocumentDotNotation": {
doc: docDoc,
doc: doc,
path: types.NewStaticPath("foo", "bar"),
res: []any{1},
},
"ArrayIndex": {
doc: docArrayOne,
"ArrayIndexDoc": {
doc: arrayDocOne,
path: types.NewStaticPath("foo", "0", "bar"),
res: []any{1},
},
"ArrayIndexScalar": {
doc: arrayScalarThree,
path: types.NewStaticPath("foo", "1"),
res: []any{1},
},
"ArrayDocument": {
doc: docArrayOne,
doc: arrayDocOne,
path: types.NewStaticPath("foo", "bar"),
res: []any{1},
},
"ArrayDocumentTwo": {
doc: docArrayTwo,
doc: arrayDocTwo,
path: types.NewStaticPath("foo", "bar"),
opts: FindValuesOpts{IgnoreArrayIndex: true},
opts: &FindValuesOpts{IgnoreArrayIndex: true},
res: []any{1, 2},
},
"IgnoreArrayIndex": {
doc: docArrayOne,
doc: arrayDocOne,
path: types.NewStaticPath("foo", "0", "bar"),
opts: FindValuesOpts{IgnoreArrayIndex: true},
opts: &FindValuesOpts{IgnoreArrayIndex: true},
res: []any{},
},
"IgnoreArrayElement": {
doc: docArrayOne,
doc: arrayDocOne,
path: types.NewStaticPath("foo", "bar"),
opts: FindValuesOpts{IgnoreArrayElement: true},
opts: &FindValuesOpts{IgnoreArrayElement: true},
res: []any{},
},
} {
Expand Down

0 comments on commit bee2816

Please sign in to comment.