forked from a8m/documentdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
150 lines (128 loc) · 4.26 KB
/
options.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package documentdb
import (
"encoding/json"
"strconv"
)
// Consistency type to define consistency levels
type Consistency string
const (
// Strong consistency level
Strong Consistency = "strong"
// Bounded consistency level
Bounded Consistency = "bounded"
// Session consistency level
Session Consistency = "session"
// Eventual consistency level
Eventual Consistency = "eventual"
)
// CallOption function
type CallOption func(r *Request) error
// PartitionKey specificy which partiotion will be used to satisfty the request
func PartitionKey(partitionKey interface{}) CallOption {
// The partition key header must be an array following the spec:
// https: //docs.microsoft.com/en-us/rest/api/cosmos-db/common-cosmosdb-rest-request-headers
// and must contain brackets
// example: x-ms-documentdb-partitionkey: [ "abc" ]
var (
pk []byte
err error
)
switch v := partitionKey.(type) {
case json.Marshaler:
pk, err = Serialization.Marshal(v)
default:
pk, err = Serialization.Marshal([]interface{}{v})
}
header := []string{string(pk)}
return func(r *Request) error {
if err != nil {
return err
}
r.Header[HEADER_PARTITION_KEY] = header
return nil
}
}
// Upsert if set to true, Cosmos DB creates the document with the ID (and partition key value if applicable) if it doesn’t exist, or update the document if it exists.
func Upsert() CallOption {
return func(r *Request) error {
r.Header.Set(HEADER_UPSERT, "true")
return nil
}
}
// Limit set max item count for response
func Limit(limit int) CallOption {
header := strconv.Itoa(limit)
return func(r *Request) error {
r.Header.Set(HEADER_MAX_ITEM_COUNT, header)
return nil
}
}
// Continuation a string token returned for queries and read-feed operations if there are more results to be read. Clients can retrieve the next page of results by resubmitting the request with the x-ms-continuation request header set to this value.
func Continuation(continuation string) CallOption {
return func(r *Request) error {
if continuation == "" {
return nil
}
r.Header.Set(HEADER_CONTINUATION, continuation)
return nil
}
}
// ConsistencyLevel override for read options against documents and attachments. The valid values are: Strong, Bounded, Session, or Eventual (in order of strongest to weakest). The override must be the same or weaker than the account�s configured consistency level.
func ConsistencyLevel(consistency Consistency) CallOption {
return func(r *Request) error {
r.Header.Set(HEADER_CONSISTENCY, string(consistency))
return nil
}
}
// SessionToken a string token used with session level consistency. For more information, see
func SessionToken(sessionToken string) CallOption {
return func(r *Request) error {
r.Header.Set(HEADER_SESSION, sessionToken)
return nil
}
}
// CrossPartition allows query to run on all partitions
func CrossPartition() CallOption {
return func(r *Request) error {
r.Header.Set(HEADER_CROSSPARTITION, "True")
return nil
}
}
// IfMatch used to make operation conditional for optimistic concurrency. The value should be the etag value of the resource.
// (applicable only on PUT and DELETE)
func IfMatch(etag string) CallOption {
return func(r *Request) error {
r.Header.Set(HEADER_IFMATCH, etag)
return nil
}
}
// IfNoneMatch makes operation conditional to only execute if the resource has changed. The value should be the etag of the resource.
// Optional (applicable only on GET)
func IfNoneMatch(etag string) CallOption {
return func(r *Request) error {
r.Header.Set(HEADER_IF_NONE_MATCH, etag)
return nil
}
}
// IfModifiedSince returns etag of resource modified after specified date in RFC 1123 format. Ignored when If-None-Match is specified
// Optional (applicable only on GET)
func IfModifiedSince(date string) CallOption {
return func(r *Request) error {
r.Header.Set(HEADER_IF_MODIFIED_SINCE, date)
return nil
}
}
// ChangeFeed indicates a change feed request
func ChangeFeed() CallOption {
return func(r *Request) error {
r.Header.Set(HEADER_A_IM, "Incremental feed")
return nil
}
}
// ChangeFeedPartitionRangeID used in change feed requests. The partition key range ID for reading data.
func ChangeFeedPartitionRangeID(id string) CallOption {
return func(r *Request) error {
r.Header.Set(HEADER_PARTITION_KEY_RANGE_ID, id)
return nil
}
}