зеркало из https://github.com/microsoft/PSRule.git
Option interface refactoring (#2598)
This commit is contained in:
Родитель
d1a3bdc5a7
Коммит
b176840fe0
|
@ -89,12 +89,10 @@ public sealed class BaselineOption : IEquatable<BaselineOption>, IBaselineOption
|
|||
Group = group;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load from a dictionary.
|
||||
/// </summary>
|
||||
internal void Load(Dictionary<string, object> index)
|
||||
/// <inheritdoc/>
|
||||
public void Import(IDictionary<string, object> dictionary)
|
||||
{
|
||||
if (index.TryPopStringArrayMap("Baseline.Group", out var group))
|
||||
if (dictionary.TryPopStringArrayMap("Baseline.Group", out var group))
|
||||
Group = group;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -351,48 +351,46 @@ public sealed class ExecutionOption : IEquatable<ExecutionOption>, IExecutionOpt
|
|||
UnprocessedObject = unprocessedObject;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load from dictionary.
|
||||
/// </summary>
|
||||
internal void Load(Dictionary<string, object> index)
|
||||
/// <inheritdoc/>
|
||||
public void Import(IDictionary<string, object> dictionary)
|
||||
{
|
||||
if (index.TryPopEnum("Execution.Break", out BreakLevel @break))
|
||||
if (dictionary.TryPopEnum("Execution.Break", out BreakLevel @break))
|
||||
Break = @break;
|
||||
|
||||
if (index.TryPopEnum("Execution.HashAlgorithm", out HashAlgorithm hashAlgorithm))
|
||||
if (dictionary.TryPopEnum("Execution.HashAlgorithm", out HashAlgorithm hashAlgorithm))
|
||||
HashAlgorithm = hashAlgorithm;
|
||||
|
||||
if (index.TryPopEnum("Execution.DuplicateResourceId", out ExecutionActionPreference duplicateResourceId))
|
||||
if (dictionary.TryPopEnum("Execution.DuplicateResourceId", out ExecutionActionPreference duplicateResourceId))
|
||||
DuplicateResourceId = duplicateResourceId;
|
||||
|
||||
if (index.TryPopEnum("Execution.LanguageMode", out LanguageMode languageMode))
|
||||
if (dictionary.TryPopEnum("Execution.LanguageMode", out LanguageMode languageMode))
|
||||
LanguageMode = languageMode;
|
||||
|
||||
if (index.TryPopEnum("Execution.InitialSessionState", out SessionState initialSessionState))
|
||||
if (dictionary.TryPopEnum("Execution.InitialSessionState", out SessionState initialSessionState))
|
||||
InitialSessionState = initialSessionState;
|
||||
|
||||
if (index.TryPopEnum("Execution.RestrictScriptSource", out RestrictScriptSource restrictScriptSource))
|
||||
if (dictionary.TryPopEnum("Execution.RestrictScriptSource", out RestrictScriptSource restrictScriptSource))
|
||||
RestrictScriptSource = restrictScriptSource;
|
||||
|
||||
if (index.TryPopEnum("Execution.SuppressionGroupExpired", out ExecutionActionPreference suppressionGroupExpired))
|
||||
if (dictionary.TryPopEnum("Execution.SuppressionGroupExpired", out ExecutionActionPreference suppressionGroupExpired))
|
||||
SuppressionGroupExpired = suppressionGroupExpired;
|
||||
|
||||
if (index.TryPopEnum("Execution.RuleExcluded", out ExecutionActionPreference ruleExcluded))
|
||||
if (dictionary.TryPopEnum("Execution.RuleExcluded", out ExecutionActionPreference ruleExcluded))
|
||||
RuleExcluded = ruleExcluded;
|
||||
|
||||
if (index.TryPopEnum("Execution.RuleSuppressed", out ExecutionActionPreference ruleSuppressed))
|
||||
if (dictionary.TryPopEnum("Execution.RuleSuppressed", out ExecutionActionPreference ruleSuppressed))
|
||||
RuleSuppressed = ruleSuppressed;
|
||||
|
||||
if (index.TryPopEnum("Execution.AliasReference", out ExecutionActionPreference aliasReference))
|
||||
if (dictionary.TryPopEnum("Execution.AliasReference", out ExecutionActionPreference aliasReference))
|
||||
AliasReference = aliasReference;
|
||||
|
||||
if (index.TryPopEnum("Execution.RuleInconclusive", out ExecutionActionPreference ruleInconclusive))
|
||||
if (dictionary.TryPopEnum("Execution.RuleInconclusive", out ExecutionActionPreference ruleInconclusive))
|
||||
RuleInconclusive = ruleInconclusive;
|
||||
|
||||
if (index.TryPopEnum("Execution.InvariantCulture", out ExecutionActionPreference invariantCulture))
|
||||
if (dictionary.TryPopEnum("Execution.InvariantCulture", out ExecutionActionPreference invariantCulture))
|
||||
InvariantCulture = invariantCulture;
|
||||
|
||||
if (index.TryPopEnum("Execution.UnprocessedObject", out ExecutionActionPreference unprocessedObject))
|
||||
if (dictionary.TryPopEnum("Execution.UnprocessedObject", out ExecutionActionPreference unprocessedObject))
|
||||
UnprocessedObject = unprocessedObject;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,4 +8,9 @@ namespace PSRule.Options;
|
|||
/// </summary>
|
||||
public interface IOption
|
||||
{
|
||||
/// <summary>
|
||||
/// Import from a dictionary index by a string key.
|
||||
/// </summary>
|
||||
/// <param name="dictionary">A dictionary of key value pairs to load the option from.</param>
|
||||
void Import(IDictionary<string, object> dictionary);
|
||||
}
|
||||
|
|
|
@ -203,30 +203,28 @@ public sealed class BindingOption : IEquatable<BindingOption>, IBindingOption
|
|||
UseQualifiedName = useQualifiedName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load from a dictionary.
|
||||
/// </summary>
|
||||
internal void Load(Dictionary<string, object> index)
|
||||
/// <inheritdoc/>
|
||||
public void Import(IDictionary<string, object> dictionary)
|
||||
{
|
||||
if (index.TryPopValue("Binding.Field", out Hashtable map))
|
||||
if (dictionary.TryPopValue("Binding.Field", out Hashtable map))
|
||||
Field = new FieldMap(map);
|
||||
|
||||
if (index.TryPopBool("Binding.IgnoreCase", out var ignoreCase))
|
||||
if (dictionary.TryPopBool("Binding.IgnoreCase", out var ignoreCase))
|
||||
IgnoreCase = ignoreCase;
|
||||
|
||||
if (index.TryPopString("Binding.NameSeparator", out var nameSeparator))
|
||||
if (dictionary.TryPopString("Binding.NameSeparator", out var nameSeparator))
|
||||
NameSeparator = nameSeparator;
|
||||
|
||||
if (index.TryPopBool("Binding.PreferTargetInfo", out var preferTargetInfo))
|
||||
if (dictionary.TryPopBool("Binding.PreferTargetInfo", out var preferTargetInfo))
|
||||
PreferTargetInfo = preferTargetInfo;
|
||||
|
||||
if (index.TryPopStringArray("Binding.TargetName", out var targetName))
|
||||
if (dictionary.TryPopStringArray("Binding.TargetName", out var targetName))
|
||||
TargetName = targetName;
|
||||
|
||||
if (index.TryPopStringArray("Binding.TargetType", out var targetType))
|
||||
if (dictionary.TryPopStringArray("Binding.TargetType", out var targetType))
|
||||
TargetType = targetType;
|
||||
|
||||
if (index.TryPopValue("Binding.UseQualifiedName", out bool useQualifiedName))
|
||||
if (dictionary.TryPopValue("Binding.UseQualifiedName", out bool useQualifiedName))
|
||||
UseQualifiedName = useQualifiedName;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -160,13 +160,17 @@ public sealed class PSRuleOption : IEquatable<PSRuleOption>, IBaselineV1Spec
|
|||
var yaml = GetYaml();
|
||||
return string.IsNullOrEmpty(_SourcePath)
|
||||
? yaml
|
||||
: string.Concat(
|
||||
string.Format(
|
||||
: string.Concat
|
||||
(
|
||||
string.Format
|
||||
(
|
||||
Thread.CurrentThread.CurrentCulture,
|
||||
PSRuleResources.OptionsSourceComment,
|
||||
_SourcePath),
|
||||
_SourcePath
|
||||
),
|
||||
System.Environment.NewLine,
|
||||
yaml);
|
||||
yaml
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -354,10 +358,10 @@ public sealed class PSRuleOption : IEquatable<PSRuleOption>, IBaselineV1Spec
|
|||
|
||||
// Start loading matching values
|
||||
var index = BuildIndex(hashtable);
|
||||
option.Baseline.Load(index);
|
||||
option.Binding.Load(index);
|
||||
option.Baseline.Import(index);
|
||||
option.Binding.Import(index);
|
||||
option.Convention.Load(index);
|
||||
option.Execution.Load(index);
|
||||
option.Execution.Import(index);
|
||||
option.Include.Load(index);
|
||||
option.Input.Load(index);
|
||||
option.Logging.Load(index);
|
||||
|
@ -526,6 +530,7 @@ public sealed class PSRuleOption : IEquatable<PSRuleOption>, IBaselineV1Spec
|
|||
.WithTypeConverter(new FieldMapYamlTypeConverter())
|
||||
.WithTypeConverter(new StringArrayMapConverter())
|
||||
.Build();
|
||||
|
||||
return s.Serialize(this);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,12 +46,15 @@ internal sealed class EmitterCollection : IDisposable
|
|||
/// Visit an object with applicable emitters.
|
||||
/// </summary>
|
||||
/// <param name="o">The object to visit.</param>
|
||||
/// <returns>Returns <c>true</c> if the object was processed by any emitter.</returns>
|
||||
public bool Visit(object? o)
|
||||
{
|
||||
if (o == null) return false;
|
||||
|
||||
if (TryGetFile(o, out var info) && info != null)
|
||||
if (TryGetFile(o, out var info, out var exists) && info != null)
|
||||
{
|
||||
if (!exists) return false;
|
||||
|
||||
if (_ShouldEmitFile && _Context.ShouldQueue(info.Path))
|
||||
{
|
||||
// Emit the file.
|
||||
|
@ -90,23 +93,27 @@ internal sealed class EmitterCollection : IDisposable
|
|||
return o is PSObject pso ? pso.BaseObject : o;
|
||||
}
|
||||
|
||||
private static bool TryGetFile(object o, out InternalFileInfo? info)
|
||||
private static bool TryGetFile(object o, out InternalFileInfo? info, out bool exists)
|
||||
{
|
||||
info = null;
|
||||
exists = false;
|
||||
o = GetBaseObject(o);
|
||||
if (o is FileInfo fileInfo && fileInfo.Exists)
|
||||
if (o is FileInfo fileInfo)
|
||||
{
|
||||
info = new InternalFileInfo(fileInfo.FullName, fileInfo.Extension);
|
||||
exists = fileInfo.Exists;
|
||||
return true;
|
||||
}
|
||||
if (o is InputFileInfo inputFileInfo && inputFileInfo.AsFileInfo().Exists)
|
||||
if (o is InputFileInfo inputFileInfo)
|
||||
{
|
||||
info = new InternalFileInfo(inputFileInfo.FullName, inputFileInfo.Extension);
|
||||
exists = inputFileInfo.AsFileInfo().Exists;
|
||||
return true;
|
||||
}
|
||||
if (o is InternalFileInfo internalFile && File.Exists(internalFile.Path))
|
||||
if (o is InternalFileInfo internalFile)
|
||||
{
|
||||
info = internalFile;
|
||||
exists = File.Exists(internalFile.Path);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -160,7 +167,12 @@ internal sealed class EmitterCollection : IDisposable
|
|||
{
|
||||
return next == null ?
|
||||
(context, o, type) => e.Accepts(context, type) && e.Visit(context, o) :
|
||||
(context, o, type) => e.Accepts(context, type) && e.Visit(context, o) || next(context, o, type);
|
||||
(context, o, type) =>
|
||||
{
|
||||
var r1 = e.Accepts(context, type) && e.Visit(context, o);
|
||||
var r2 = next(context, o, type);
|
||||
return r1 || r2;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче