Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/benpate/exp
Simple Expression Builder for Go
https://github.com/benpate/exp
Last synced: about 2 months ago
JSON representation
Simple Expression Builder for Go
- Host: GitHub
- URL: https://github.com/benpate/exp
- Owner: benpate
- License: apache-2.0
- Created: 2021-04-04T15:58:39.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-14T18:16:14.000Z (4 months ago)
- Last Synced: 2024-10-28T20:49:23.141Z (3 months ago)
- Language: Go
- Size: 58.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Expressions 🤩
[![GoDoc](https://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://pkg.go.dev/github.com/benpate/exp)
[![Version](https://img.shields.io/github/v/release/benpate/exp?include_prereleases&style=flat-square&color=brightgreen)](https://github.com/benpate/exp/releases)
[![Build Status](https://img.shields.io/github/actions/workflow/status/benpate/exp/go.yml?branch=main)](https://github.com/benpate/exp/actions/workflows/go.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/benpate/exp?style=flat-square)](https://goreportcard.com/report/github.com/benpate/exp)
[![Codecov](https://img.shields.io/codecov/c/github/benpate/exp.svg?style=flat-square)](https://codecov.io/gh/benpate/exp)## Intermediate Expression Formats for the Masses
Every database has its own query language, so this library provides in intermediate format that should be easy to convert into whatever specific language you need to use.
The expression library only represents the structure of the logical expression, and does not include implementations for any data sources. Those should be implemented in each individual data source adapter library.
```go
// build single predicate expressions
criteria := exp.Equal("_id", 42)// use chaining for logical constructs
criteria := exp.Equal("_id", 42).AndEqual("deleteDate", 0)
criteria := exp.Equal("name", "John").OrEqual("name", "Sarah")// Also supports complex and/or logic
criteria := exp.Or(
exp.Equal("_id", 42).AndEqual("deleteDate", 0),
exp.Equal("_id", 42).AndEqual("name", "Sarah"),
)// Constants define standard expected operators
data.OperatorEqual = "="
data.OperatorNotEqual = "!="
data.OperatorLessThan = "<"
data.OperatorLessOrEqual = "<="
data.OperatorGreaterThan = ">"
data.OperatorGreaterOrEqual = ">="
```## Interfaces
This is accomplished with three very similar data types that all implement the same `Expression` interface.
**`Predicate`** represents a single predicate/comparison. Using `.And()` and `.Or()` will return the corresponding `AndExpression` or `OrExpression` object
**`AndExpression`** represents multiple predicates, all chained together with AND logic. Only supports the `.And()` method for additional predicates
**`OrExpression`** represents multiple predicates, all chained together with OR logic. Only supports the `.Or()` method for additional predicates.
## Manually Walking the Logic Tree
Each of the three interfaces above implements a `.Match()` function that can be used by external programs to see if a dataset matches this exp. You must pass in a `MatcherFunc` that accepts a predicate and returns `TRUE` if that predicate matches the dataaset. `AndExpression` and `OrExpression` objects will call this function repeatedly for each element in their logic tree, and return a final boolean value for the entire logic structure.
## Pull Requests Welcome
This library is a work in progress, and will benefit from your experience reports, use cases, and contributions. If you have an idea for making this library better, send in a pull request. We're all in this together! 🤩