Skip to content

Commit 2bcd398

Browse files
authored
Add SetFromFile and SetFromDirectory Commands for Json Files (#40)
* Start working on JSON SetFile * Add SetFile to IJsonCommands * Fix TestSetFile * Add SetFileAsync + Test * Fix Documentation * Add SetFiles command * Naming * Add TODO * Add File Exsistence
1 parent aab0b67 commit 2bcd398

File tree

3 files changed

+343
-68
lines changed

3 files changed

+343
-68
lines changed

src/NRedisStack/Json/IJsonCommands.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,27 @@ public interface IJsonCommands
205205
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
206206
bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When.Always);
207207

208+
/// <summary>
209+
/// Set json file from the provided file Path.
210+
/// </summary>
211+
/// <param name="key">The key.</param>
212+
/// <param name="path">The path to set within the key.</param>
213+
/// <param name="filePath">The path of the file to set.</param>
214+
/// <param name="when">When to set the value.</param>
215+
/// <returns>The disposition of the command</returns>
216+
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
217+
bool SetFromFile(RedisKey key, RedisValue path, string filePath, When when = When.Always);
218+
219+
/// <summary>
220+
/// Set all json files in the provided file Path.
221+
/// </summary>
222+
/// <param name="path">The path to set within the file name as key.</param>
223+
/// <param name="filesPath">The path of the file to set.</param>
224+
/// <param name="when">When to set the value.</param>
225+
/// <returns>The number of files that have been set</returns>
226+
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
227+
int SetFromDirectory(RedisValue path, string filesPath, When when = When.Always);
228+
208229
/// <summary>
209230
/// Appends the provided string to the string(s) at the provided path.
210231
/// </summary>
@@ -451,6 +472,27 @@ public interface IJsonCommands
451472
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
452473
Task<bool> SetAsync(RedisKey key, RedisValue path, RedisValue json, When when = When.Always);
453474

475+
/// <summary>
476+
/// Set json file from the provided file Path.
477+
/// </summary>
478+
/// <param name="key">The key.</param>
479+
/// <param name="path">The path to set within the key.</param>
480+
/// <param name="filePath">The path of the file to set.</param>
481+
/// <param name="when">When to set the value.</param>
482+
/// <returns>The disposition of the command</returns>
483+
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
484+
Task<bool> SetFromFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always);
485+
486+
/// <summary>
487+
/// Set all json files in the provided file Path.
488+
/// </summary>
489+
/// <param name="path">The path to set within the file name as key.</param>
490+
/// <param name="filesPath">The path of the file to set.</param>
491+
/// <param name="when">When to set the value.</param>
492+
/// <returns>The number of files that have been set</returns>
493+
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
494+
Task<int> SetFromDirectoryAsync(RedisValue path, string filesPath, When when = When.Always);
495+
454496
/// <summary>
455497
/// Appends the provided string to the string(s) at the provided path.
456498
/// </summary>

src/NRedisStack/Json/JsonCommands.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,41 @@ public bool Set(RedisKey key, RedisValue path, RedisValue json, When when = When
6060
return true;
6161
}
6262

63+
/// <inheritdoc/>
64+
public bool SetFromFile(RedisKey key, RedisValue path, string filePath, When when = When.Always)
65+
{
66+
if(!File.Exists(filePath))
67+
{
68+
throw new FileNotFoundException($"File {filePath} not found.");
69+
}
70+
71+
string fileContent = File.ReadAllText(filePath);
72+
return Set(key, path, fileContent, when);
73+
}
74+
75+
/// <inheritdoc/>
76+
public int SetFromDirectory(RedisValue path, string filesPath, When when = When.Always)
77+
{
78+
int inserted = 0;
79+
string key;
80+
var files = Directory.EnumerateFiles(filesPath, "*.json");
81+
foreach (var filePath in files)
82+
{
83+
key = filePath.Substring(0, filePath.IndexOf("."));
84+
if(SetFromFile(key, path, filePath, when))
85+
{
86+
inserted++;
87+
}
88+
}
89+
90+
foreach (var dirPath in Directory.EnumerateDirectories(filesPath))
91+
{
92+
inserted += SetFromDirectory(path, dirPath, when);
93+
}
94+
95+
return inserted;
96+
}
97+
6398
/// <inheritdoc/>
6499
public long?[] StrAppend(RedisKey key, string value, string? path = null)
65100
{
@@ -402,6 +437,42 @@ public async Task<bool> SetAsync(RedisKey key, RedisValue path, RedisValue json,
402437
return true;
403438
}
404439

440+
/// <inheritdoc/> // TODO: check way asnyc methods dont have documenation
441+
public async Task<bool> SetFromFileAsync(RedisKey key, RedisValue path, string filePath, When when = When.Always)
442+
{
443+
if (!File.Exists(filePath))
444+
{
445+
throw new FileNotFoundException($"File {filePath} not found.");
446+
}
447+
448+
string fileContent = File.ReadAllText(filePath);
449+
return await SetAsync(key, path, fileContent, when);
450+
}
451+
452+
/// <inheritdoc/>
453+
public async Task<int> SetFromDirectoryAsync(RedisValue path, string filesPath, When when = When.Always)
454+
{
455+
int inserted = 0;
456+
string key;
457+
var files = Directory.EnumerateFiles(filesPath, "*.json");
458+
foreach (var filePath in files)
459+
{
460+
key = filePath.Substring(0, filePath.IndexOf("."));
461+
if(await SetFromFileAsync(key, path, filePath, when))
462+
{
463+
inserted++;
464+
}
465+
}
466+
467+
foreach (var dirPath in Directory.EnumerateDirectories(filesPath))
468+
{
469+
inserted += await SetFromDirectoryAsync(path, dirPath, when);
470+
}
471+
472+
return inserted;
473+
}
474+
475+
405476
public async Task<long?[]> StrAppendAsync(RedisKey key, string value, string? path = null)
406477
{
407478
RedisResult result;

0 commit comments

Comments
 (0)