Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/suyashkumar/filter
Easily filter an arbitrary slice of structs based on certain configurable criteria determined at runtime
https://github.com/suyashkumar/filter
array dynamic-constraints filter filtering golang golang-library golang-package golang-tools golang-util slice
Last synced: 21 days ago
JSON representation
Easily filter an arbitrary slice of structs based on certain configurable criteria determined at runtime
- Host: GitHub
- URL: https://github.com/suyashkumar/filter
- Owner: suyashkumar
- License: mit
- Created: 2017-07-21T06:55:41.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-07-23T00:35:32.000Z (over 7 years ago)
- Last Synced: 2024-10-08T01:41:28.266Z (about 1 month ago)
- Topics: array, dynamic-constraints, filter, filtering, golang, golang-library, golang-package, golang-tools, golang-util, slice
- Language: Go
- Homepage:
- Size: 3.91 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# filter
Filter provides the ability to quickly and easily filter an arbitrary slice of structs based on certain dynamic constraints **at runtime**. This makes it simple to ask questions like: "Give me all `Thing`s where A="HI" and B=2" where the actual constraints themselves are dynamic and determined at runtime.Though there are limited compile time safety checks (due to the need to use `interface{}`), there are extensive runtime checks in an attempt to ensure programs don't crash or do bad things (you'll get `error`s returned to you if you attempt to use this package incorrectly).
If you do not need dynamic constraints at runtime, or do not need a general filtering function, you may wish to implement someting more specific and perhaps better for your use.
This project is still under active development. Contributions are welcome--reach out if you'd like to do so!
Short Example:
```go
package mainimport (
"fmt"
"github.com/suyashkumar/filter"
)type Thing struct {
A string
B int64
}func main() {
// Thing Array
in := []Thing{
Thing{A: "HI", B: 1},
Thing{A: "HI2", B: 2},
}cons, _ := filter.NewConstraints(Thing{}) // Creates a new Constraints for the Thing struct
cons.Add("A", "HI") // Add a constraint to filter on Thing.A = "HI"out, _ := filter.Filter(in, cons) // Filter
// Map out []interface{} into your desired type
outArr := make([]Thing, len(out))
for i := 0; i < len(out); i++ {
if out[i] == nil {
continue
}
outArr[i] = out[i].(Thing)
}fmt.Println(outArr) // [{HI 1} { 0}]
}
```