-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3caa32
commit d9c93a3
Showing
6 changed files
with
184 additions
and
5 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/JasperFx/Events/CodeGeneration/CreateDocumentMethodCollection.cs
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 @@ | ||
namespace JasperFx.Events.CodeGeneration; | ||
|
||
/// <summary> | ||
/// This would be a helper for the open ended EventProjection | ||
/// </summary> | ||
public class CreateDocumentMethodCollection: MethodCollection | ||
{ | ||
private readonly Type _operationsType; | ||
public static readonly string MethodName = "Create"; | ||
public static readonly string TransformMethodName = "Transform"; | ||
|
||
|
||
|
||
public CreateDocumentMethodCollection(Type projectionType, Type operationsType): base(new[] { MethodName, TransformMethodName }, | ||
projectionType, null) | ||
{ | ||
_operationsType = operationsType; | ||
_validArgumentTypes.Add(operationsType); | ||
} | ||
|
||
public override IEventHandlingFrame CreateEventTypeHandler(Type aggregateType, | ||
IStorageMapping aggregateMapping, | ||
MethodSlot slot) | ||
{ | ||
return new CreateMethodFrame(_operationsType, slot); | ||
} | ||
|
||
protected override void validateMethod(MethodSlot method) | ||
{ | ||
if (method.ReturnType == typeof(void)) | ||
{ | ||
method.AddError("The return value must be a new document"); | ||
} | ||
} | ||
} |
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,45 @@ | ||
using System.Reflection; | ||
using JasperFx.CodeGeneration; | ||
using JasperFx.CodeGeneration.Frames; | ||
using JasperFx.CodeGeneration.Model; | ||
|
||
namespace JasperFx.Events.CodeGeneration; | ||
|
||
internal class CreateMethodFrame: MethodCall, IEventHandlingFrame | ||
{ | ||
private readonly Type _operationsType; | ||
private static int _counter; | ||
|
||
private Variable _operations; | ||
|
||
public CreateMethodFrame(Type operationsType, MethodSlot slot): base(slot.HandlerType, (MethodInfo)slot.Method) | ||
{ | ||
_operationsType = operationsType; | ||
EventType = Method.GetEventType(null); | ||
ReturnVariable.OverrideName(ReturnVariable.Usage + ++_counter); | ||
} | ||
|
||
public Type EventType { get; } | ||
|
||
public void Configure(EventProcessingFrame parent) | ||
{ | ||
// Replace any arguments to IEvent<T> | ||
TrySetArgument(parent.SpecificEvent); | ||
|
||
// Replace any arguments to the specific T event type | ||
TrySetArgument(parent.DataOnly); | ||
} | ||
|
||
public override IEnumerable<Variable> FindVariables(IMethodVariables chain) | ||
{ | ||
foreach (var variable in base.FindVariables(chain)) yield return variable; | ||
|
||
_operations = chain.FindVariable(_operationsType); | ||
} | ||
|
||
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) | ||
{ | ||
base.GenerateCode(method, writer); | ||
writer.WriteLine($"{_operations.Usage}.Store({ReturnVariable.Usage});"); | ||
} | ||
} |
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,25 @@ | ||
using System.Reflection; | ||
using JasperFx.CodeGeneration.Frames; | ||
|
||
namespace JasperFx.Events.CodeGeneration; | ||
|
||
internal class ProjectMethodCall: MethodCall, IEventHandlingFrame | ||
{ | ||
public ProjectMethodCall(MethodSlot slot): base(slot.HandlerType, (MethodInfo)slot.Method) | ||
{ | ||
EventType = Method.GetEventType(null); | ||
Target = slot.Setter; | ||
} | ||
|
||
public Type EventType { get; } | ||
|
||
public void Configure(EventProcessingFrame parent) | ||
{ | ||
// Replace any arguments to IEvent<T> | ||
|
||
TrySetArgument(parent.SpecificEvent); | ||
|
||
// Replace any arguments to the specific T event type | ||
TrySetArgument(parent.DataOnly); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/JasperFx/Events/CodeGeneration/ProjectMethodCollection.cs
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,36 @@ | ||
using JasperFx.Core.Reflection; | ||
|
||
namespace JasperFx.Events.CodeGeneration; | ||
|
||
/// <summary> | ||
/// This would be a helper for the open ended EventProjection | ||
/// </summary> | ||
public class ProjectMethodCollection: MethodCollection | ||
{ | ||
private readonly Type _operationsType; | ||
public static readonly string MethodName = "Project"; | ||
|
||
|
||
public ProjectMethodCollection(Type projectionType, Type operationsType): base(MethodName, projectionType, null) | ||
{ | ||
_operationsType = operationsType; | ||
_validArgumentTypes.Add(operationsType); | ||
_validReturnTypes.Add(typeof(void)); | ||
_validReturnTypes.Add(typeof(Task)); | ||
} | ||
|
||
protected override void validateMethod(MethodSlot method) | ||
{ | ||
if (method.Method.GetParameters().All(x => x.ParameterType != _operationsType)) | ||
{ | ||
method.AddError($"{_operationsType.FullNameInCode()} is a required parameter"); | ||
} | ||
} | ||
|
||
public override IEventHandlingFrame CreateEventTypeHandler(Type aggregateType, | ||
IStorageMapping aggregateMapping, | ||
MethodSlot slot) | ||
{ | ||
return new ProjectMethodCall(slot); | ||
} | ||
} |
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