-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.go
88 lines (76 loc) · 2.31 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
package scopes
// #include <stdlib.h>
// #include "shim.h"
import "C"
import (
"encoding/json"
"runtime"
"unsafe"
)
// CannedQuery represents a search query from the user.
type CannedQuery struct {
q *C._CannedQuery
}
func finalizeCannedQuery(query *CannedQuery) {
if query.q != nil {
C.destroy_canned_query(query.q)
}
query.q = nil
}
func makeCannedQuery(q *C._CannedQuery) *CannedQuery {
query := new(CannedQuery)
runtime.SetFinalizer(query, finalizeCannedQuery)
query.q = q
return query
}
// NewCannedQuery creates a new CannedQuery with the given scope ID,
// query string and department ID.
func NewCannedQuery(scopeID, queryString, departmentID string) *CannedQuery {
return makeCannedQuery(C.new_canned_query(
strData(scopeID),
strData(queryString),
strData(departmentID)))
}
// ScopeID returns the scope ID for this canned query.
func (query *CannedQuery) ScopeID() string {
s := C.canned_query_get_scope_id(query.q)
defer C.free(unsafe.Pointer(s))
return C.GoString(s)
}
// DepartmentID returns the department ID for this canned query.
func (query *CannedQuery) DepartmentID() string {
s := C.canned_query_get_department_id(query.q)
defer C.free(unsafe.Pointer(s))
return C.GoString(s)
}
// QueryString returns the query string for this canned query.
func (query *CannedQuery) QueryString() string {
s := C.canned_query_get_query_string(query.q)
defer C.free(unsafe.Pointer(s))
return C.GoString(s)
}
// FilterState returns the state of the filters for this canned query.
func (query *CannedQuery) FilterState() FilterState {
var length C.int
s := C.canned_query_get_filter_state(query.q, &length)
defer C.free(s)
var state FilterState
if err := json.Unmarshal(C.GoBytes(s, length), &state); err != nil {
panic(err)
}
return state
}
// SetDepartmentID changes the department ID for this canned query.
func (query *CannedQuery) SetDepartmentID(departmentID string) {
C.canned_query_set_department_id(query.q, strData(departmentID))
}
// SetQueryString changes the query string for this canned query.
func (query *CannedQuery) SetQueryString(queryString string) {
C.canned_query_set_query_string(query.q, strData(queryString))
}
// ToURI formats the canned query as a URI.
func (query *CannedQuery) ToURI() string {
s := C.canned_query_to_uri(query.q)
defer C.free(unsafe.Pointer(s))
return C.GoString(s)
}