Skip to content

Commit

Permalink
fixed warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
coronabytes committed Oct 19, 2024
1 parent eeb9364 commit f2ddf93
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 34 deletions.
10 changes: 6 additions & 4 deletions Core.ServiceMesh.SourceGen.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ namespace Core.ServiceMesh.SourceGen.Tests;

public static class TestHelper
{
public static async Task VerifySourceGen(string source)
public static Task VerifySourceGen(string source)
{
var syntaxTree = CSharpSyntaxTree.ParseText(source);

// force reference
typeof(ServiceMeshAttribute).ToString();
var _ = typeof(ServiceMeshAttribute).ToString();

var references = AppDomain
.CurrentDomain.GetAssemblies()
Expand All @@ -34,8 +34,8 @@ public static async Task VerifySourceGen(string source)

var generator = new ServiceMeshGenerator();

var driver = CSharpGeneratorDriver.Create(generator)
.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out _);
CSharpGeneratorDriver.Create(generator)
.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var _);

foreach (var tree in outputCompilation.SyntaxTrees.Skip(1))
{
Expand All @@ -44,5 +44,7 @@ public static async Task VerifySourceGen(string source)

Assert.Empty(genErrors);
}

return Task.CompletedTask;
}
}
8 changes: 8 additions & 0 deletions Core.ServiceMesh.SourceGen/Core.ServiceMesh.SourceGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
<IsRoslynComponent>true</IsRoslynComponent>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;NU5128</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<NoWarn>1701;1702;NU5128</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
Expand Down
17 changes: 10 additions & 7 deletions Core.ServiceMesh.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Core.ServiceMesh.Abstractions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SampleInterfaces;
using Xunit.Abstractions;

Expand All @@ -10,9 +11,9 @@ namespace Core.ServiceMesh.Tests;
public class UnitTest1(ITestOutputHelper logger) : IAsyncLifetime
{
private readonly CancellationTokenSource _cancellation = new();
private IServiceMesh _mesh;
private IServiceProvider _serviceProvider;
private BackgroundService _worker;
private IServiceMesh? _mesh;
private IServiceProvider? _serviceProvider;
private BackgroundService? _worker;

public async Task InitializeAsync()
{
Expand All @@ -34,16 +35,18 @@ public async Task InitializeAsync()
_mesh = _serviceProvider.GetRequiredService<IServiceMesh>();

_worker = (BackgroundService)_mesh;
logger.WriteLine("starting background worker");
await _worker.StartAsync(_cancellation.Token);
}

public async Task DisposeAsync()
{
logger.WriteLine("aborting background worker");
await _cancellation.CancelAsync();

try
{
await _worker.ExecuteTask!;
await _worker!.ExecuteTask!;
}
catch
{
Expand All @@ -54,7 +57,7 @@ public async Task DisposeAsync()
[Fact]
public async Task Test1()
{
var someService = _mesh.CreateProxy<ISomeService>();
var someService = _mesh!.CreateProxy<ISomeService>();

var res = await someService.GenericAdd(2, 4);

Expand All @@ -64,7 +67,7 @@ public async Task Test1()
[Fact]
public async Task Test2()
{
var someService = _mesh.CreateProxy<ISomeService>();
var someService = _mesh!.CreateProxy<ISomeService>();

var res = await someService.GenericAdd(6m, 6m);

Expand All @@ -74,7 +77,7 @@ public async Task Test2()
[Fact]
public async Task Test3()
{
var someService = _mesh.CreateProxy<ISomeService>();
var someService = _mesh!.CreateProxy<ISomeService>();

var list = new List<SampleResponse>();

Expand Down
11 changes: 5 additions & 6 deletions Core.ServiceMesh/Internal/ConsumerRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ namespace Core.ServiceMesh.Internal;

internal class ConsumerRegistration
{
public string Name { get; init; }
public string Name { get; init; } = string.Empty;
public bool IsDurable { get; init; }
public string[] Subjects { get; init; }
public string Stream { get; init; }
public string[] Subjects { get; init; } = [];
public string Stream { get; init; } = string.Empty;
public string? QueueGroup { get; init; }
public Type Consumer { get; init; }
public FrozenDictionary<string, (MethodInfo Method, Type MessageType)> Methods { get; init; }
public Type Consumer { get; init; } = null!;
public FrozenDictionary<string, (MethodInfo Method, Type MessageType)> Methods { get; init; } = FrozenDictionary<string, (MethodInfo Method, Type MessageType)>.Empty;
public bool Obsolete { get; init; }

public DurableConsumerAttribute? Durable { get; init; }
}
22 changes: 11 additions & 11 deletions Core.ServiceMesh/Internal/ServiceMeshWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ private async Task TransientWorker(CancellationToken stoppingToken)

var parentContext = Propagators.DefaultTextMapPropagator.Extract(default, msg.Headers, (headers, key) =>
{
if (headers.TryGetValue(key, out var value))
if (headers!.TryGetValue(key, out var value))
return [value[0]];

return Array.Empty<string>();
Expand Down Expand Up @@ -447,7 +447,7 @@ private async Task ServiceWorker(CancellationToken stoppingToken)

var parentContext = Propagators.DefaultTextMapPropagator.Extract(default, msg.Headers, (headers, key) =>
{
if (headers.TryGetValue(key, out var value))
if (headers!.TryGetValue(key, out var value))
return [value[0]];

return Array.Empty<string>();
Expand Down Expand Up @@ -479,28 +479,28 @@ private async Task ServiceWorker(CancellationToken stoppingToken)

for (var i = 0; i < signatures.Length; i++)
{
var sig = signatures[i];
args[i] = options.Deserialize(invocation.Arguments[i], sig, false);
var sig = signatures[i]!;
args[i] = options.Deserialize(invocation.Arguments[i], sig, false)!;
}

await using var scope = serviceProvider.CreateAsyncScope();
var instance = scope.ServiceProvider.GetRequiredService(reg.ImplementationType);

if (method.ReturnType.GetGenericTypeDefinition() == typeof(IAsyncEnumerable<>))
{
var subId = msg.Headers["return-sub-id"].FirstOrDefault();
var subId = msg.Headers!["return-sub-id"].FirstOrDefault();

try
{
var resType = method.ReturnType.GenericTypeArguments[0];

var wrapper = typeof(ServiceMeshWorker).GetMethod(nameof(AsyncEnumerableWrapper),
BindingFlags.NonPublic | BindingFlags.Instance)
BindingFlags.NonPublic | BindingFlags.Instance)!
.MakeGenericMethod(resType);

dynamic stream = method.Invoke(instance, args.ToArray());
dynamic stream = method.Invoke(instance, args.ToArray())!;

dynamic awaitable = wrapper.Invoke(this, [subId, stream]);
dynamic awaitable = wrapper.Invoke(this, [subId, stream])!;
await awaitable;
}
catch (Exception ex)
Expand All @@ -510,7 +510,7 @@ private async Task ServiceWorker(CancellationToken stoppingToken)
["exception"] = ex.Message
};

await nats.PublishAsync<byte[]>(subId, [], headers);
await nats.PublishAsync<byte[]>(subId!, [], headers);
activity?.SetStatus(ActivityStatusCode.Error);
activity?.RecordException(ex);
}
Expand All @@ -523,7 +523,7 @@ private async Task ServiceWorker(CancellationToken stoppingToken)
method = method.MakeGenericMethod(
invocation.Generics.Select(options.ResolveType).ToArray()!);

dynamic awaitable = method.Invoke(instance, args.ToArray());
dynamic awaitable = method.Invoke(instance, args.ToArray())!;
await awaitable;

if (method.ReturnType == typeof(Task))
Expand Down Expand Up @@ -573,7 +573,7 @@ private async Task DurableWorker(CancellationToken stoppingToken)

var parentContext = Propagators.DefaultTextMapPropagator.Extract(default, msg.Headers, (headers, key) =>
{
if (headers.TryGetValue(key, out var value))
if (headers!.TryGetValue(key, out var value))
return [value[0]];

return Array.Empty<string>();
Expand Down
6 changes: 3 additions & 3 deletions Core.ServiceMesh/Internal/ServiceRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace Core.ServiceMesh.Internal;

internal class ServiceRegistration
{
public string Name { get; init; }
public string Sub { get; init; }
public string Name { get; init; } = string.Empty;
public string Sub { get; init; } = string.Empty;

//public string Subject { get; init; } = string.Empty;
public Type InterfaceType { get; init; } = null!;
public Type ImplementationType { get; init; } = null!;
public FrozenDictionary<string, MethodInfo> Methods { get; init; }
public FrozenDictionary<string, MethodInfo> Methods { get; init; } = FrozenDictionary<string, MethodInfo>.Empty;
public string QueueGroup { get; init; } = string.Empty;
}
4 changes: 2 additions & 2 deletions Core.ServiceMesh/ServiceMeshExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static IServiceCollection AddServiceMesh(this IServiceCollection services
{
var subject =
applyPrefix(
$"{attr.Name}.{method.Name}.G{method.GetGenericArguments().Length}P{method.GetParameters().Length}");
$"{attr!.Name}.{method.Name}.G{method.GetGenericArguments().Length}P{method.GetParameters().Length}");

if (subject == null)
continue;
Expand All @@ -79,7 +79,7 @@ public static IServiceCollection AddServiceMesh(this IServiceCollection services

Services.Add(new ServiceRegistration
{
Name = attr.Name,
Name = attr!.Name,
Sub = applyPrefix(attr.Name)!,
InterfaceType = itype,
ImplementationType = type,
Expand Down
2 changes: 1 addition & 1 deletion SampleApp/Services/AnotherService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace SampleApp.Services;

[ServiceMesh]
public class AnotherService(ILogger<AnotherService> logger) : IAnotherService
public class AnotherService : IAnotherService
{
public ValueTask<SampleResponse> SampleA(SampleRequest request)
{
Expand Down

0 comments on commit f2ddf93

Please sign in to comment.