-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dotnet] Add mixin for easier state save/load apis (#5438)
Co-authored-by: Ryan Sweet <[email protected]>
- Loading branch information
1 parent
213da85
commit 181925c
Showing
7 changed files
with
120 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ISaveStateMixin.cs | ||
|
||
using System.Text.Json; | ||
|
||
namespace Microsoft.AutoGen.Contracts; | ||
|
||
/// <summary> | ||
/// Defines a contract for saving and loading the state of an object. | ||
/// The state must be JSON serializable. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the object implementing this interface.</typeparam> | ||
/// | ||
public interface ISaveStateMixin<T> : ISaveState | ||
{ | ||
/// <summary> | ||
/// Saves the current state of the object. | ||
/// </summary> | ||
/// <returns> | ||
/// A task representing the asynchronous operation, returning a dictionary | ||
/// containing the saved state. The structure of the state is implementation-defined | ||
/// but must be JSON serializable. | ||
/// </returns> | ||
async ValueTask<JsonElement> ISaveState.SaveStateAsync() | ||
{ | ||
var state = await SaveStateImpl(); | ||
return JsonSerializer.SerializeToElement(state); | ||
} | ||
|
||
/// <summary> | ||
/// Loads a previously saved state into the object. | ||
/// </summary> | ||
/// <param name="state"> | ||
/// A dictionary representing the saved state. The structure of the state | ||
/// is implementation-defined but must be JSON serializable. | ||
/// </param> | ||
/// <returns>A task representing the asynchronous operation.</returns> | ||
ValueTask ISaveState.LoadStateAsync(JsonElement state) | ||
{ | ||
// Throw if failed to deserialize | ||
var stateObject = JsonSerializer.Deserialize<T>(state) ?? throw new InvalidDataException(); | ||
return LoadStateImpl(stateObject); | ||
} | ||
|
||
protected ValueTask<T> SaveStateImpl(); | ||
|
||
protected ValueTask LoadStateImpl(T state); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters