Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overload to deserialize GetBulkStateAsync item values #1173

Merged
merged 11 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Dapr.Client/BulkStateItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,41 @@ public BulkStateItem(string key, string value, string etag)
/// </summary>
public string ETag { get; }
}

/// <summary>
/// Represents a state object returned from a bulk get state operation.
/// </summary>
public readonly struct BulkStateItem<TValue>
{
/// <summary>
/// Initializes a new instance of the <see cref="BulkStateItem"/> class.
/// </summary>
/// <param name="key">The state key.</param>
/// <param name="value">The value.</param>
/// <param name="etag">The ETag.</param>
/// <remarks>
/// Application code should not need to create instances of <see cref="BulkStateItem" />.
/// </remarks>
public BulkStateItem(string key, TValue value, string etag)
{
this.Key = key;
this.Value = value;
this.ETag = etag;
}

/// <summary>
/// Gets the state key.
/// </summary>
public string Key { get; }

/// <summary>
/// Gets the value.
/// </summary>
public TValue Value { get; }
WhitWaldo marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Get the ETag.
/// </summary>
public string ETag { get; }
}
}
11 changes: 11 additions & 0 deletions src/Dapr.Client/DaprClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,17 @@ public abstract Task<TResponse> InvokeMethodGrpcAsync<TRequest, TResponse>(
/// <returns>A <see cref="Task{IReadOnlyList}" /> that will return the list of values when the operation has completed.</returns>
public abstract Task<IReadOnlyList<BulkStateItem>> GetBulkStateAsync(string storeName, IReadOnlyList<string> keys, int? parallelism, IReadOnlyDictionary<string, string> metadata = default, CancellationToken cancellationToken = default);

/// <summary>
/// Gets a list of deserialized values associated with the <paramref name="keys" /> from the Dapr state store.
WhitWaldo marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <param name="storeName">The name of state store to read from.</param>
/// <param name="keys">The list of keys to get values for.</param>
/// <param name="parallelism">The number of concurrent get operations the Dapr runtime will issue to the state store. a value equal to or smaller than 0 means max parallelism.</param>
/// <param name="metadata">A collection of metadata key-value pairs that will be provided to the state store. The valid metadata keys and values are determined by the type of state store used.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
/// <returns>A <see cref="Task{IReadOnlyList}" /> that will return the list of deserialized values when the operation has completed.</returns>
public abstract Task<IReadOnlyList<BulkStateItem<TValue>>> GetBulkStateAsync<TValue>(string storeName, IReadOnlyList<string> keys, int? parallelism, IReadOnlyDictionary<string, string> metadata = default, CancellationToken cancellationToken = default);

/// <summary>
/// Saves a list of <paramref name="items" /> to the Dapr state store.
/// </summary>
Expand Down
49 changes: 49 additions & 0 deletions src/Dapr.Client/DaprClientGrpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,55 @@ public override async Task<IReadOnlyList<BulkStateItem>> GetBulkStateAsync(strin
return bulkResponse;
}

/// <inheritdoc/>
public override async Task<IReadOnlyList<BulkStateItem<TValue>>> GetBulkStateAsync<TValue>(string storeName,
IReadOnlyList<string> keys, int? parallelism, IReadOnlyDictionary<string, string> metadata = default,
CancellationToken cancellationToken = default)
{
ArgumentVerifier.ThrowIfNullOrEmpty(storeName, nameof(storeName));
WhitWaldo marked this conversation as resolved.
Show resolved Hide resolved
if (keys.Count == 0)
throw new ArgumentException("keys do not contain any elements");

var envelope = new Autogenerated.GetBulkStateRequest()
{
StoreName = storeName, Parallelism = parallelism ?? default
WhitWaldo marked this conversation as resolved.
Show resolved Hide resolved
};

if (metadata != null)
{
foreach (var kvp in metadata)
{
envelope.Metadata.Add(kvp.Key, kvp.Value);
}
}

envelope.Keys.AddRange(keys);

var options = CreateCallOptions(headers: null, cancellationToken);
Autogenerated.GetBulkStateResponse response;

try
{
response = await client.GetBulkStateAsync(envelope, options);
}
catch (RpcException ex)
{
throw new DaprException(
"State operation failed: the Dapr endpoint indicated a failure. See InnerException for details.",
ex);
}

var bulkResponse = new List<BulkStateItem<TValue>>();
foreach (var item in response.Items)
{
var deserializedValue =
TypeConverters.FromJsonByteString<TValue>(item.Data, this.JsonSerializerOptions);
bulkResponse.Add(new BulkStateItem<TValue>(item.Key, deserializedValue, item.Etag));
}

return bulkResponse;
}

/// <inheritdoc/>
public override async Task<TValue> GetStateAsync<TValue>(
string storeName,
Expand Down
24 changes: 24 additions & 0 deletions test/Dapr.Client.Test/StateApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ public async Task GetBulkStateAsync_CanReadState()
state.Should().HaveCount(1);
}

[Fact]
public async Task GetBulkStateAsync_CanReadDeserializedState()
{
await using var client = TestClient.CreateForDaprClient();

var key = "test";
var request = await client.CaptureGrpcRequestAsync(async daprClient =>
{
return await daprClient.GetBulkStateAsync<Widget>("testStore", new List<string>() {key}, null);
});

// Create Response & Respond
const string size = "small";
const string color = "yellow";
var data = new Widget() {Size = size, Color = color};
var envelope = MakeGetBulkStateResponse<Widget>(key, data);
var state = await request.CompleteWithMessageAsync(envelope);

// Get response and validate
state.Should().HaveCount(1);
state[0].Value.Size.Should().Match(size);
state[0].Value.Color.Should().Match(color);
}

[Fact]
public async Task GetBulkStateAsync_WrapsRpcException()
{
Expand Down