Skip to content

Commit

Permalink
Merge pull request #62 from LSViana/stylecop-everywhere
Browse files Browse the repository at this point in the history
Add StyleCop everywhere
  • Loading branch information
LSViana authored Nov 16, 2023
2 parents 749baae + a7de397 commit 40eebd6
Show file tree
Hide file tree
Showing 63 changed files with 316 additions and 263 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dotnet_diagnostic.SA1413.severity = suggestion # Use trailing comma in multi-lin
dotnet_diagnostic.SA1500.severity = suggestion # Braces for multi-line statements should not share line

# Documentation rules
dotnet_diagnostic.SA1600.severity = suggestion # Elements should be documented
dotnet_diagnostic.SA1600.severity = none # Elements should be documented
dotnet_diagnostic.SA1601.severity = suggestion # Partial elements should be documented
dotnet_diagnostic.SA1602.severity = suggestion # Enumeration items should be documented
dotnet_diagnostic.SA1633.severity = none # File should have header
Expand Down
2 changes: 1 addition & 1 deletion Demo/Callback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ await @event.Source.UpdateDocAsync(notificationCtx, (doc) =>
}
});
});


return default;
}
Expand Down
4 changes: 2 additions & 2 deletions Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public static void Main(string[] args)

builder.Services.AddControllers();
builder.Services.AddRazorPages();
var yDotNet =

var yDotNet =
builder.Services.AddYDotNet()
.AddCallback<Callback>()
.AddWebSockets();
Expand Down
2 changes: 1 addition & 1 deletion Tests/YDotNet.Tests.Unit/Protocol/WriteAndRead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public async Task EncodeAndDecodeBytes(int length)
public async Task DecodeJsSample()
{
// Arrange
var buffer = new byte[]
var buffer = new byte[]
{
1,
252,
Expand Down
17 changes: 16 additions & 1 deletion YDotNet.Extensions/YDotNet.Extensions.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand All @@ -10,4 +10,19 @@
<ProjectReference Include="..\YDotNet\YDotNet.csproj" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="..\stylecop.json" Link="stylecop.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.110">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions YDotNet.Extensions/YDotNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public static string ToJson(this Output output, Transaction transaction)
return Encoding.UTF8.GetString(jsonStream.ToArray());
}

#pragma warning disable MA0051 // Method is too long
public static void ToJson(this Output output, Stream stream, Transaction transaction)
#pragma warning restore MA0051 // Method is too long
{
var jsonWriter = new Utf8JsonWriter(stream);

Expand Down
19 changes: 9 additions & 10 deletions YDotNet.Server.MongoDB/MongoDocumentStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace YDotNet.Server.MongoDB;

public sealed class MongoDocumentStorage : IDocumentStorage, IHostedService
{
private readonly UpdateOptions Upsert = new() { IsUpsert = true };
private readonly UpdateOptions upsert = new() { IsUpsert = true };
private readonly MongoDocumentStorageOptions options;
private readonly IMongoClient mongoClient;
private IMongoCollection<DocumentEntity>? collection;
Expand All @@ -34,7 +34,7 @@ await collection.Indexes.CreateOneAsync(
{
ExpireAfter = TimeSpan.Zero
}),
cancellationToken: cancellationToken);
cancellationToken: cancellationToken).ConfigureAwait(false);
}

public Task StopAsync(
Expand All @@ -43,33 +43,32 @@ public Task StopAsync(
return Task.CompletedTask;
}

public async ValueTask<byte[]?> GetDocAsync(string name,
CancellationToken ct = default)
public async ValueTask<byte[]?> GetDocAsync(string name, CancellationToken ct = default)
{
if (collection == null)
{
return null;
}

var document = await collection.Find(x => x.Id == name).FirstOrDefaultAsync(ct);
var document = await collection.Find(x => x.Id == name).FirstOrDefaultAsync(ct).ConfigureAwait(false);

return document?.Data;
}

public async ValueTask StoreDocAsync(string name, byte[] doc,
CancellationToken ct = default)
public async ValueTask StoreDocAsync(string name, byte[] doc, CancellationToken ct = default)
{
if (collection == null)
{
return;
}

await collection.UpdateOneAsync(x => x.Id == name,
await collection.UpdateOneAsync(
x => x.Id == name,
Builders<DocumentEntity>.Update
.Set(x => x.Data, doc)
.Set(x => x.Expiration, GetExpiration(name)),
Upsert,
ct);
upsert,
ct).ConfigureAwait(false);
}

private DateTime? GetExpiration(string name)
Expand Down
2 changes: 1 addition & 1 deletion YDotNet.Server.MongoDB/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static YDotnetRegistration AddMongoStorage(this YDotnetRegistration regis
{
registration.Services.Configure(configure ?? (x => { }));
registration.Services.AddSingleton<MongoDocumentStorage>();

registration.Services.AddSingleton<IDocumentStorage>(
c => c.GetRequiredService<MongoDocumentStorage>());

Expand Down
10 changes: 9 additions & 1 deletion YDotNet.Server.MongoDB/YDotNet.Server.MongoDB.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand All @@ -7,7 +7,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.110">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions YDotNet.Server.Redis/RedisConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace YDotNet.Server.Redis;

public sealed class RedisConnection : IDisposable
{
public Task<IConnectionMultiplexer> Instance { get; }

public RedisConnection(IOptions<RedisOptions> options, ILogger<RedisConnection> logger)
{
Instance = options.Value.ConnectAsync(new LoggerTextWriter(logger));
}

public Task<IConnectionMultiplexer> Instance { get; }

public void Dispose()
{
if (Instance.IsCompletedSuccessfully)
Expand Down
6 changes: 3 additions & 3 deletions YDotNet.Server.Redis/RedisDocumentStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public RedisDocumentStorage(IOptions<RedisDocumentStorageOptions> redisOptions,
private async Task InitializeAsync(RedisConnection redisConnection)
{
// Use a single task, so that the ordering of registrations does not matter.
var connection = await redisConnection.Instance;
var connection = await redisConnection.Instance.ConfigureAwait(false);

database = connection.GetDatabase(redisOptions.Database);
}
Expand All @@ -32,7 +32,7 @@ private async Task InitializeAsync(RedisConnection redisConnection)
return null;
}

var item = await database.StringGetAsync(Key(name));
var item = await database.StringGetAsync(Key(name)).ConfigureAwait(false);

if (item == RedisValue.Null)
{
Expand All @@ -50,7 +50,7 @@ public async ValueTask StoreDocAsync(string name, byte[] doc,
return;
}

await database.StringSetAsync(Key(name), doc, redisOptions.Expiration?.Invoke(name));
await database.StringSetAsync(Key(name), doc, redisOptions.Expiration?.Invoke(name)).ConfigureAwait(false);
}

private string Key(string name)
Expand Down
4 changes: 2 additions & 2 deletions YDotNet.Server.Redis/RedisOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ internal async Task<IConnectionMultiplexer> ConnectAsync(TextWriter log)
{
if (ConnectionFactory != null)
{
return await ConnectionFactory(log);
return await ConnectionFactory(log).ConfigureAwait(false);
}

if (Configuration != null)
{
return await ConnectionMultiplexer.ConnectAsync(Configuration, log);
return await ConnectionMultiplexer.ConnectAsync(Configuration, log).ConfigureAwait(false);
}

throw new InvalidOperationException("Either configuration or connection factory must be set.");
Expand Down
14 changes: 13 additions & 1 deletion YDotNet.Server.Redis/YDotNet.Server.Redis.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand All @@ -7,12 +7,24 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.110">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\YDotNet.Server\YDotNet.Server.csproj" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="..\stylecop.json" Link="stylecop.json" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions YDotNet.Server.WebSockets/ClientState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public sealed class ClientState : IDisposable

public async Task WriteLockedAsync<T>(T state, Func<WebSocketEncoder, T, ClientState, CancellationToken, Task> action, CancellationToken ct)
{
await slimLock.WaitAsync(ct);
await slimLock.WaitAsync(ct).ConfigureAwait(false);
try
{
await action(Encoder, state, this, ct);
await action(Encoder, state, this, ct).ConfigureAwait(false);
}
finally
{
Expand All @@ -38,6 +38,6 @@ public void Dispose()
WebSocket.Dispose();

Encoder.Dispose();
Decoder.Dispose();
Decoder.Dispose();
}
}
58 changes: 27 additions & 31 deletions YDotNet.Server.WebSockets/EncoderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,36 @@ namespace YDotNet.Server.WebSockets;

public static class EncoderExtensions
{
public static async Task WriteSyncStep1Async(this WebSocketEncoder encoder, byte[] stateVector,
CancellationToken ct)
public static async Task WriteSyncStep1Async(this WebSocketEncoder encoder, byte[] stateVector, CancellationToken ct)
{
await encoder.WriteVarUintAsync(MessageTypes.TypeSync, ct);
await encoder.WriteVarUintAsync(MessageTypes.SyncStep1, ct);
await encoder.WriteVarUint8Array(stateVector, ct);
await encoder.FlushAsync(ct);
await encoder.WriteVarUintAsync(MessageTypes.TypeSync, ct).ConfigureAwait(false);
await encoder.WriteVarUintAsync(MessageTypes.SyncStep1, ct).ConfigureAwait(false);
await encoder.WriteVarUint8Array(stateVector, ct).ConfigureAwait(false);
await encoder.FlushAsync(ct).ConfigureAwait(false);
}

public static async Task WriteSyncStep2Async(this WebSocketEncoder encoder, byte[] update,
CancellationToken ct)
public static async Task WriteSyncStep2Async(this WebSocketEncoder encoder, byte[] update, CancellationToken ct)
{
await encoder.WriteVarUintAsync(MessageTypes.TypeSync, ct);
await encoder.WriteVarUintAsync(MessageTypes.SyncStep2, ct);
await encoder.WriteVarUint8Array(update, ct);
await encoder.FlushAsync(ct);
await encoder.WriteVarUintAsync(MessageTypes.TypeSync, ct).ConfigureAwait(false);
await encoder.WriteVarUintAsync(MessageTypes.SyncStep2, ct).ConfigureAwait(false);
await encoder.WriteVarUint8Array(update, ct).ConfigureAwait(false);
await encoder.FlushAsync(ct).ConfigureAwait(false);
}

public static async Task WriteSyncUpdateAsync(this WebSocketEncoder encoder, byte[] update,
CancellationToken ct)
public static async Task WriteSyncUpdateAsync(this WebSocketEncoder encoder, byte[] update, CancellationToken ct)
{
await encoder.WriteVarUintAsync(MessageTypes.TypeSync, ct);
await encoder.WriteVarUintAsync(MessageTypes.SyncUpdate, ct);
await encoder.WriteVarUint8Array(update, ct);
await encoder.FlushAsync(ct);
await encoder.WriteVarUintAsync(MessageTypes.TypeSync, ct).ConfigureAwait(false);
await encoder.WriteVarUintAsync(MessageTypes.SyncUpdate, ct).ConfigureAwait(false);
await encoder.WriteVarUint8Array(update, ct).ConfigureAwait(false);
await encoder.FlushAsync(ct).ConfigureAwait(false);
}

public static async Task WriteAuthErrorAsync(this WebSocketEncoder encoder, string reason,
CancellationToken ct)
public static async Task WriteAuthErrorAsync(this WebSocketEncoder encoder, string reason, CancellationToken ct)
{
await encoder.WriteVarUintAsync(MessageTypes.TypeAuth, ct);
await encoder.WriteVarUintAsync(0, ct);
await encoder.WriteVarStringAsync(reason, ct);
await encoder.FlushAsync(ct);
await encoder.WriteVarUintAsync(MessageTypes.TypeAuth, ct).ConfigureAwait(false);
await encoder.WriteVarUintAsync(0, ct).ConfigureAwait(false);
await encoder.WriteVarStringAsync(reason, ct).ConfigureAwait(false);
await encoder.FlushAsync(ct).ConfigureAwait(false);
}

public static async Task WriteAwarenessAsync(this WebSocketEncoder encoder, (ulong ClientId, ulong Clock, string? State)[] clients,
Expand All @@ -48,20 +44,20 @@ public static async Task WriteAwarenessAsync(this WebSocketEncoder encoder, (ulo
return;
}

await encoder.WriteVarUintAsync(MessageTypes.TypeAwareness, ct);
await encoder.WriteVarUintAsync(MessageTypes.TypeAwareness, ct).ConfigureAwait(false);

var buffer = new BufferEncoder();

await buffer.WriteVarUintAsync((ulong)clients.Length, ct);
await buffer.WriteVarUintAsync((ulong)clients.Length, ct).ConfigureAwait(false);

foreach (var (clientId, clock, state) in clients)
{
await buffer.WriteVarUintAsync(clientId, ct);
await buffer.WriteVarUintAsync(clock, ct);
await buffer.WriteVarStringAsync(state ?? string.Empty, ct);
await buffer.WriteVarUintAsync(clientId, ct).ConfigureAwait(false);
await buffer.WriteVarUintAsync(clock, ct).ConfigureAwait(false);
await buffer.WriteVarStringAsync(state ?? string.Empty, ct).ConfigureAwait(false);
}

await encoder.WriteVarUint8Array(buffer.ToArray(), ct);
await encoder.FlushAsync(ct);
await encoder.WriteVarUint8Array(buffer.ToArray(), ct).ConfigureAwait(false);
await encoder.FlushAsync(ct).ConfigureAwait(false);
}
}
Loading

0 comments on commit 40eebd6

Please sign in to comment.