Skip to content

Commit 6ce66cd

Browse files
update querybuilder readme
1 parent 5cba9ee commit 6ce66cd

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

querybuilder/README.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,65 @@
1-
# querybuilder
2-
Package for jQuery Query Builder Rule Evaluator
1+
# Query Builder
2+
This package is a Golang Rule Evaluator for [jQuery QueryBuilder](https://querybuilder.js.org/index.html)
3+
4+
You will use the Evaluator to check if a dataset matches the JSON rules produced by the jQuery QueryBuilder plugin.
5+
Dataset is a map of the type `map[string]interface{}`
6+
You can access nested fields with a dot notation. e.g. fields.Question to access {'fields' => {'Question' => 'Answer'}}
7+
8+
## Example usage
9+
10+
```golang
11+
12+
var rulesetJSON = `{
13+
"condition": "AND",
14+
"rules": [
15+
{"id": "name", "field": "name", "type": "string", "input": "text", "operator": "begins_with", "value": "shirt"},
16+
{"id": "category", "field": "category", "type": "string", "input": "text", "operator": "equal", "value": "woman"},
17+
{"condition": "OR","rules": [
18+
{"id": "price", "field": "price", "type": "double", "input": "text", "operator": "greater","value": 10.50},
19+
{"id": "published_at", "field": "published_at", "type": "date", "input": "text", "operator": "less", "value": "2019-12-25"}
20+
]}
21+
]}`
22+
23+
var datasetJSON = `{"name": "short sleeve shirt", "category": "woman", "price": 33.0, "published_at": "2020-1-1"}`
24+
25+
func main(){
26+
var ruleset map[string]interface{}
27+
var dataset map[string]interface{}
28+
json.Unmarshal([]byte(rulesetJSON), &ruleset)
29+
json.Unmarshal([]byte(datasetJSON), &dataset)
30+
31+
qb := querybuilder.New(ruleset)
32+
if qb.Match(dataset) {
33+
fmt.Println("√ dataset is valid!")
34+
}
35+
}
36+
37+
```
38+
39+
## Operators
40+
All the default operators have been implemented, see in `/operators` folder
41+
You can easily add custom operators, see:
42+
43+
```golang
44+
import "github.com/enjoei/pkg/querybuilder/operator"
45+
46+
func init() {
47+
AddOperator(EqualsFive)
48+
}
49+
50+
// Equal
51+
var EqualsFive = &Operator{
52+
Name: "equals_five",
53+
Evaluate: func(input, value interface{}) bool {
54+
return input(int) == 5
55+
},
56+
}
57+
```
58+
The Evaluate function shoult receive a input and value params, even if a value is not required
59+
60+
## Contributing
61+
- Fork it
62+
- Create your feature branch (`git checkout -b my-new-feature`)
63+
- Commit your changes (`git commit -am 'Add some feature'`)
64+
- Push to the branch (`git push origin my-new-feature`)
65+
- Create new Pull Request

0 commit comments

Comments
 (0)