|
| 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 | +} |
0 commit comments