Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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 main

import 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}
```