forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_terms_operation.go
103 lines (85 loc) · 2.47 KB
/
get_terms_operation.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
94
95
96
97
98
99
100
101
102
103
package ravendb
import (
"net/http"
"strconv"
)
var _ IMaintenanceOperation = &GetTermsOperation{}
// GetTermsOperation represents "get terms" operation
type GetTermsOperation struct {
_indexName string
_field string
_fromValue string
_pageSize int // 0 for unset
Command *GetTermsCommand
}
// NewGetTermsOperation returns GetTermsOperation. pageSize 0 means default size
func NewGetTermsOperation(indexName string, field string, fromValue string, pageSize int) (*GetTermsOperation, error) {
if indexName == "" {
return nil, newIllegalStateError("Index name connot be empty")
}
if field == "" {
return nil, newIllegalStateError("Field name connot be empty")
}
return &GetTermsOperation{
_indexName: indexName,
_field: field,
_fromValue: fromValue,
_pageSize: pageSize,
}, nil
}
// GetCommand returns command for this operation
func (o *GetTermsOperation) GetCommand(conventions *DocumentConventions) (RavenCommand, error) {
var err error
o.Command, err = NewGetTermsCommand(o._indexName, o._field, o._fromValue, o._pageSize)
if err != nil {
return nil, err
}
return o.Command, err
}
var (
_ RavenCommand = &GetTermsCommand{}
)
// GetTermsCommand represents "get terms" command
type GetTermsCommand struct {
RavenCommandBase
_indexName string
_field string
_fromValue string
_pageSize int
Result []string
}
// NewGetTermsCommand returns new GetTermsCommand
func NewGetTermsCommand(indexName string, field string, fromValue string, pageSize int) (*GetTermsCommand, error) {
if indexName == "" {
return nil, newIllegalArgumentError("Index name connot be empty")
}
res := &GetTermsCommand{
RavenCommandBase: NewRavenCommandBase(),
_indexName: indexName,
_field: field,
_fromValue: fromValue,
_pageSize: pageSize,
}
res.IsReadRequest = true
return res, nil
}
func (c *GetTermsCommand) CreateRequest(node *ServerNode) (*http.Request, error) {
pageSize := ""
if c._pageSize > 0 {
pageSize = strconv.Itoa(c._pageSize)
}
url := node.URL + "/databases/" + node.Database + "/indexes/terms?name=" + urlUtilsEscapeDataString(c._indexName) + "&field=" + urlUtilsEscapeDataString(c._field) + "&fromValue=" + c._fromValue + "&pageSize=" + pageSize
return newHttpGet(url)
}
func (c *GetTermsCommand) SetResponse(response []byte, fromCache bool) error {
if response == nil {
return throwInvalidResponse()
}
var res TermsQueryResult
err := jsonUnmarshal(response, &res)
if err != nil {
return err
}
c.Result = res.Terms
return nil
}