forked from si3nloong/go-rsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsql.go
91 lines (78 loc) · 1.63 KB
/
rsql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package rsql
import (
"reflect"
"github.com/timtadh/lexmachine"
)
const (
defaultLimit = uint(50)
// defaultMaxLimit = uint(100)
maxUint = ^uint(0)
)
// FormatFunc :
type FormatFunc func(string) string
// RSQL :
type RSQL struct {
SelectTag string
FilterTag string
SortTag string
LimitTag string
PageTag string
codec *Struct
lexer *lexmachine.Lexer
FormatColumn FormatFunc
DefaultLimit uint
MaxLimit uint
}
// New :
func New(src interface{}) (*RSQL, error) {
v := reflect.ValueOf(src)
codec := getCodec(v.Type())
lexer := lexmachine.NewLexer()
dl := newDefaultTokenLexer()
dl.addActions(lexer)
p := new(RSQL)
p.lexer = lexer
p.FilterTag = "filter"
p.SortTag = "sort"
p.LimitTag = "limit"
p.PageTag = "page"
p.DefaultLimit = defaultLimit
p.codec = codec
return p, nil
}
// MustNew :
func MustNew(src interface{}) *RSQL {
p, err := New(src)
if err != nil {
panic(err)
}
return p
}
// ParseQuery :
func (p *RSQL) ParseQuery(query string) (*Params, error) {
return p.ParseQueryBytes([]byte(query))
}
// ParseQueryBytes :
func (p *RSQL) ParseQueryBytes(query []byte) (*Params, error) {
values := make(map[string]string)
if err := parseRawQuery(values, string(query)); err != nil {
return nil, err
}
var (
params = new(Params)
// errs = make(Errors, 0)
)
if err := p.parseFilter(values, params); err != nil {
return nil, err
}
if err := p.parseSort(values, params); err != nil {
return nil, err
}
if err := p.parseLimit(values, params); err != nil {
return nil, err
}
if err := p.parseOffset(values, params); err != nil {
return nil, err
}
return params, nil
}