Skip to content

Commit c7a091a

Browse files
author
Kapil Borle
committed
Extract rules from settings hashtable type input
1 parent 28caa63 commit c7a091a

1 file changed

Lines changed: 67 additions & 28 deletions

File tree

Engine/ScriptAnalyzer.cs

Lines changed: 67 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -412,18 +412,19 @@ private Dictionary<string, object> GetDictionaryFromHashTableAst(
412412
return output;
413413
}
414414

415-
private bool ParseProfileHashtable(Hashtable profile, PathIntrinsics path, IOutputWriter writer,
416-
List<string> severityList, List<string> includeRuleList, List<string> excludeRuleList)
415+
/// <summary>
416+
/// Recursively convert hashtable to dictionary
417+
/// </summary>
418+
/// <param name="hashtable"></param>
419+
/// <returns>Dictionary that maps string to object</returns>
420+
private Dictionary<string, object> GetDictionaryFromHashtable(
421+
Hashtable hashtable,
422+
IOutputWriter writer,
423+
out bool hasError)
417424
{
418-
bool hasError = false;
419-
420-
HashSet<string> validKeys = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
421-
validKeys.Add("severity");
422-
validKeys.Add("includerules");
423-
validKeys.Add("excluderules");
424-
validKeys.Add("rules");
425-
426-
foreach (var obj in profile.Keys)
425+
var dictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
426+
hasError = false;
427+
foreach (var obj in hashtable.Keys)
427428
{
428429
string key = obj as string;
429430
if (key == null)
@@ -433,13 +434,64 @@ private bool ParseProfileHashtable(Hashtable profile, PathIntrinsics path, IOutp
433434
new InvalidDataException(string.Format(CultureInfo.CurrentCulture, Strings.KeyNotString, key)),
434435
Strings.ConfigurationKeyNotAString,
435436
ErrorCategory.InvalidData,
436-
profile));
437+
hashtable));
437438
hasError = true;
438-
continue;
439+
return null;
440+
}
441+
var valueHashtableObj = hashtable[obj];
442+
if (valueHashtableObj == null)
443+
{
444+
writer.WriteError(
445+
new ErrorRecord(
446+
new InvalidDataException(string.Format(CultureInfo.CurrentCulture, Strings.WrongValueHashTable, valueHashtableObj, key)),
447+
Strings.WrongConfigurationKey,
448+
ErrorCategory.InvalidData,
449+
hashtable));
450+
hasError = true;
451+
return null;
452+
}
453+
var valueHashtable = valueHashtableObj as Hashtable;
454+
if (valueHashtable == null)
455+
{
456+
dictionary.Add(key, valueHashtableObj);
457+
}
458+
else
459+
{
460+
var dict = GetDictionaryFromHashtable(valueHashtable, writer, out hasError);
461+
if (dict != null)
462+
{
463+
dictionary.Add(key, dict);
464+
}
465+
else
466+
{
467+
return null;
468+
}
439469
}
440470

441-
key = key.ToLower();
442-
object value = profile[obj];
471+
}
472+
return dictionary;
473+
}
474+
475+
private bool ParseProfileHashtable(Hashtable profile, PathIntrinsics path, IOutputWriter writer,
476+
List<string> severityList, List<string> includeRuleList, List<string> excludeRuleList)
477+
{
478+
bool hasError = false;
479+
480+
HashSet<string> validKeys = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
481+
validKeys.Add("severity");
482+
validKeys.Add("includerules");
483+
validKeys.Add("excluderules");
484+
validKeys.Add("rules");
485+
486+
settings = GetDictionaryFromHashtable(profile, writer, out hasError);
487+
if (hasError)
488+
{
489+
return hasError;
490+
}
491+
foreach (var settingKey in settings.Keys)
492+
{
493+
var key = settingKey.ToLower();
494+
object value = settings[key];
443495
switch (key)
444496
{
445497
case "severity":
@@ -496,19 +548,6 @@ private bool ParseProfileHashtable(Hashtable profile, PathIntrinsics path, IOutp
496548
break;
497549

498550
case "rules":
499-
var ruleDict = value as Dictionary<string, object>;
500-
if (ruleDict == null)
501-
{
502-
writer.WriteError(
503-
new ErrorRecord(
504-
new InvalidDataException(string.Format(CultureInfo.CurrentCulture, Strings.WrongValueHashTable, value, key)),
505-
Strings.WrongConfigurationKey,
506-
ErrorCategory.InvalidData,
507-
profile));
508-
hasError = true;
509-
break;
510-
}
511-
settings[key] = ruleDict;
512551
break;
513552

514553
default:

0 commit comments

Comments
 (0)