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 all commits
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
38 changes: 38 additions & 0 deletions src/Dapr.Client/BulkStateItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,42 @@
/// </summary>
public string ETag { get; }
}

/// <summary>
/// Represents a state object returned from a bulk get state operation where the value has
/// been deserialized to the specified type.
/// </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 typed 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; }

Check warning on line 78 in src/Dapr.Client/BulkStateItem.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Client/BulkStateItem.cs#L78

Added line #L78 was not covered by tests

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

/// <summary>
/// Get the ETag.
/// </summary>
public string ETag { get; }

Check warning on line 88 in src/Dapr.Client/BulkStateItem.cs

View check run for this annotation

Codecov / codecov/patch

src/Dapr.Client/BulkStateItem.cs#L88

Added line #L88 was not covered by tests
}
}
14 changes: 14 additions & 0 deletions src/Dapr.Client/DaprClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,20 @@ 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. This overload should be used
/// if you expect the values of all the retrieved items to match the shape of the indicated <typeparam name="TValue"/>. If you expect that
/// the values may differ in type from one another, do not specify the type parameter and instead use the original <see cref="GetBulkStateAsync"/> method
/// so the serialized string values will be returned instead.
/// </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
55 changes: 50 additions & 5 deletions src/Dapr.Client/DaprClientGrpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,15 +601,58 @@ public override async Task<TResponse> InvokeMethodGrpcAsync<TRequest, TResponse>

#region State Apis

/// <inheritdoc />
public override async Task<IReadOnlyList<BulkStateItem>> GetBulkStateAsync(string storeName, IReadOnlyList<string> keys, int? parallelism, IReadOnlyDictionary<string, string> metadata = default, CancellationToken cancellationToken = default)
{
var rawBulkState = await GetBulkStateRawAsync(storeName, keys, parallelism, metadata, cancellationToken);

var bulkResponse = new List<BulkStateItem>();
foreach (var item in rawBulkState)
{
bulkResponse.Add(new BulkStateItem(item.Key, item.Value.ToStringUtf8(), item.Etag));
}

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)
{
var rawBulkState = await GetBulkStateRawAsync(storeName, keys, parallelism, metadata, cancellationToken);

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

return bulkResponse;
}

/// <summary>
/// Retrieves the bulk state data, but rather than deserializing the values, leaves the specific handling
/// to the public callers of this method to avoid duplicate deserialization.
/// </summary>
private async Task<IReadOnlyList<(string Key, string Etag, ByteString Value)>> GetBulkStateRawAsync(
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,
StoreName = storeName,
Parallelism = parallelism ?? default
};

Expand All @@ -632,18 +675,20 @@ public override async Task<IReadOnlyList<BulkStateItem>> GetBulkStateAsync(strin
}
catch (RpcException ex)
{
throw new DaprException("State operation failed: the Dapr endpoint indicated a failure. See InnerException for details.", ex);
throw new DaprException(
"State operation failed: the Dapr endpoint indicated a failure. See InnerException for details.",
ex);
}

var bulkResponse = new List<BulkStateItem>();
var bulkResponse = new List<(string Key, string Etag, ByteString Value)>();
foreach (var item in response.Items)
{
bulkResponse.Add(new BulkStateItem(item.Key, item.Data.ToStringUtf8(), item.Etag));
bulkResponse.Add((item.Key, item.Etag, item.Data));
}

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
Loading