-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
70 additions
and
169 deletions.
There are no files selected for viewing
25 changes: 0 additions & 25 deletions
25
src/KeyValuePush.Redis copy/AutoGuru.KeyValuePush.Redis.csproj
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
} | ||
|
||
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
|
||
} | ||
|
||
dict.Add(key, value); | ||
} | ||
} | ||
} |