Skip to content

Commit 36aff92

Browse files
author
timsolov
committed
improve documentation
1 parent 423786c commit 36aff92

File tree

6 files changed

+134
-108
lines changed

6 files changed

+134
-108
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Query Parser for REST
22
Query Parser is a library for easy building dynamic SQL queries to Database. It provides a simple API for web-applications which needs to do some filtering throught GET queries. It is a connector between the HTTP handler and the DB engine, and manages validations and translations for user inputs.
33

4+
[![GoDoc](https://godoc.org/github.com/timsolov/rest-query-parser?status.png)](https://godoc.org/github.com/timsolov/rest-query-parser)
5+
46
## Installation
57
go get -u github.com/timsolov/rest-query-parser
68

@@ -50,9 +52,9 @@ See cmd/main.go and tests for more examples.
5052
```
5153

5254
## Top level fields:
53-
* `fields` - fields for SELECT clause separated by comma (",") Eg. `&fields=id,name`. If nothing provided use "\*" by default. Attention! If you want to use this filter you have to defaine validation func for it. Use `rqp.In()` func for limit fields for your query.
55+
* `fields` - fields for SELECT clause separated by comma (",") Eg. `&fields=id,name`. If nothing provided will use "\*" by default. Attention! If you want to use this filter you have to define validation func for it. Use `rqp.In("id", "name")` func for limit fields for your query.
5456
* `sort` - sorting fields list separated by comma (","). Must be validated too. Could include prefix +/- which means ASC/DESC sorting. Eg. `&sort=+id,-name` will print `ORDER BY id, name DESC`. You have to filter fields in this parameter by adding `rqp.In("id", "name")`.
55-
* `limit` - is limit for LIMIT clause. Should be greater then 0 by default. Definition of the validation for `limit` is not required.
57+
* `limit` - is limit for LIMIT clause. Should be greater then 0 by default. Definition of the validation for `limit` is not required. But you may use `rqp.Max(100)` to limit top threshold.
5658
* `offset` - is offset for OFFSET clause. Should be greater then or equal to 0 by default. Definition of the validation for `offset` is not required.
5759

5860
## Validation modificators:
@@ -61,7 +63,7 @@ See cmd/main.go and tests for more examples.
6163
* `:bool` - parameter must be convertable to bool type. Raise error if not.
6264

6365
## Supported types
64-
- `strings` - the default type for all provided filters if not specified another. Could be compared by `eq, ne, like, ilkie, in` methods.
66+
- `string` - the default type for all provided filters if not specified another. Could be compared by `eq, ne, like, ilike, in` methods.
6567
- `int` - integer type. Must be specified with tag ":int". Could be compared by `eq, ne, gt, lt, gte, lte, in` methods.
6668
- `bool` - boolean type. Must be specified with tag ":bool". Could be compared by `eq` method.
6769

errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package rqp
22

3+
// Error special rqp.Error type
34
type Error struct {
45
s string
56
}
@@ -8,10 +9,12 @@ func (e *Error) Error() string {
89
return e.s
910
}
1011

12+
// NewError constructor for internal errors
1113
func NewError(msg string) *Error {
1214
return &Error{msg}
1315
}
1416

17+
// Errors list:
1518
var (
1619
ErrRequired = NewError("required")
1720
ErrBadFormat = NewError("bad format")

filter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (f *Filter) parseKey(key string) error {
142142

143143
if epos-spos > 0 {
144144
f.Method = Method(strings.ToUpper(string(key[spos:epos])))
145-
if _, ok := TranslateMethods[f.Method]; !ok {
145+
if _, ok := translateMethods[f.Method]; !ok {
146146
return ErrUnknownMethod
147147
}
148148
}
@@ -192,16 +192,16 @@ func (f *Filter) Where() (string, error) {
192192

193193
switch f.Method {
194194
case EQ, NE, GT, LT, GTE, LTE, LIKE, ILIKE:
195-
exp = fmt.Sprintf("%s %s ?", f.Name, TranslateMethods[f.Method])
195+
exp = fmt.Sprintf("%s %s ?", f.Name, translateMethods[f.Method])
196196
return exp, nil
197197
case NOT:
198198
if f.Value == NULL {
199-
exp = fmt.Sprintf("%s %s NULL", f.Name, TranslateMethods[f.Method])
199+
exp = fmt.Sprintf("%s %s NULL", f.Name, translateMethods[f.Method])
200200
return exp, nil
201201
}
202202
return exp, ErrUnknownMethod
203203
case IN:
204-
exp = fmt.Sprintf("%s %s (?)", f.Name, TranslateMethods[f.Method])
204+
exp = fmt.Sprintf("%s %s (?)", f.Name, translateMethods[f.Method])
205205
exp, _, _ = in(exp, f.Value)
206206
return exp, nil
207207
default:

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/timsolov/rest-query-parser
33
go 1.13
44

55
require (
6-
github.com/jmoiron/sqlx v1.2.0 // indirect
76
github.com/pkg/errors v0.9.1
8-
github.com/stretchr/testify v1.4.0
7+
github.com/stretchr/testify v1.5.1
98
)

go.sum

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
4-
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
5-
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
6-
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
7-
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
83
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
94
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
105
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
116
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
127
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
138
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
149
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
10+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
11+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
1512
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1613
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1714
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=

0 commit comments

Comments
 (0)