Skip to content

Commit

Permalink
merge into master
Browse files Browse the repository at this point in the history
  • Loading branch information
aabs committed Aug 27, 2024
2 parents a879cb6 + 83ef179 commit df70bf6
Show file tree
Hide file tree
Showing 21 changed files with 2,409 additions and 344 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

# ConstFieldDocumentationHeader: The field must have a documentation header.
dotnet_diagnostic.ConstFieldDocumentationHeader.severity = none
dotnet_diagnostic.RS1035.severity = none
4 changes: 3 additions & 1 deletion ActorSrcGen.Abstractions/ActorAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ActorSrcGen;

[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public sealed class ActorAttribute : Attribute { }
public sealed class ActorAttribute : Attribute
{
}
9 changes: 9 additions & 0 deletions ActorSrcGen.Abstractions/IngestAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[AttributeUsage(AttributeTargets.Method)]
public class IngestAttribute : Attribute
{
public IngestAttribute(int priority)
{
Priority = priority;
}
public int Priority { get; set; }
}
2 changes: 1 addition & 1 deletion ActorSrcGen.Abstractions/ReceiverAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
[AttributeUsage(AttributeTargets.Method)]
public class ReceiverAttribute : Attribute
{
}
}
57 changes: 56 additions & 1 deletion ActorSrcGen.Playground/MyActor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Gridsum.DataflowEx;
using Microsoft.Extensions.Logging;
using System.Text.Json;

namespace ActorSrcGen.Abstractions.Playground;

Expand All @@ -8,6 +9,60 @@ public record Context<TRequestType, TPayloadType>(TRequestType OriginalRequest,
Stack<IDisposable> Activities);

public record PollRequest(string Id, string Name);
public record PollResults(string Id, double[] Values);

public record DataPoint(DateTimeOffset Timestamp, double Value);
public record TelemetryResponse(string Id, string Name, DataPoint[] Result);
public record TelemetryResponse(string Id, string Name, DataPoint[] Result);


[Actor]
public partial class MyActor
{
[Ingest(1)]
[NextStep(nameof(DecodeRequest))]
public async Task<string> ReceivePollRequest(CancellationToken cancellationToken)
{
return await GetTheNextRequest();
}

[FirstStep("decode incoming poll request")]
[NextStep(nameof(ActOnTheRequest))]
public PollRequest DecodeRequest(string json)
{
Console.WriteLine(nameof(DecodeRequest));
var pollRequest = JsonSerializer.Deserialize<PollRequest>(json);
return pollRequest;
}

[Step]
[NextStep(nameof(DeliverResults))]
public PollResults ActOnTheRequest(PollRequest req)
{
Console.WriteLine(nameof(ActOnTheRequest));
var result = GetTheResults(req.Id);
return result;
}

[LastStep]
public bool DeliverResults(PollResults res)
{
return TryPush(res);
}

private bool TryPush(PollResults res)
{
return true;
}

private async Task<string> GetTheNextRequest()
{
return """
{"property": "value"}
""";
}

private PollResults GetTheResults(string id)
{
return new PollResults(id, [1,2,3,4,5]);
}
}
69 changes: 54 additions & 15 deletions ActorSrcGen.Playground/MyPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,75 @@
[Actor]
public partial class MyPipeline
{
private int counter = 0;

[Ingest(1)]
[NextStep(nameof(DecodePollRequest))]
public async Task<string> ReceivePollRequest(CancellationToken cancellationToken)
{
if (++counter % 3 != 0)
{
return null;
}
await Task.Delay(250);
return nameof(ReceivePollRequest) + Guid.NewGuid();
}

[Ingest(3)]
[NextStep(nameof(DecodePollRequest))]
public async Task<string> ReceiveFcasRequest(CancellationToken cancellationToken)
{
if (++counter % 5 != 0)
{
return null;
} await Task.Delay(250);
return nameof(ReceiveFcasRequest) + Guid.NewGuid();
}

[Ingest(2)]
[NextStep(nameof(DecodePollRequest))]
public async Task<string> ReceiveBackfillRequest(CancellationToken cancellationToken)
{
if (++counter % 7 != 0)
{
return null;
} await Task.Delay(250);
return nameof(ReceiveBackfillRequest) + Guid.NewGuid();
}

// decode
[FirstStep("decode poll request")]
[Receiver]
[NextStep(nameof(SetupGapTracking))]
[NextStep(nameof(LogIncomingPollRequest))]
public TRequest DecodePollRequest(string x)
{
throw new NotImplementedException();
}

protected partial Task<string> ReceiveDecodePollRequest(CancellationToken ct)
{
throw new NotImplementedException();
Console.WriteLine(nameof(DecodePollRequest));
var pollRequest = new PollRequest(Guid.NewGuid().ToString(), x);
return new TRequest(pollRequest,pollRequest, []);
}

[Step]
[NextStep(nameof(SplitRequestBySignal))]
public TRequest SetupGapTracking(TRequest x)
{
throw new NotImplementedException();
Console.WriteLine(nameof(SetupGapTracking));
return x;
}

[Step]
[NextStep(nameof(PollForMetrics))]
public List<TRequest> SplitRequestBySignal(TRequest input)
public IEnumerable<TRequest> SplitRequestBySignal(TRequest input)
{
throw new NotImplementedException();
Console.WriteLine(nameof(SplitRequestBySignal));
yield return input;
}

[Step]
[NextStep(nameof(EncodeResult))]
public TResponse PollForMetrics(TRequest x)
{
throw new NotImplementedException();
Console.WriteLine(nameof(PollForMetrics));
return new TResponse(x.OriginalRequest, new TelemetryResponse(x.OriginalRequest.Id, "somesig", []), []);
}

// encode results
Expand All @@ -47,22 +83,25 @@ public TResponse PollForMetrics(TRequest x)
[NextStep(nameof(DeliverResults))]
public TResponse EncodeResult(TResponse x)
{
throw new NotImplementedException();
Console.WriteLine(nameof(EncodeResult));
return x;
}
// deliver results

[Step]
[NextStep(nameof(TrackTelemetryGaps))]
public TResponse DeliverResults(TResponse x)
{
throw new NotImplementedException();
Console.WriteLine(nameof(DeliverResults));
return x;
}
// track gaps

[LastStep]
public List<bool> TrackTelemetryGaps(TResponse x)
public bool TrackTelemetryGaps(TResponse x)
{
throw new NotImplementedException();
Console.WriteLine(nameof(TrackTelemetryGaps));
return true;
}

[LastStep]
Expand Down
11 changes: 7 additions & 4 deletions ActorSrcGen.Playground/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ActorSrcGen.Abstractions.Playground;
using System.Diagnostics;
using ActorSrcGen.Abstractions.Playground;

var actor = new MyPipeline();

Expand All @@ -9,20 +10,22 @@
"""))
Console.WriteLine("Called Synchronously");

var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));

var t = Task.Run(async () => await actor.ListenForReceiveDecodePollRequest(cts.Token), cts.Token);
var t = Task.Run(async () => await actor.Ingest(cts.Token), cts.Token);

while (!cts.Token.IsCancellationRequested)
{
var result = await actor.AcceptAsync(cts.Token);
Console.WriteLine($"Result: {result}");
}

await t;
await actor.SignalAndWaitForCompletionAsync();
}
catch (OperationCanceledException operationCanceledException)
catch (OperationCanceledException _)
{
Console.WriteLine("All Done!");
}

Debugger.Break();
32 changes: 32 additions & 0 deletions ActorSrcGen/ActorSrcGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0" />
<PackageReference Include="System.CodeDom" Version="8.0.0" />
</ItemGroup>


Expand All @@ -49,9 +50,40 @@
</ItemGroup>


<ItemGroup>
<None Update="Templates\Actor.tt">
<Generator>TextTemplatingFilePreprocessor</Generator>
<LastGenOutput>Actor.cs</LastGenOutput>
</None>
<None Update="Templates\HandlerBody.tt">
<Generator>TextTemplatingFilePreprocessor</Generator>
<LastGenOutput>HandlerBody.cs</LastGenOutput>
</None>
</ItemGroup>


<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.1.1" />
</ItemGroup>


<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>


<ItemGroup>
<Compile Update="Templates\Actor.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Actor.tt</DependentUpon>
</Compile>
<Compile Update="Templates\HandlerBody.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>HandlerBody.tt</DependentUpon>
</Compile>
</ItemGroup>


</Project>
9 changes: 3 additions & 6 deletions ActorSrcGen/Generators/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Immutable;
using System.Diagnostics;
using ActorSrcGen.Templates;

namespace ActorSrcGen;

Expand Down Expand Up @@ -80,12 +81,8 @@ private void OnGenerate(SourceProductionContext context,
v.VisitActor(input);
foreach (var actor in v.Actors)
{
ActorGenerator ag = new(context);
ag.GenerateActor(actor);
context.AddSource($"{actor.Name}.generated.cs", ag.Builder.ToString());
#if DEBUG
Console.WriteLine(ag.Builder.ToString());
#endif
var source = new Actor(actor).TransformText();
context.AddSource($"{actor.Name}.generated.cs", source);
}
}
catch (Exception e)
Expand Down
5 changes: 5 additions & 0 deletions ActorSrcGen/Helpers/RoslynExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ public static AttributeData GetBlockAttr(this IMethodSymbol ms)
return ms.GetAttributes().FirstOrDefault(a => attrNames.Any(x => x == a.AttributeClass.Name));
}

public static AttributeData GetIngestAttr(this IMethodSymbol ms)
{
return ms.GetAttributes().FirstOrDefault(a => a.AttributeClass.Name == nameof(IngestAttribute));
}

public static AttributeData GetBlockAttr(this INamedTypeSymbol s)
{
string[] attrNames = new[]{nameof(StepAttribute), nameof(FirstStepAttribute), nameof(LastStepAttribute) };
Expand Down
26 changes: 20 additions & 6 deletions ActorSrcGen/Helpers/TypeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,29 @@ public static string RenderTypenameOld(this ITypeSymbol? ts, bool stripTask = fa
return ts.Name;
}

public static string RenderTypename(this ITypeSymbol? ts, bool stripTask = false)
public static string RenderTypename(this ITypeSymbol? ts, bool stripTask = false, bool stripCollection = false)
{
if (ts is null)
return "";
if (stripTask && ts.Name == "Task" && ts is INamedTypeSymbol nts)
var t = ts;
if (stripTask && t.Name == "Task" && t is INamedTypeSymbol nt)
{
return nts.TypeArguments[0].ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
t = nt.TypeArguments[0];
}

if (stripCollection && t.AllInterfaces.Any(i => i.Name == "IEnumerable"))
{
nt = t as INamedTypeSymbol;
t = nt.TypeArguments[0];
}
return ts.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);

return t.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);

//if (ts is null)
// return "";
//if (stripTask && ts.Name == "Task" && ts is INamedTypeSymbol nts)
//{
// return nts.TypeArguments[0].ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
//}
//return ts.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
}

public static string RenderTypename(this GenericNameSyntax? ts, Compilation compilation, bool stripTask = false)
Expand Down
Loading

0 comments on commit df70bf6

Please sign in to comment.