Simple filter query language parser so that you can build SQL, Elasticsearch, etc. queries safely from user input.

fexpr Go Report Card GoDoc

fexpr is a filter query language parser that generates extremely 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).

Example usage

go get github.com/ganigeorgiev/fexpr
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 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.

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)
  • <= Less than or equal operator (eg. a<=b)
  • ~ Like/Contains operator (eg. a~b)
  • !~ NOT Like/Contains operator (eg. a!~b)
  • && AND join operator (eg. a=b && c=d)
  • || OR join operator (eg. a=b || c=d)
  • () Parenthesis (eg. (a=1 && b=2) || (a=3 && b=4))

Numbers

Number tokens are any integer or decimal numbers. Example: 123, 10.50.

Identifiers

Identifier tokens are literals that start with a letter, _, @ or # and could contain further any number of digits or . (usually used as a separator). Example: id, a.b.c, @request.method, field2.

Quoted text

Text tokens are any literals that are wrapped by ' or " quotes. Example: 'Lorem ipsum dolor 123!', "escaped \"word\"", "mixed 'quotes' are fine".

Using only the scanner

The tokenizer (aka. fexpr.Scanner) could be used without the parser's state machine so that you can write your own custom tokens processing:

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}">
s := fexpr.NewScanner(strings.NewReader("id > 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}
Similar Resources

A simple (yet effective) GraphQL to HTTP / REST router

ReGraphQL A simple (yet effective) GraphQL to REST / HTTP router. ReGraphQL helps you expose your GraphQL queries / mutations as REST / HTTP endpoints

Dec 12, 2022

QueryCSV enables you to load CSV files and manipulate them using SQL queries then after you finish you can export the new values to a CSV file

QueryCSV enables you to load CSV files and manipulate them using SQL queries then after you finish you can export the new values to a CSV file

QueryCSV enable you to load CSV files and manipulate them using SQL queries then after you finish you can export the new values to CSV file

Dec 22, 2021

A simple javascript website that takes user input, queries a Go based backend which then creates ascii art and sends it back to the frontend

A simple javascript website that takes user input, queries a Go based backend which then creates ascii art and sends it back to the frontend. Finally the site displays the ascii art and offers the option to download as multiple file types.

Jan 7, 2022

Caddy log filter module with a log field filter to extract the user from a basic Authorization HTTP-Header

caddy-basic-auth-filter This packages contains a log field filter to extract the user from a basic Authorization HTTP-Header. Installation xcaddy buil

May 10, 2022

CLI tool that can execute SQL queries on CSV, LTSV, JSON and TBLN. Can output to various formats.

CLI tool that can execute SQL queries on CSV, LTSV, JSON and TBLN. Can output to various formats.

trdsql CLI tool that can execute SQL queries on CSV, LTSV, JSON and TBLN. It is a tool like q, textql and others. The difference from these tools is t

Jan 1, 2023

A simple parser for the query used in gmail to filter for e-mails

Go Gmail Query Parser by Dustin Breuer This project is work in progress. Prerequisites Before installing this project you need: 🐀 Go (at least 1.17)

Feb 2, 2022

👷 Library for safely running groups of workers concurrently or consecutively that require input and output through channels

👷 Library for safely running groups of workers concurrently or consecutively that require input and output through channels

Examples Quickstart Multiple Go Workers Passing Fields Getting Started Pull in the dependency go get github.com/catmullet/go-workers Add the import to

Dec 1, 2022

This is old and unmaintained code, ignore it. starfish is a simple, SDL based, 2D graphics and user input library for Go. If you intend to work on it, please fork from the 'devel' branch, not 'master'. Current release: 0.12.0

What is starfish? What starfish is: starfish is a simple 2D graphics and user input library for Go built on SDL. What starfish is not: While it is bui

Jun 4, 2019

Map, Reduce, Filter, De/Multiplex, etc. for the Go language.

proto proto gives Go operations like Map, Reduce, Filter, De/Multiplex, etc. without sacrificing idiomatic harmony or speed. It also introduces a conv

Nov 17, 2022

write APIs using direct SQL queries with no hassle, let's rethink about SQL

SQLer SQL-er is a tiny portable server enables you to write APIs using SQL query to be executed when anyone hits it, also it enables you to define val

Jan 7, 2023

Go fearless SQL. Sqlvet performs static analysis on raw SQL queries in your Go code base.

Sqlvet Sqlvet performs static analysis on raw SQL queries in your Go code base to surface potential runtime errors at build time. Feature highlights:

Dec 19, 2022

"Go SQL DB" is a relational database that supports SQL queries for research purposes

A pure golang SQL database for database theory research

Jan 6, 2023

Parses a file and associate SQL queries to a map. Useful for separating SQL from code logic

goyesql This package is based on nleof/goyesql but is not compatible with it any more. This package introduces support for arbitrary tag types and cha

Oct 20, 2021

PHP functions implementation to Golang. This package is for the Go beginners who have developed PHP code before. You can use PHP like functions in your app, module etc. when you add this module to your project.

PHP Functions for Golang - phpfuncs PHP functions implementation to Golang. This package is for the Go beginners who have developed PHP code before. Y

Dec 30, 2022

GitHub CLI extension to clone (or update) all repositories in an Organization, with the ability to filter via search queries.

gh-org-repo-sync GitHub CLI extension to clone all repositories in an Organization, with the ability to filter via search queries. If a local clone al

Nov 2, 2022

go-xss is a module used to filter input from users to prevent XSS attacks

go-xss 根据白名单过滤 HTML(防止 XSS 攻击) go-xss is a module used to filter input from users to prevent XSS attacks go-xss是一个用于对用户输入的内容进行过滤,以避免遭受 XSS 攻击的模块

Nov 3, 2022

A soothing face filter where you can appreciate the beauty but not fully identify the person.

A soothing face filter where you can appreciate the beauty but not fully identify the person.

⭐ the project to show your appreciation. ↗️ Showerglass A soothing face filter where you can appreciate the beauty but not fully identify the person.

Oct 26, 2022

A Go Application helps you save your contacts on cloud safely

Contact Saver This Application helps you save your contacts on cloud safely. The backend is built with Go programming language and the front end with

Nov 10, 2021
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.

GraphJin - Build APIs in 5 minutes GraphJin gives you a high performance GraphQL API without you having to write any code. GraphQL is automagically co

Jan 1, 2023
Resource Query Language for REST
Resource Query Language for REST

RQL is a resource query language for REST. It provides a simple and light-weight API for adding dynamic querying capabilities to web-applications that

Jan 2, 2023
GSQL is a structured query language code builder for golang.

GSQL is a structured query language code builder for golang.

Dec 12, 2022
A simple Go package to Query over JSON/YAML/XML/CSV Data
A simple Go package to Query over JSON/YAML/XML/CSV Data

A simple Go package to Query over JSON Data. It provides simple, elegant and fast ODM like API to access, query JSON document Installation Install the

Jan 8, 2023
Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.
Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.

dasel Dasel (short for data-selector) allows you to query and modify data structures using selector strings. Comparable to jq / yq, but supports JSON,

Jan 2, 2023
JSON query expression library in Golang.

jsonql JSON query expression library in Golang. This library enables query against JSON. Currently supported operators are: (precedences from low to h

Dec 31, 2022
graphql parser + utilities

graphql utilities for dealing with GraphQL queries in Go. This package focuses on actually creating GraphQL servers and expects you to describe your s

Dec 20, 2022
GraphQL parser comparison in different languages

graphql-parser-bench Parsing a schema or document can be a critical part of the application, especially if you have to care about latency. This is the

Jul 10, 2022
A simple Go, GraphQL, and PostgreSQL starter template

Simple Go/GraphQL/PostgreSQL template Purpose Have a good starting point for any project that needs a graphql, go, and postgres backend. It's a very l

Jan 8, 2022
A simple 2D demo in go using embiten game engine
A simple 2D demo in go using embiten game engine

a little demo in go using the ebiten game engine vaguely follows my memory of ho

Dec 29, 2021