Skip to content

Commit 052fcfa

Browse files
authored
fix(state): Updates State#get to return a Hash instead of an Array (#76)
* fix(state)!: Updates the State#get call to return a hash instead of array * docs(state): Adds documentation for return value of #set
1 parent 0b8a9f7 commit 052fcfa

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

Readme.adoc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ TIP: Multiple topics can be subscribed to simultaneously by passing an array of
126126

127127
Implementation of {state-block}
128128

129+
==== Set state key(s)
130+
131+
[source,ruby]
132+
----
133+
require 'dapr/client/state' <1>
134+
desired_state = { key1: 'value1', key2: { nested: 'value2' }.to_json } <2>
135+
Rubyists::Dapr::Client::State.set(desired_state) <3>
136+
----
137+
<1> Require the state client.
138+
<2> Define the desired state. This must be a hash with simple keys, and the values must be strings (thus the #to_json)
139+
<3> Set the state. This will overwrite any existing state with the same key(s) (and update the etag of any updated keys)
140+
141+
==== Get state key(s)
142+
143+
[source,ruby]
144+
----
145+
require 'dapr/client/state' <1>
146+
states = Rubyists::Dapr::Client::State.get('key1', 'key2') <2>
147+
puts states['key1'].data <3>
148+
----
149+
<1> Require the state client.
150+
<2> Get the state for the specified key(s). This will return a hash whose values respond to #data.
151+
<3> Access the data of the state for the `key1` key.
152+
129153
=== Actors
130154

131155
Implementation of {actors-block}
@@ -178,7 +202,7 @@ Releases are handled by release-please in github actions.
178202

179203
== Contributing
180204

181-
Bug reports and pull requests are welcome on GitHub at https://github.com/rubyists/dapr.
205+
Bug reports and pull requests are welcome on GitHub at https://github.com/rubyists/dapr-ruby-client.
182206

183207
=== Conventional Commits
184208

coverage/coverage.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"timestamp":1729718516,"command_name":"Unit Tests","files":[{"filename":"/home/bougyman/rubyists/dapr/lib/dapr.rb","covered_percent":100.0,"coverage":{"lines":[null,null,1,1,null,null,1,1,7,null,null,null,1,1,1,1,1,null,null]},"covered_strength":1.6,"covered_lines":10,"lines_of_code":10},{"filename":"/home/bougyman/rubyists/dapr/lib/dapr/client.rb","covered_percent":100.0,"coverage":{"lines":[null,null,1,1,1,1,1,null,1,1,null,1,1,1,1,1,1,null,1,4,null,1,1,null,null,1,11,null,null,1,1,null,null,1,14,null,null,null,1,1,null,1,5,null,null,1,1,1,1,null,1,null,null,1,1,null,null,null,null,null]},"covered_strength":1.8823529411764706,"covered_lines":34,"lines_of_code":34},{"filename":"/home/bougyman/rubyists/dapr/lib/dapr/client/configuration.rb","covered_percent":100.0,"coverage":{"lines":[null,null,1,null,1,1,1,null,1,null,1,1,null,null,1,null,null,1,null,1,1,null,1,1,1,null,null,null,null,null,null,null,1,1,1,1,null,null,1,1,1,null,null,null,null,null]},"covered_strength":1.0,"covered_lines":21,"lines_of_code":21},{"filename":"/home/bougyman/rubyists/dapr/lib/dapr/client/lock.rb","covered_percent":100.0,"coverage":{"lines":[null,null,1,1,null,1,1,1,null,1,null,1,1,null,null,1,null,null,1,null,1,1,null,null,null,null,null,null,1,6,6,null,null,null,null,null,null,1,6,6,null,null,null,1,6,6,5,5,null,null,1,null,null,null,1,4,4,4,null,3,3,null,null,1,null,null,1,18,null,null,null,null,null,1,6,null,null,null,null,null]},"covered_strength":3.085714285714286,"covered_lines":35,"lines_of_code":35},{"filename":"/home/bougyman/rubyists/dapr/lib/dapr/client/state.rb","covered_percent":100.0,"coverage":{"lines":[null,null,1,null,1,1,1,null,null,1,null,1,1,null,1,null,1,1,1,1,1,1,null,null,null,null,null,null,1,1,null,null,null,null,null,null,null,1,1,null,null,null,null,null,1,2,null,null,null,null,null,null,1,1,1,1,null,null,null,null,1,3,1,1,1,null,null,null,null,null]},"covered_strength":1.103448275862069,"covered_lines":29,"lines_of_code":29},{"filename":"/home/bougyman/rubyists/dapr/lib/dapr/version.rb","covered_percent":100.0,"coverage":{"lines":[null,null,1,1,null,1,null,null,null]},"covered_strength":1.0,"covered_lines":3,"lines_of_code":3}],"metrics":{"covered_percent":100.0,"covered_strength":1.8484848484848484,"covered_lines":132,"total_lines":132}}
1+
{"timestamp":1729724582,"command_name":"Unit Tests","files":[{"filename":"/home/bougyman/rubyists/dapr/lib/dapr.rb","covered_percent":100.0,"coverage":{"lines":[null,null,1,1,null,null,1,1,7,null,null,null,1,1,1,1,1,null,null]},"covered_strength":1.6,"covered_lines":10,"lines_of_code":10},{"filename":"/home/bougyman/rubyists/dapr/lib/dapr/client.rb","covered_percent":100.0,"coverage":{"lines":[null,null,1,1,1,1,1,null,1,1,null,1,1,1,1,1,1,null,1,4,null,1,1,null,null,1,11,null,null,1,1,null,null,1,14,null,null,null,1,1,null,1,5,null,null,1,1,1,1,null,1,null,null,1,1,null,null,null,null,null]},"covered_strength":1.8823529411764706,"covered_lines":34,"lines_of_code":34},{"filename":"/home/bougyman/rubyists/dapr/lib/dapr/client/configuration.rb","covered_percent":100.0,"coverage":{"lines":[null,null,1,null,1,1,1,null,1,null,1,1,null,null,1,null,null,1,null,1,1,null,1,1,1,null,null,null,null,null,null,null,1,1,1,1,null,null,1,1,1,null,null,null,null,null]},"covered_strength":1.0,"covered_lines":21,"lines_of_code":21},{"filename":"/home/bougyman/rubyists/dapr/lib/dapr/client/lock.rb","covered_percent":100.0,"coverage":{"lines":[null,null,1,1,null,1,1,1,null,1,null,1,1,null,null,1,null,null,1,null,1,1,null,null,null,null,null,null,1,6,6,null,null,null,null,null,null,1,6,6,null,null,null,1,6,6,5,5,null,null,1,null,null,null,1,4,4,4,null,3,3,null,null,1,null,null,1,18,null,null,null,null,null,1,6,null,null,null,null,null]},"covered_strength":3.085714285714286,"covered_lines":35,"lines_of_code":35},{"filename":"/home/bougyman/rubyists/dapr/lib/dapr/client/state.rb","covered_percent":100.0,"coverage":{"lines":[null,null,1,null,1,1,1,null,null,1,null,1,1,null,1,null,1,1,1,1,1,1,1,null,null,null,null,null,null,1,1,null,null,null,null,null,null,null,1,1,null,null,null,null,null,1,2,null,null,null,null,null,null,1,1,1,3,null,null,null,null,1,3,1,1,1,null,null,null,null,null]},"covered_strength":1.1666666666666667,"covered_lines":30,"lines_of_code":30},{"filename":"/home/bougyman/rubyists/dapr/lib/dapr/version.rb","covered_percent":100.0,"coverage":{"lines":[null,null,1,1,null,1,null,null,null]},"covered_strength":1.0,"covered_lines":3,"lines_of_code":3}],"metrics":{"covered_percent":100.0,"covered_strength":1.8571428571428572,"covered_lines":133,"total_lines":133}}

lib/dapr/client/state.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class State
1414

1515
attr_reader :store_name
1616

17+
Item = Struct.new(:data, :etag, :metadata)
1718
Empty = Google::Protobuf::Empty
1819
Runtime = Client::Runtime
1920
GetBulkStateResponse = Runtime::GetBulkStateResponse
@@ -53,11 +54,13 @@ def initialize(store_name)
5354
def get(keys, metadata: {})
5455
logger.debug('Getting state', keys:, store_name:, metadata:)
5556
response = singleton.get_bulk_state GetStateRequest.new(store_name:, keys:, metadata:)
56-
response.items
57+
response.items.to_h { |i| [i.key, Item.new(data: i.data, etag: i.etag, metadata: i.metadata)] }
5758
end
5859

5960
# @param states [Hash] states to set (key/values in the state store)
6061
# @param metadata [Hash] metadata to include
62+
#
63+
# @return [Empty] The response from the Dapr State Management component (Empty means success)
6164
def set(states, metadata: {})
6265
states = states.map { |key, value| { key:, value:, metadata: } }
6366
request = SaveStateRequest.new(store_name:, states:)

test/dapr/client/test_state.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,15 @@ def testy?
4545
@testy
4646
end
4747

48-
context 'State' do # rubocop:disable Metrics/BlockLength
48+
context 'State' do
4949
should 'Read existing State item' do
5050
make_it_testy!
5151
messages = semantic_logger_events(Rubyists::Dapr::Client::State) do
5252
states = Rubyists::Dapr::Client::State.get('test_item1', 'test_item2')
5353

5454
assert_equal 2, states.size
55-
assert_equal 'test-state-data-0', states[0].data
56-
assert_equal 'test_item1', states[0].key
57-
assert_equal 'test_item2', states[1].key
58-
assert_equal 'test-state-data-1', states[1].data
55+
assert_equal 'test-state-data-0', states['test_item1'].data
56+
assert_equal 'test-state-data-1', states['test_item2'].data
5957
end
6058

6159
assert_equal 1, messages.size

0 commit comments

Comments
 (0)