-
Notifications
You must be signed in to change notification settings - Fork 508
/
Copy pathrequests.go
163 lines (135 loc) · 4.95 KB
/
requests.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
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package state
import (
"errors"
"strings"
"github.com/dapr/components-contrib/state/query"
)
// GetRequest is the object describing a state "fetch" request.
type GetRequest struct {
Key string `json:"key"`
Metadata map[string]string `json:"metadata"`
Options GetStateOption `json:"options,omitempty"`
}
// Key gets the Key on a GetRequest.
func (r GetRequest) GetKey() string {
return r.Key
}
// Metadata gets the Metadata on a GetRequest.
func (r GetRequest) GetMetadata() map[string]string {
return r.Metadata
}
// GetStateOption controls how a state store reacts to a get request.
type GetStateOption struct {
Consistency string `json:"consistency"` // "eventual, strong"
}
// DeleteRequest is the object describing a delete state request.
type DeleteRequest struct {
Key string `json:"key"`
ETag *string `json:"etag,omitempty"`
Metadata map[string]string `json:"metadata"`
Options DeleteStateOption `json:"options,omitempty"`
}
// Key gets the Key on a DeleteRequest.
func (r DeleteRequest) GetKey() string {
return r.Key
}
// Metadata gets the Metadata on a DeleteRequest.
func (r DeleteRequest) GetMetadata() map[string]string {
return r.Metadata
}
// HasETag returns true if the request has a non-empty ETag.
func (r DeleteRequest) HasETag() bool {
return r.ETag != nil && *r.ETag != ""
}
// Operation returns the operation type for DeleteRequest, implementing TransactionalStateOperationRequest.
func (r DeleteRequest) Operation() OperationType {
return OperationDelete
}
// DeleteWithPrefixRequest is the object describing a delete with prefix state request used for deleting actors.
type DeleteWithPrefixRequest struct {
Prefix string `json:"prefix"`
}
func (r *DeleteWithPrefixRequest) Validate() error {
if r.Prefix == "" || r.Prefix == "||" {
return errors.New("a prefix is required for deleteWithPrefix request")
}
if !strings.HasSuffix(r.Prefix, "||") {
r.Prefix += "||"
}
return nil
}
// DeleteStateOption controls how a state store reacts to a delete request.
type DeleteStateOption struct {
Concurrency string `json:"concurrency,omitempty"` // "concurrency"
Consistency string `json:"consistency"` // "eventual, strong"
}
// SetRequest is the object describing an upsert request.
type SetRequest struct {
Key string `json:"key"`
Value any `json:"value"`
ETag *string `json:"etag,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
Options SetStateOption `json:"options,omitempty"`
ContentType *string `json:"contentType,omitempty"`
}
// GetKey gets the Key on a SetRequest.
func (r SetRequest) GetKey() string {
return r.Key
}
// GetMetadata gets the Key on a SetRequest.
func (r SetRequest) GetMetadata() map[string]string {
return r.Metadata
}
// HasETag returns true if the request has a non-empty ETag.
func (r SetRequest) HasETag() bool {
return r.ETag != nil && *r.ETag != ""
}
// Operation returns the operation type for SetRequest, implementing TransactionalStateOperationRequest.
func (r SetRequest) Operation() OperationType {
return OperationUpsert
}
// SetStateOption controls how a state store reacts to a set request.
type SetStateOption struct {
Concurrency string // first-write, last-write
Consistency string // "eventual, strong"
}
// OperationType describes a CRUD operation performed against a state store.
type OperationType string
const (
// OperationUpsert is an update or create transactional operation.
OperationUpsert OperationType = "upsert"
// OperationDelete is a delete transactional operation.
OperationDelete OperationType = "delete"
)
// TransactionalStateRequest describes a transactional operation against a state store that comprises multiple types of operations
// The Request field is either a DeleteRequest or SetRequest.
type TransactionalStateRequest struct {
Operations []TransactionalStateOperation
Metadata map[string]string
}
// StateRequest is an interface that allows gets of the Key and Metadata inside requests.
type StateRequest interface {
GetKey() string
GetMetadata() map[string]string
}
// TransactionalStateOperation is an interface for all requests that can be part of a transaction.
type TransactionalStateOperation interface {
StateRequest
Operation() OperationType
}
type QueryRequest struct {
Query query.Query `json:"query"`
Metadata map[string]string `json:"metadata,omitempty"`
}