Skip to content

Commit 61177ca

Browse files
committed
Moved EventProcessingFrame to JasperFx
1 parent 021fa43 commit 61177ca

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

src/JasperFx/Core/Reflection/TypeNameExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reflection;
2+
using System.Text.RegularExpressions;
23

34
namespace JasperFx.Core.Reflection;
45

@@ -206,4 +207,20 @@ public static string ToSuffixedTypeName(this Type type, string suffix)
206207
var hash = Math.Abs(type.FullNameInCode().GetStableHashCode());
207208
return $"{prefix}{suffix}{hash}".Replace("-", "");
208209
}
210+
211+
public static string Sanitize(this string value)
212+
{
213+
return Regex.Replace(value, @"[\#\<\>\,\.\]\[\`\+\-]", "_").Replace(" ", "");
214+
}
215+
216+
public static string ToTypeNamePart(this Type type)
217+
{
218+
if (type.IsGenericType)
219+
{
220+
return type.Name.Split('`').First() + "_of_" +
221+
type.GetGenericArguments().Select(x => x.ToTypeNamePart()).Join("_");
222+
}
223+
224+
return type.Name;
225+
}
209226
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using JasperFx.CodeGeneration;
2+
using JasperFx.CodeGeneration.Frames;
3+
using JasperFx.CodeGeneration.Model;
4+
using JasperFx.Core.Reflection;
5+
6+
namespace JasperFx.Events.CodeGeneration;
7+
8+
public interface IEventHandlingFrame
9+
{
10+
Type EventType { get; }
11+
void Configure(EventProcessingFrame parent);
12+
}
13+
14+
/// <summary>
15+
/// Organizes a single Event type within a pattern
16+
/// matching switch statement
17+
/// </summary>
18+
public class EventProcessingFrame: Frame
19+
{
20+
private static int _counter;
21+
protected readonly IList<Frame> _inner = new List<Frame>();
22+
private Variable _event;
23+
24+
public EventProcessingFrame(bool isAsync, Type aggregateType, Type eventType): base(isAsync)
25+
{
26+
EventType = eventType;
27+
AggregateType = aggregateType;
28+
29+
SpecificEvent = new Variable(typeof(IEvent<>).MakeGenericType(eventType),
30+
"event_" + eventType.Name.Sanitize() + ++_counter);
31+
DataOnly = new Variable(EventType, $"{SpecificEvent.Usage}.{nameof(IEvent<string>.Data)}");
32+
}
33+
34+
public EventProcessingFrame(Type aggregateType, IEventHandlingFrame inner)
35+
: this(inner.As<Frame>().IsAsync, aggregateType, inner.EventType)
36+
{
37+
Add(inner.As<Frame>());
38+
}
39+
40+
public Type AggregateType { get; }
41+
42+
public Type EventType { get; }
43+
44+
45+
public Variable SpecificEvent { get; }
46+
47+
public Variable Aggregate { get; private set; }
48+
49+
public Variable DataOnly { get; }
50+
51+
public void Add(Frame inner)
52+
{
53+
_inner.Add(inner);
54+
}
55+
56+
public override IEnumerable<Variable> FindVariables(IMethodVariables chain)
57+
{
58+
if (AggregateType != null)
59+
{
60+
// You don't need it if you're in a Create method
61+
Aggregate = chain.TryFindVariable(AggregateType, VariableSource.All);
62+
if (Aggregate != null)
63+
{
64+
yield return Aggregate;
65+
}
66+
}
67+
68+
foreach (var inner in _inner.OfType<IEventHandlingFrame>()) inner.Configure(this);
69+
70+
_event = chain.FindVariable(typeof(IEvent));
71+
72+
yield return _event;
73+
74+
foreach (var inner in _inner)
75+
{
76+
foreach (var variable in inner.FindVariables(chain)) yield return variable;
77+
}
78+
}
79+
80+
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
81+
{
82+
writer.Write($"case {SpecificEvent.VariableType.FullNameInCode()} {SpecificEvent.Usage}:");
83+
84+
writer.IndentionLevel++;
85+
86+
foreach (var frame in _inner) frame.GenerateCode(method, writer);
87+
88+
writer.Write("break;");
89+
writer.IndentionLevel--;
90+
91+
Next?.GenerateCode(method, writer);
92+
}
93+
}

src/JasperFx/Events/Projections/ProjectionSource.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,16 @@ public virtual Task ApplyAsync(TOperations operations, IReadOnlyList<IEvent> eve
102102
// TODO -- make this use the Project<T> stuff. Order by most specific first
103103
return Task.CompletedTask;
104104
}
105+
106+
[JasperFxIgnore]
107+
public void Project<TEvent>(Action<TEvent, TOperations> project)
108+
{
109+
//_projectMethods.AddLambda(project, typeof(TEvent));
110+
}
111+
112+
[JasperFxIgnore]
113+
public void ProjectAsync<TEvent>(Func<TEvent, TOperations, Task> project)
114+
{
115+
//_projectMethods.AddLambda(project, typeof(TEvent));
116+
}
105117
}

0 commit comments

Comments
 (0)