-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add deserialization/snapshot constructor
This simplifies authoring by not requiring users to annotate each and every property with a private setter with the `[JsonInclude]` attribute.
- Loading branch information
Showing
11 changed files
with
212 additions
and
67 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,35 @@ | ||
// <auto-generated/> | ||
#nullable enable annotations | ||
#nullable disable warnings | ||
|
||
using System; | ||
using System.CodeDom.Compiler; | ||
using System.ComponentModel; | ||
using System.Runtime.CompilerServices; | ||
using System.Text.Json.Serialization; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Orleans; | ||
using Orleans.Concurrency; | ||
using Orleans.Runtime; | ||
using Devlooped.CloudActors; | ||
|
||
namespace {{ Namespace }} | ||
{ | ||
partial class {{ Name }} | ||
{ | ||
/// <summary> | ||
/// Intended for snapshot deserialization only. | ||
/// </summary> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
[JsonConstructor] | ||
{{~ param(x) = x.Type + " " + x.Name ~}} | ||
public {{ Name }}(string id, {{ Parameters | array.each @param | array.join ", " }}) | ||
: this(id) | ||
{ | ||
{{~ for arg in Parameters ~}} | ||
this.{{ arg.Property }} = {{ arg.Name }}; | ||
{{~ end ~}} | ||
} | ||
} | ||
} |
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,56 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Scriban; | ||
using static Devlooped.CloudActors.Diagnostics; | ||
|
||
namespace Devlooped.CloudActors; | ||
|
||
/// <summary> | ||
/// A source generator that creates a [JsonConstructor] containing the id plus | ||
/// all properties with private constructors, so restoring their values when snapshots | ||
/// are read does not require any additional attributes applied to them. | ||
/// </summary> | ||
[Generator(LanguageNames.CSharp)] | ||
public class ActorSnapshotGenerator : IIncrementalGenerator | ||
{ | ||
static readonly Template template = Template.Parse(ThisAssembly.Resources.ActorSnapshot.Text); | ||
|
||
public void Initialize(IncrementalGeneratorInitializationContext context) | ||
{ | ||
var source = context.CompilationProvider | ||
.SelectMany((x, _) => x.Assembly.GetAllTypes().OfType<INamedTypeSymbol>()) | ||
.Where(x => x.GetAttributes().Any(IsActor)); | ||
|
||
context.RegisterImplementationSourceOutput(source, (ctx, actor) => | ||
{ | ||
var props = actor.GetMembers() | ||
.OfType<IPropertySymbol>() | ||
.Where(x => | ||
x.CanBeReferencedByName && | ||
!x.IsIndexer && | ||
!x.IsAbstract && | ||
x.SetMethod is { } setter && | ||
setter.DeclaredAccessibility == Accessibility.Private) | ||
.ToArray(); | ||
|
||
if (props.Length > 0) | ||
{ | ||
var model = new SnapshotModel( | ||
Namespace: actor.ContainingNamespace.ToDisplayString(), | ||
Name: actor.Name, | ||
Parameters: props.Select(x => new Parameter( | ||
char.ToLowerInvariant(x.Name[0]) + new string(x.Name.Skip(1).ToArray()), | ||
x.Type.ToDisplayString(FullName), x.Name))); | ||
|
||
var output = template.Render(model, member => member.Name); | ||
|
||
ctx.AddSource($"{actor.ToFileName()}.g.cs", output); | ||
} | ||
}); | ||
} | ||
|
||
record Parameter(string Name, string Type, string Property); | ||
|
||
record SnapshotModel(string Namespace, string Name, IEnumerable<Parameter> Parameters); | ||
} |
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
Oops, something went wrong.