forked from padlik/gosugar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
93 lines (78 loc) · 2.02 KB
/
query.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
92
93
package gosugar
import (
"encoding/json"
"errors"
"strings"
)
//Raw json structure representing Filter for JSON module query
type FilterJson struct {
Filter *json.RawMessage `json:"filter"` //filer in JSON
MaxNum int `json:"max_num"` //Number of records to return at one go
Offset int `json:"offset"` //Offset to the next record bunch
Fields string `json:"fields"` //Fields
View string `json:"view"` //internal not used
OrderBy string `json:"order_by"` //order by string
Q string `json:"q"` //internal not used
Deleted bool `json:"deleted"` //show deleted record
}
const returnmax = 1000
const sasc = ":ASC"
const sdesc = ":DESC"
type Sorting struct {
//Lists of fields to sort by
Asc []string
Desc []string
}
type Query struct {
//Module to be queried
Module string
//Filter for module
Filter *QueryFilter
//Fields list. ID will always return
Fields []string
//Order by lists
Sort Sorting
//Query method (default POST)
Method string
//Maximum number of Records
MaxNum int
}
func NewQuery(mod string) (*Query, error) {
if len(mod) == 0 {
return nil, errors.New("Empty module name")
}
q := &Query{Module: mod, Method: "POST", MaxNum: returnmax}
return q, nil
}
func makeSort(s []string, p string) string {
r := ""
for k, _ := range s {
r = r + s[k] + p
if k < len(s)-1 {
r = r + ","
}
}
return r
}
//Implementing valid JSON part for query
func (q *Query) MarshalJSON() ([]byte, error) {
fj := FilterJson{MaxNum: q.MaxNum, Deleted: false, Offset: 0}
if q.Filter != nil {
b, err := json.Marshal(q.Filter)
if err != nil {
return nil, err
}
raw := json.RawMessage(b)
fj.Filter = &raw
}
//we can simple join fields
fj.Fields = strings.Join(q.Fields, ",")
//sorting
sort := []string{makeSort(q.Sort.Asc, sasc), makeSort(q.Sort.Desc, sdesc)}
fj.OrderBy = strings.Trim(strings.Join(sort, ","), ",")
b, err := json.Marshal(fj)
if err != nil {
return nil, err
}
return b, nil
}