Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ganigeorgiev/fexpr
Simple filter query language parser so that you can build SQL, Elasticsearch, etc. queries safely from user input.
https://github.com/ganigeorgiev/fexpr
dsl filter-parser parser query
Last synced: about 3 hours ago
JSON representation
Simple filter query language parser so that you can build SQL, Elasticsearch, etc. queries safely from user input.
- Host: GitHub
- URL: https://github.com/ganigeorgiev/fexpr
- Owner: ganigeorgiev
- License: bsd-3-clause
- Created: 2022-01-22T17:29:26.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-06-03T05:28:09.000Z (7 months ago)
- Last Synced: 2025-01-01T03:08:58.149Z (7 days ago)
- Topics: dsl, filter-parser, parser, query
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 112
- Watchers: 4
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
fexpr
[![Go Report Card](https://goreportcard.com/badge/github.com/ganigeorgiev/fexpr)](https://goreportcard.com/report/github.com/ganigeorgiev/fexpr)
[![GoDoc](https://godoc.org/github.com/ganigeorgiev/fexpr?status.svg)](https://pkg.go.dev/github.com/ganigeorgiev/fexpr)
================================================================================**fexpr** is a filter query language parser that generates easy to work with AST structure so that you can create safely SQL, Elasticsearch, etc. queries from user input.
Or in other words, transform the string `"id > 1"` into the struct `[{&& {{identifier id} > {number 1}}}]`.
Supports parenthesis and various conditional expression operators (see [Grammar](https://github.com/ganigeorgiev/fexpr#grammar)).
## Example usage
```
go get github.com/ganigeorgiev/fexpr
``````go
package mainimport github.com/ganigeorgiev/fexpr
func main() {
result, err := fexpr.Parse("id=123 && status='active'")
// result: [{&& {{identifier id} = {number 123}}} {&& {{identifier status} = {text active}}}]
}
```> Note that each parsed expression statement contains a join/union operator (`&&` or `||`) so that the result can be consumed on small chunks without having to rely on the group/nesting context.
> See the [package documentation](https://pkg.go.dev/github.com/ganigeorgiev/fexpr) for more details and examples.
## Grammar
**fexpr** grammar resembles the SQL `WHERE` expression syntax. It recognizes several token types (identifiers, numbers, quoted text, expression operators, whitespaces, etc.).
> You could find all supported tokens in [`scanner.go`](https://github.com/ganigeorgiev/fexpr/blob/master/scanner.go).
#### Operators
- **`=`** Equal operator (eg. `a=b`)
- **`!=`** NOT Equal operator (eg. `a!=b`)
- **`>`** Greater than operator (eg. `a>b`)
- **`>=`** Greater than or equal operator (eg. `a>=b`)
- **`<`** Less than or equal operator (eg. `a`** Array/Any Greater than operator (eg. `a?>b`)
- **`?>=`** Array/Any Greater than or equal operator (eg. `a?>=b`)
- **`?<`** Array/Any Less than or equal operator (eg. `a? 123"))// scan single token at a time until EOF or error is reached
for {
t, err := s.Scan()
if t.Type == fexpr.TokenEOF || err != nil {
break
}fmt.Println(t)
}// Output:
// {identifier id}
// {whitespace }
// {sign >}
// {whitespace }
// {number 123}
```