querybuilder

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

README

Query Builder

This package is a Golang Rule Evaluator for jQuery QueryBuilder

You will use the Evaluator to check if a dataset matches the JSON rules produced by the jQuery QueryBuilder plugin.

Dataset is a map of the type map[string]interface{}

You can access nested fields with a dot notation. e.g. fields.Question to access {'fields' => {'Question' => 'Answer'}}

Example usage


var rulesetJSON = `{
  "condition": "AND",
  "rules": [
    {"id": "name", "field": "name", "type": "string", "input": "text", "operator": "begins_with", "value": "shirt"},
    {"id": "category", "field": "category", "type": "string", "input": "text", "operator": "equal", "value": "woman"},
    {"condition": "OR","rules": [
      {"id": "price", "field": "price", "type": "double", "input": "text", "operator": "greater","value": 10.50},
      {"id": "published_at", "field": "published_at", "type": "date", "input": "text", "operator": "less", "value": "2019-12-25"}
      ]}
    ]}`

var datasetJSON = `{"name": "short sleeve shirt", "category": "woman", "price":  33.0, "published_at": "2020-1-1"}`

func main(){
  var ruleset map[string]interface{}
  var dataset map[string]interface{}
  json.Unmarshal([]byte(rulesetJSON), &ruleset)
  json.Unmarshal([]byte(datasetJSON), &dataset)
  
  qb := querybuilder.New(ruleset)
  if qb.Match(dataset) {
    fmt.Println("√ dataset is valid!")
  }
}

Operators

All the default operators have been implemented, see in /operators folder

You can easily add custom operators, see:

import "github.com/enjoei/pkg/querybuilder/operator"

func init() {
	AddOperator(EqualsFive)
}

var EqualsFive = &Operator{
	Name: "equals_five",
	Evaluate: func(input, value interface{}) bool {
		return input(int) == 5
	},
}

The Evaluate function should receive a input and value params, even if a value is not required

Contributing

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

Documentation

Index

Constants

View Source
const (
	AND = "AND"
	OR  = "OR"
)
View Source
const (
	DATE_ISO_8601      = "2006-01-02"
	TIME_ISO_8601      = "15:04:05"
	DATE_TIME_ISO_8601 = "2006-01-02T15:04:05"
)
View Source
const NoSymbolsPattern = "[^a-zA-Z0-9]+"

Variables

This section is empty.

Functions

This section is empty.

Types

type Checker

type Checker interface {
	Evaluate(dataset map[string]interface{}) bool
}

type Evaluator

type Evaluator struct {
	Ruleset map[string]interface{}
}

func New

func New(ruleset map[string]interface{}) *Evaluator

func (*Evaluator) Match

func (e *Evaluator) Match(dataset map[string]interface{}) bool

type Rule

type Rule struct {
	ID           string
	Field        string
	Type         string
	Input        string
	Operator     string
	Sanitize     bool
	RemoveAccent bool
	Value        interface{}
}

func (*Rule) Evaluate

func (r *Rule) Evaluate(dataset map[string]interface{}) bool

Evaluate function checks whether the dataset matches with rule

type RuleGroup

type RuleGroup struct {
	Condition interface{}
	Rules     interface{}
}

func (*RuleGroup) Evaluate

func (rg *RuleGroup) Evaluate(dataset map[string]interface{}) bool

Directories

Path Synopsis
Operator package contains the operators used in rules Reference: https://golang.org/ref/spec#Comparison_operators
Operator package contains the operators used in rules Reference: https://golang.org/ref/spec#Comparison_operators

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL