Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
xusterlg committed Dec 31, 2024
1 parent 31eec52 commit b7336a3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 169 deletions.
25 changes: 0 additions & 25 deletions src/KeyValuePush.Redis copy/AutoGuru.KeyValuePush.Redis.csproj

This file was deleted.

91 changes: 0 additions & 91 deletions src/KeyValuePush.Redis copy/Program.cs

This file was deleted.

8 changes: 0 additions & 8 deletions src/KeyValuePush.Redis copy/Properties/launchSettings.json

This file was deleted.

45 changes: 0 additions & 45 deletions src/KeyValuePush.Redis copy/RedisPusher.cs

This file was deleted.

70 changes: 70 additions & 0 deletions src/KeyValuePush/DefaultDictionaryBuilder2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace AutoGuru.KeyValuePush
{
public class DefaultDictionaryBuilder2 : IDictionaryBuilder
{
public async Task<IDictionary<string, string>> BuildAsync(
string path,
string searchPattern,
SearchOption searchOption,
bool recurseIntoJsonFiles,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

var filePaths = Directory.GetFiles(path, searchPattern, searchOption);
var dict = new Dictionary<string, string>(filePaths.Length);

foreach (var filePath in filePaths)
{
cancellationToken.ThrowIfCancellationRequested();

if (Path.GetExtension(filePath).Contains(".json") &&
recurseIntoJsonFiles)
{
// Read file as JSON and extract kvps
var bytes = await File.ReadAllBytesAsync(filePath, cancellationToken);
var innerDict = JsonSerializer.Deserialize<Dictionary<string, string>>(bytes);
if (innerDict is null)
{
throw new Exception($"Problem reading {filePath} as dictionary.");

Check warning on line 36 in src/KeyValuePush/DefaultDictionaryBuilder2.cs

View workflow job for this annotation

GitHub Actions / Analyze with SonarCloud

'System.Exception' should not be thrown by user code. (https://rules.sonarsource.com/csharp/RSPEC-112)

Check warning on line 36 in src/KeyValuePush/DefaultDictionaryBuilder2.cs

View workflow job for this annotation

GitHub Actions / Analyze with SonarCloud

'System.Exception' should not be thrown by user code. (https://rules.sonarsource.com/csharp/RSPEC-112)
}

foreach (var kvp in innerDict)
{
TryAdd2(dict, kvp.Key, kvp.Value);
}
}
else
{
var fileName = Path.GetFileNameWithoutExtension(filePath);
var text = await File.ReadAllTextAsync(filePath, cancellationToken);
TryAdd2(dict, fileName, text);
}
}

return dict;
}

public static void TryAdd2(IDictionary<string, string> dict, string key, string value)
{
if (dict.ContainsKey(key))
{
if (dict[key] == value)
{
return;
}

throw new Exception($"Duplicate key of '{key}' with a different value detected.");

Check warning on line 64 in src/KeyValuePush/DefaultDictionaryBuilder2.cs

View workflow job for this annotation

GitHub Actions / Analyze with SonarCloud

'System.Exception' should not be thrown by user code. (https://rules.sonarsource.com/csharp/RSPEC-112)

Check warning on line 64 in src/KeyValuePush/DefaultDictionaryBuilder2.cs

View workflow job for this annotation

GitHub Actions / Analyze with SonarCloud

'System.Exception' should not be thrown by user code. (https://rules.sonarsource.com/csharp/RSPEC-112)
}

dict.Add(key, value);
}
}
}

0 comments on commit b7336a3

Please sign in to comment.