Skip to content

Commit

Permalink
chore/fmt: Switch from dotnet-format to csharpier (#189)
Browse files Browse the repository at this point in the history
Co-authored-by: Konstantin <konstantin.klein+github@hochfrequenz.de>
  • Loading branch information
hf-kklein and Konstantin authored Oct 1, 2024
1 parent 00b149a commit 807e2a9
Show file tree
Hide file tree
Showing 29 changed files with 2,219 additions and 882 deletions.
11 changes: 11 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": 1,
"isRoot": true,
"tools": {
"csharpier": {
"version": "0.29.2",
"commands": ["dotnet-csharpier"],
"rollForward": false
}
}
}
15 changes: 7 additions & 8 deletions .github/workflows/formatter.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
name: dotnet-format
name: csharpier

on: [push, pull_request]
on: [pull_request]

jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup .NET 5
- name: Setup .NET 8
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.100
- name: Install dotnet-format
run: |
dotnet tool install -g dotnet-format
- name: Run dotnet-format
run: dotnet-format EDILibrary.sln --check
- name: Restore .NET tools
run: dotnet tool restore
- name: Run CSharpier
run: dotnet csharpier EDILibrary.sln --check
157 changes: 116 additions & 41 deletions EDIFileLoader/AzureStorageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

using Azure.Storage.Blobs;

using EDILibrary;

namespace EDIFileLoader
Expand All @@ -23,16 +21,23 @@ public class AzureStorageLoader : EDILibrary.Interfaces.ITemplateLoader
protected string _containerName;
protected string _connectionString;


protected BlobServiceClient _blobClient;
protected BlobContainerClient _container;

protected ConcurrentDictionary<string, Dictionary<string, string>> Cache { get; set; } = new ConcurrentDictionary<string, Dictionary<string, string>>();
protected ConcurrentDictionary<string, Dictionary<string, string>> Cache { get; set; } =
new ConcurrentDictionary<string, Dictionary<string, string>>();

/// <summary>
/// JsonSerializer options
/// </summary>
protected JsonSerializerOptions JsonOptions { get; set; }
public AzureStorageLoader(string accountName, string accountKey, string containerName, string connectionString)

public AzureStorageLoader(
string accountName,
string accountKey,
string containerName,
string connectionString
)
{
_accountKey = accountKey;
_accountName = accountName;
Expand All @@ -50,55 +55,100 @@ public AzureStorageLoader(string accountName, string accountKey, string containe
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
}

public async Task PreloadCache()
{
var tasks = new ConcurrentBag<Task>();
await foreach (var prefixPage in _container.GetBlobsByHierarchyAsync(Azure.Storage.Blobs.Models.BlobTraits.Metadata, Azure.Storage.Blobs.Models.BlobStates.None, "/").AsPages())
await foreach (
var prefixPage in _container
.GetBlobsByHierarchyAsync(
Azure.Storage.Blobs.Models.BlobTraits.Metadata,
Azure.Storage.Blobs.Models.BlobStates.None,
"/"
)
.AsPages()
)
{
foreach (var prefix in prefixPage.Values)
{
tasks.Add(Task.Run(async () =>
{
if (prefix.IsPrefix)
tasks.Add(
Task.Run(async () =>
{
Cache.TryAdd(prefix.Prefix.TrimEnd('/'), new Dictionary<string, string>());
await foreach (var blobPage in _container.GetBlobsByHierarchyAsync(Azure.Storage.Blobs.Models.BlobTraits.Metadata, Azure.Storage.Blobs.Models.BlobStates.None, null, prefix.Prefix).AsPages())
if (prefix.IsPrefix)
{
foreach (var blob in blobPage.Values)
Cache.TryAdd(
prefix.Prefix.TrimEnd('/'),
new Dictionary<string, string>()
);
await foreach (
var blobPage in _container
.GetBlobsByHierarchyAsync(
Azure.Storage.Blobs.Models.BlobTraits.Metadata,
Azure.Storage.Blobs.Models.BlobStates.None,
null,
prefix.Prefix
)
.AsPages()
)
{
if (blob.IsBlob)
foreach (var blob in blobPage.Values)
{
var blockBlob = _container.GetBlobClient(blob.Blob.Name);
// if this stream reader failes, because the blockBlob comes without a ByteOrderMark, then use new UTF8Encoding(false) as encoding
var text = await new StreamReader((await blockBlob.DownloadAsync()).Value.Content, Encoding.UTF8).ReadToEndAsync();
text = EDIHelper.RemoveByteOrderMark(text);
Cache[prefix.Prefix.TrimEnd('/')].TryAdd(blob.Blob.Name, text);
if (blob.IsBlob)
{
var blockBlob = _container.GetBlobClient(
blob.Blob.Name
);
// if this stream reader failes, because the blockBlob comes without a ByteOrderMark, then use new UTF8Encoding(false) as encoding
var text = await new StreamReader(
(await blockBlob.DownloadAsync()).Value.Content,
Encoding.UTF8
).ReadToEndAsync();
text = EDIHelper.RemoveByteOrderMark(text);
Cache[prefix.Prefix.TrimEnd('/')]
.TryAdd(blob.Blob.Name, text);
}
}
}
}
}
}));
})
);
}
}
await Task.WhenAll(tasks);
}

public async Task<string> LoadEDITemplate(EDIFileInfo info, string type)
{
if (Cache != null)
{
try
{
return Cache["edi"][Path.Combine("edi", info.Format.ToString(), info.Format.ToString() + info.Version + "." + type).Replace("\\", "/")];
}
catch (KeyNotFoundException)
{
return Cache["edi"][
Path.Combine(
"edi",
info.Format.ToString(),
info.Format.ToString() + info.Version + "." + type
)
.Replace("\\", "/")
];
}
catch (KeyNotFoundException) { }
}
var blockBlob = _container.GetBlobClient(Path.Combine("edi", info.Format.ToString(), info.Format.ToString() + info.Version + "." + type));
var text = await new StreamReader((await blockBlob.DownloadAsync()).Value.Content, Encoding.UTF8).ReadToEndAsync();
var blockBlob = _container.GetBlobClient(
Path.Combine(
"edi",
info.Format.ToString(),
info.Format.ToString() + info.Version + "." + type
)
);
var text = await new StreamReader(
(await blockBlob.DownloadAsync()).Value.Content,
Encoding.UTF8
).ReadToEndAsync();
text = EDIHelper.RemoveByteOrderMark(text);
return text;
}

public Task<string> LoadJSONTemplate(string fileName)
{
throw new NotSupportedException();
Expand All @@ -112,44 +162,69 @@ public async Task<string> LoadJSONTemplate(string formatPackage, string fileName
return await LoadJSONTemplate(format, version, fileName);
}

public async Task<string> LoadJSONTemplate(EdifactFormat? format, string version, string fileName)
public async Task<string> LoadJSONTemplate(
EdifactFormat? format,
string version,
string fileName
)
{
if (Cache != null)
{
try
{
return Cache["edi"][Path.Combine(version.Replace("/", ""), fileName.Replace("\\", "/"))];
}
catch (KeyNotFoundException)
{
return Cache["edi"][
Path.Combine(version.Replace("/", ""), fileName.Replace("\\", "/"))
];
}
catch (KeyNotFoundException) { }
}
var blockBlob = _container.GetBlobClient(Path.Combine(version.Replace("/", ""), fileName).Replace("\\", "/"));
var blockBlob = _container.GetBlobClient(
Path.Combine(version.Replace("/", ""), fileName).Replace("\\", "/")
);

var text = await new StreamReader((await blockBlob.DownloadAsync()).Value.Content, Encoding.UTF8).ReadToEndAsync();
var text = await new StreamReader(
(await blockBlob.DownloadAsync()).Value.Content,
Encoding.UTF8
).ReadToEndAsync();
text = EDIHelper.RemoveByteOrderMark(text);
return text;
}

/// <summary>
/// <see cref="EDILibrary.Interfaces.TemplateLoader.LoadMausTemplate"/>
/// </summary>
public async Task<EDILibrary.MAUS.Anwendungshandbuch> LoadMausTemplate(EdifactFormat? format, EdifactFormatVersion version, string pid)
public async Task<EDILibrary.MAUS.Anwendungshandbuch> LoadMausTemplate(
EdifactFormat? format,
EdifactFormatVersion version,
string pid
)
{
if (Cache != null)
{
try
{
return JsonSerializer.Deserialize<EDILibrary.MAUS.Anwendungshandbuch>(Cache["maus"][Path.Combine(version.ToString(), format.ToString(), pid + "_maus.json")], JsonOptions);
}
catch (KeyNotFoundException)
{
return JsonSerializer.Deserialize<EDILibrary.MAUS.Anwendungshandbuch>(
Cache["maus"][
Path.Combine(version.ToString(), format.ToString(), pid + "_maus.json")
],
JsonOptions
);
}
catch (KeyNotFoundException) { }
}
var blockBlob = _container.GetBlobClient(Path.Combine("maus", version.ToString(), format.ToString(), pid + "_maus.json"));
var blockBlob = _container.GetBlobClient(
Path.Combine("maus", version.ToString(), format.ToString(), pid + "_maus.json")
);

var text = await new StreamReader((await blockBlob.DownloadAsync()).Value.Content, Encoding.UTF8).ReadToEndAsync();
var text = await new StreamReader(
(await blockBlob.DownloadAsync()).Value.Content,
Encoding.UTF8
).ReadToEndAsync();
text = EDIHelper.RemoveByteOrderMark(text);
return JsonSerializer.Deserialize<EDILibrary.MAUS.Anwendungshandbuch>(text, JsonOptions);
return JsonSerializer.Deserialize<EDILibrary.MAUS.Anwendungshandbuch>(
text,
JsonOptions
);
}
}
}
Loading

0 comments on commit 807e2a9

Please sign in to comment.