forked from sourcenetwork/defradb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Inline array filters (sourcenetwork#3028)
## Relevant issue(s) Resolves sourcenetwork#2857 ## Description This PR adds three new filter types for inline array types: `_all`, `_any`, and `_none`. ## Tasks - [x] I made sure the code is well commented, particularly hard-to-understand areas. - [x] I made sure the repository-held documentation is changed accordingly. - [x] I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in [tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)). - [x] I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ... ## How has this been tested? Added integration tests Specify the platform(s) on which this was tested: - MacOS
- Loading branch information
Showing
17 changed files
with
1,467 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package connor | ||
|
||
import ( | ||
"github.com/sourcenetwork/defradb/client" | ||
|
||
"github.com/sourcenetwork/immutable" | ||
) | ||
|
||
// all is an operator which allows the evaluation of | ||
// a number of conditions over a list of values | ||
// matching if all of them match. | ||
func all(condition, data any) (bool, error) { | ||
switch t := data.(type) { | ||
case []string: | ||
return allSlice(condition, t) | ||
|
||
case []immutable.Option[string]: | ||
return allSlice(condition, t) | ||
|
||
case []int64: | ||
return allSlice(condition, t) | ||
|
||
case []immutable.Option[int64]: | ||
return allSlice(condition, t) | ||
|
||
case []bool: | ||
return allSlice(condition, t) | ||
|
||
case []immutable.Option[bool]: | ||
return allSlice(condition, t) | ||
|
||
case []float64: | ||
return allSlice(condition, t) | ||
|
||
case []immutable.Option[float64]: | ||
return allSlice(condition, t) | ||
|
||
default: | ||
return false, client.NewErrUnhandledType("data", data) | ||
} | ||
} | ||
|
||
func allSlice[T any](condition any, data []T) (bool, error) { | ||
for _, c := range data { | ||
m, err := eq(condition, c) | ||
if err != nil { | ||
return false, err | ||
} else if !m { | ||
return false, nil | ||
} | ||
} | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package connor | ||
|
||
import ( | ||
"github.com/sourcenetwork/defradb/client" | ||
|
||
"github.com/sourcenetwork/immutable" | ||
) | ||
|
||
// anyOp is an operator which allows the evaluation of | ||
// a number of conditions over a list of values | ||
// matching if any of them match. | ||
func anyOp(condition, data any) (bool, error) { | ||
switch t := data.(type) { | ||
case []string: | ||
return anySlice(condition, t) | ||
|
||
case []immutable.Option[string]: | ||
return anySlice(condition, t) | ||
|
||
case []int64: | ||
return anySlice(condition, t) | ||
|
||
case []immutable.Option[int64]: | ||
return anySlice(condition, t) | ||
|
||
case []bool: | ||
return anySlice(condition, t) | ||
|
||
case []immutable.Option[bool]: | ||
return anySlice(condition, t) | ||
|
||
case []float64: | ||
return anySlice(condition, t) | ||
|
||
case []immutable.Option[float64]: | ||
return anySlice(condition, t) | ||
|
||
default: | ||
return false, client.NewErrUnhandledType("data", data) | ||
} | ||
} | ||
|
||
func anySlice[T any](condition any, data []T) (bool, error) { | ||
for _, c := range data { | ||
m, err := eq(condition, c) | ||
if err != nil { | ||
return false, err | ||
} else if m { | ||
return true, nil | ||
} | ||
} | ||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package connor | ||
|
||
// none is an operator which allows the evaluation of | ||
// a number of conditions over a list of values | ||
// matching if all of them do not match. | ||
func none(condition, data any) (bool, error) { | ||
m, err := anyOp(condition, data) | ||
if err != nil { | ||
return false, err | ||
} | ||
return !m, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.