Skip to content

Commit d9fa073

Browse files
committed
Minor cleanup from last commit
1 parent 95a20fb commit d9fa073

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

ProtectionScan/Features/MainFeature.cs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -255,21 +255,19 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
255255

256256
try
257257
{
258-
// Attempt to open a protection file for writing
259-
using var jsw = new StreamWriter(File.OpenWrite($"protection-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}.json"));
260-
261258
var jsonSerializerOptions = new System.Text.Json.JsonSerializerOptions { WriteIndented = true };
262259
string serializedData;
260+
263261
if (Nested)
264262
{
265263
// A nested dictionary is used to achieve proper serialization.
266264
var nestedDictionary = new Dictionary<string, object>();
267-
var trimmedPath = path.TrimEnd(['\\', '/']);
268-
265+
var trimmedPath = path.TrimEnd(['\\', '/']);
266+
269267
// Sort the keys for consistent output
270268
string[] keys = [.. protections.Keys];
271269
Array.Sort(keys);
272-
270+
273271
var modifyNodeList = new List<(Dictionary<string, object>, string, string[])>();
274272

275273
// Loop over all keys
@@ -285,17 +283,17 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
285283
Array.Sort(fileProtections);
286284

287285
// Inserts key and protections into nested dictionary, with the key trimmed of the base path.
288-
InsertNode(nestedDictionary, key.Substring(trimmedPath.Length), fileProtections, modifyNodeList);
286+
InsertNode(nestedDictionary, key[trimmedPath.Length..], fileProtections, modifyNodeList);
289287
}
290288

291289
// Adds the non-leaf-node protections back in
292290
for (int i = 0; i < modifyNodeList.Count; i++)
293291
{
294-
var copyDictionary = modifyNodeList[i].Item1[modifyNodeList[i].Item2];
295-
296-
var modifyNode = new List<object>();
297-
modifyNode.Add(modifyNodeList[i].Item3);
298-
modifyNode.Add(copyDictionary);
292+
List<object> modifyNode =
293+
[
294+
modifyNodeList[i].Item3,
295+
modifyNodeList[i].Item1[modifyNodeList[i].Item2],
296+
];
299297

300298
modifyNodeList[i].Item1[modifyNodeList[i].Item2] = modifyNode;
301299
}
@@ -305,7 +303,7 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
305303
{
306304
{trimmedPath, nestedDictionary}
307305
};
308-
306+
309307
// Create the output data
310308
serializedData = System.Text.Json.JsonSerializer.Serialize(finalDictionary, jsonSerializerOptions);
311309
}
@@ -316,7 +314,8 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
316314
}
317315

318316
// Write the output data
319-
// TODO: this prints plus symbols wrong, probably some other things too
317+
// TODO: This prints plus symbols wrong, probably some other things too
318+
using var jsw = new StreamWriter(File.OpenWrite($"protection-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}.json"));
320319
jsw.WriteLine(serializedData);
321320
jsw.Flush();
322321
}
@@ -335,36 +334,35 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
335334
/// <param name="protections">The scanned protection(s) for a given file</param>
336335
public static void InsertNode(Dictionary<string, object> nestedDictionary, string path, string[] protections, List<(Dictionary<string, object>, string, string[])> modifyNodeList)
337336
{
338-
var current = nestedDictionary;
339-
var pathParts = path.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries);
337+
var current = nestedDictionary;
338+
var pathParts = path.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries);
340339

341340
// Traverses the nested dictionary until the "leaf" dictionary is reached.
342341
for (int i = 0; i < pathParts.Length - 1; i++)
343342
{
344343
var part = pathParts[i];
345-
344+
346345
// Inserts new subdictionaries if one doesn't already exist
347346
if (!current.ContainsKey(part))
348347
{
349348
var innerDictionary = new Dictionary<string, object>();
350349
current[part] = innerDictionary;
351-
current = innerDictionary;
350+
current = innerDictionary;
352351
continue;
353352
}
354-
355-
var innerObject = current[part];
356-
353+
357354
// Handle instances where a protection was already assigned to the current node
355+
var innerObject = current[part];
358356
if (innerObject is string[] existingProtections)
359357
{
360358
modifyNodeList.Add((current, part, existingProtections));
361359
innerObject = new Dictionary<string, object>();
362360
}
363-
361+
364362
current[part] = innerObject;
365-
current = (Dictionary<string, object>)current[part];
363+
current = (Dictionary<string, object>)current[part];
366364
}
367-
365+
368366
// If the "leaf" dictionary has been reached, add the file and its protections.
369367
current.Add(pathParts[^1], protections);
370368
}

0 commit comments

Comments
 (0)