Skip to content

Commit c89770a

Browse files
authored
Add SaveStateWithETag() convenience function (dapr#321)
In the state API there's an existing DeleteStateWithETag() convenience function but there does not appear to be an equivalent SaveStateWithETag(). This commit adds SetStateWithETag() so that consumers of the SDK don't have to employ the more verbose SaveBulkState() when they only need to update a singular item. Client code updating a single item with an etag prior to this commit looks like: -- item := &dapr.SetStateItem{ Etag: &dapr.ETag{ Value: "deadbeef", }, Key: "order_1", Value: []byte(data), } err := client.SaveBulkState(ctx, store, item) -- Client code after this commit can reduce to: -- err := client.SaveStateWithEtag(ctx, store, "order_1", []byte(data), "deadbeef") -- Signed-off-by: Mike Brown <github@torvosoft.com> Signed-off-by: Mike Brown <github@torvosoft.com>
1 parent c1556a4 commit c89770a

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

client/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ type Client interface {
9090
// SaveState saves the raw data into store using default state options.
9191
SaveState(ctx context.Context, storeName, key string, data []byte, meta map[string]string, so ...StateOption) error
9292

93+
// SaveState saves the raw data into store using provided state options and etag.
94+
SaveStateWithETag(ctx context.Context, storeName, key string, data []byte, etag string, meta map[string]string, so ...StateOption) error
95+
9396
// SaveBulkState saves multiple state item to store with specified options.
9497
SaveBulkState(ctx context.Context, storeName string, items ...*SetStateItem) error
9598

client/state.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ func (c *GRPCClient) ExecuteStateTransaction(ctx context.Context, storeName stri
276276

277277
// SaveState saves the raw data into store, default options: strong, last-write.
278278
func (c *GRPCClient) SaveState(ctx context.Context, storeName, key string, data []byte, meta map[string]string, so ...StateOption) error {
279+
return c.SaveStateWithETag(ctx, storeName, key, data, "", meta, so...)
280+
}
281+
282+
// SaveStateWithETag saves the raw data into store using provided state options and etag.
283+
func (c *GRPCClient) SaveStateWithETag(ctx context.Context, storeName, key string, data []byte, etag string, meta map[string]string, so ...StateOption) error {
279284
stateOptions := new(StateOptions)
280285
for _, o := range so {
281286
o(stateOptions)
@@ -289,6 +294,9 @@ func (c *GRPCClient) SaveState(ctx context.Context, storeName, key string, data
289294
Metadata: meta,
290295
Options: stateOptions,
291296
}
297+
if etag != "" {
298+
item.Etag = &ETag{Value: etag}
299+
}
292300
return c.SaveBulkState(ctx, storeName, item)
293301
}
294302

client/state_test.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,7 @@ func TestSaveState(t *testing.T) {
8686
})
8787

8888
t.Run("save data with version", func(t *testing.T) {
89-
item := &SetStateItem{
90-
Etag: &ETag{
91-
Value: "1",
92-
},
93-
Key: key,
94-
Value: []byte(data),
95-
}
96-
err := testClient.SaveBulkState(ctx, store, item)
89+
err := testClient.SaveStateWithETag(ctx, store, key, []byte(data), "1", nil)
9790
assert.Nil(t, err)
9891
})
9992

@@ -147,15 +140,8 @@ func TestDeleteState(t *testing.T) {
147140
})
148141

149142
t.Run("save data again with etag, meta", func(t *testing.T) {
150-
err := testClient.SaveBulkState(ctx, store, &SetStateItem{
151-
Key: key,
152-
Value: []byte(data),
153-
Etag: &ETag{
154-
Value: "1",
155-
},
156-
Metadata: map[string]string{"meta1": "value1"},
157-
Options: &StateOptions{Concurrency: StateConcurrencyFirstWrite, Consistency: StateConsistencyEventual},
158-
})
143+
meta := map[string]string{"meta1": "value1"}
144+
err := testClient.SaveStateWithETag(ctx, store, key, []byte(data), "1", meta, WithConsistency(StateConsistencyEventual), WithConcurrency(StateConcurrencyFirstWrite))
159145
assert.Nil(t, err)
160146
})
161147
t.Run("confirm data saved", func(t *testing.T) {

0 commit comments

Comments
 (0)