forked from Stratoscale/swagger
/
config.go
91 lines (84 loc) · 2.76 KB
/
config.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 query
import "errors"
const (
// fields in the struct tag.
sortTag = "sort"
splitTag = "split"
filterTag = "filter"
paramTag = "param"
detailedTag = "detailed"
// search param in query string.
searchParam = "search"
// operators in query string.
opEqual = "eq"
opNotEqual = "neq"
opLike = "like"
opLessThan = "lt"
opGreaterThan = "gt"
opLessThanOrEqual = "lte"
opGreaterThanOrEqual = "gte"
)
// An expression can be optionally prefixed with + or - to control the sorting direction,
// ascending or descending. For example, '+field' or '-field'.
// If the predicate is missing or empty then it defaults to '+'
var sortDirections = map[byte]string{'+': "asc", '-': "desc"}
// Config for the Builder constructor.
type Config struct {
// Model is an instance of the struct definition. the Builder will parse
// the url.Values according to this.
Model interface{}
// TagName is the name of the tag in the struct. defaults to "query".
TagName string
// Separator between field and command. defaults to "_".
Separator string
// IgnoreSort indicates if the builder should skip the sort process.
IgnoreSort bool
// SortParam is the name of the sort parameter.
// defaults to "sort"
SortParam string
// DefaultSort is the default sort string for the query builder.
// if the builder gets and empty sort parameter it'll add this default.
DefaultSort string
// LimitParam is the name of the limit parameter in the query string.
// defaults to "limit".
LimitParam string
// DefaultLimit is the default value for limit option. default to 25.
DefaultLimit int
// LimitMaxValue is the maximum value that accept valid parameter.
LimitMaxValue int
// OffsetParam is the name of the offset parameter in the query string.
// defaults to "offset"
OffsetParam string
// SearchOperator used to combine search condition together. defaults to "AND".
SearchOperator string
// ExplicitSelect - if true, the query will select the relevant specific columns.
// else will select '*'
ExplicitSelect bool
// OnlySelectNonDetailedFields - if true will select only the non 'detailed' fields
// true implies ExplicitSelect = true
OnlySelectNonDetailedFields bool
}
func (c *Config) defaults() error {
if c.Model == nil {
return errors.New("query: 'Model' is a required field")
}
defaultString(&c.TagName, "query")
defaultString(&c.Separator, "_")
defaultString(&c.SortParam, "sort")
defaultString(&c.LimitParam, "limit")
defaultString(&c.OffsetParam, "offset")
defaultString(&c.SearchOperator, "AND")
defaultInt(&c.DefaultLimit, 25)
defaultInt(&c.LimitMaxValue, 100)
return nil
}
func defaultString(s *string, v string) {
if *s == "" {
*s = v
}
}
func defaultInt(i *int, v int) {
if *i == 0 {
*i = v
}
}