Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor target binder #2529

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PSRule/Commands/InvokeRuleBlockCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private bool AcceptsType()
if (Type == null)
return true;

var comparer = RunspaceContext.CurrentThread.LanguageScope.Binding.GetComparer();
var comparer = RunspaceContext.CurrentThread.LanguageScope.GetBindingComparer();
var targetType = RunspaceContext.CurrentThread.RuleRecord.TargetType;
for (var i = 0; i < Type.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private static bool AcceptsType(string[] type)
if (type == null)
return true;

var comparer = RunspaceContext.CurrentThread.LanguageScope.Binding.GetComparer();
var comparer = RunspaceContext.CurrentThread.LanguageScope.GetBindingComparer();
var targetType = RunspaceContext.CurrentThread.RuleRecord.TargetType;
for (var i = 0; i < type.Length; i++)
{
Expand Down Expand Up @@ -322,7 +322,7 @@ private static bool AcceptsRule(string[] rule)

var context = RunspaceContext.CurrentThread;

var stringComparer = context.LanguageScope.Binding.GetComparer();
var stringComparer = context.LanguageScope.GetBindingComparer();
var resourceIdComparer = ResourceIdEqualityComparer.Default;

var ruleRecord = context.RuleRecord;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ private static bool TryScope(IExpressionContext context, LanguageExpression.Prop
if (svalue != DOT || context?.Context?.LanguageScope == null)
return Invalid(context, svalue);

if (!context.Context.LanguageScope.TryGetScope(o, out var scope))
if (!context.Context.TryGetScope(o, out var scope))
return Invalid(context, svalue);

operand = Operand.FromScope(scope);
Expand Down
7 changes: 2 additions & 5 deletions src/PSRule/Pipeline/InvokeRulePipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,12 @@ private InvokeResult ProcessTargetObject(TargetObject targetObject)
foreach (var ruleBlockTarget in _RuleGraph.GetSingleTarget())
{
// Enter rule block scope
Context.EnterLanguageScope(ruleBlockTarget.Value.Source);
var ruleRecord = Context.EnterRuleBlock(ruleBlock: ruleBlockTarget.Value);
ruleCounter++;

try
{
if (Context.Binding.ShouldFilter)
if (Context.Binding != null && Context.Binding.ShouldFilter)
continue;

// Check if dependency failed
Expand Down Expand Up @@ -163,11 +162,9 @@ private InvokeResult ProcessTargetObject(TargetObject targetObject)
finally
{
// Exit rule block scope
Context.ExitRuleBlock();
Context.ExitRuleBlock(ruleBlock: ruleBlockTarget.Value);
if (ShouldOutput(ruleRecord.Outcome))
result.Add(ruleRecord);

Context.ExitLanguageScope(ruleBlockTarget.Value.Source);
}
}

Expand Down
17 changes: 15 additions & 2 deletions src/PSRule/Pipeline/OptionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@

namespace PSRule.Pipeline;

#nullable enable

internal sealed class OptionContext
{
private ConventionOption _Convention;
private List<string> _ConventionOrder;

public OptionContext()
public OptionContext(BindTargetMethod bindTargetName, BindTargetMethod bindTargetType, BindTargetMethod bindField, string[] inputTargetType)
{

BindTargetName = bindTargetName;
BindTargetType = bindTargetType;
BindField = bindField;
InputTargetType = inputTargetType;
}

public Options.BaselineOption Baseline { get; set; }
Expand Down Expand Up @@ -58,6 +63,12 @@ public ConventionOption Convention

public IResourceFilter RuleFilter { get; set; }

public BindTargetMethod BindTargetName { get; }
public BindTargetMethod BindTargetType { get; }
public BindTargetMethod BindField { get; }

public string[] InputTargetType { get; }

internal int GetConventionOrder(IConvention convention)
{
if (Convention?.Include == null || Convention.Include.Length == 0)
Expand All @@ -71,3 +82,5 @@ internal int GetConventionOrder(IConvention convention)
return index > -1 ? index : int.MaxValue;
}
}

#nullable restore
20 changes: 18 additions & 2 deletions src/PSRule/Pipeline/OptionContextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace PSRule.Pipeline;

#nullable enable

/// <summary>
/// A helper to create an <see cref="OptionContext"/>.
/// </summary>
Expand All @@ -23,8 +25,12 @@
private readonly OptionScopeComparer _Comparer;
private readonly string[] _DefaultCulture;
private readonly List<string> _ConventionOrder;
private readonly BindTargetMethod _BindTargetName;
private readonly BindTargetMethod _BindTargetType;
private readonly BindTargetMethod _BindField;
private readonly string[] _InputTargetType;

internal OptionContextBuilder(string[] include = null, Hashtable tag = null, string[] convention = null)

Check warning on line 33 in src/PSRule/Pipeline/OptionContextBuilder.cs

View workflow job for this annotation

GitHub Actions / Build

Cannot convert null literal to non-nullable reference type.

Check warning on line 33 in src/PSRule/Pipeline/OptionContextBuilder.cs

View workflow job for this annotation

GitHub Actions / Build

Cannot convert null literal to non-nullable reference type.

Check warning on line 33 in src/PSRule/Pipeline/OptionContextBuilder.cs

View workflow job for this annotation

GitHub Actions / Build

Cannot convert null literal to non-nullable reference type.
{
_ModuleBaselineScope = [];
_Scopes = [];
Expand All @@ -41,10 +47,17 @@
/// <param name="include">A list of rule identifiers to include set by parameters. If not set all rules that meet filters are included.</param>
/// <param name="tag">A tag filter to determine which rules are included by parameters.</param>
/// <param name="convention">A list of conventions to include by parameters.</param>
internal OptionContextBuilder(PSRuleOption option, string[] include = null, Hashtable tag = null, string[] convention = null)
/// <param name="bindTargetName"></param>
/// <param name="bindTargetType"></param>
/// <param name="bindField"></param>
internal OptionContextBuilder(PSRuleOption option, string[] include = null, Hashtable tag = null, string[] convention = null, BindTargetMethod bindTargetName = null, BindTargetMethod bindTargetType = null, BindTargetMethod bindField = null)

Check warning on line 53 in src/PSRule/Pipeline/OptionContextBuilder.cs

View workflow job for this annotation

GitHub Actions / Build

Cannot convert null literal to non-nullable reference type.

Check warning on line 53 in src/PSRule/Pipeline/OptionContextBuilder.cs

View workflow job for this annotation

GitHub Actions / Build

Cannot convert null literal to non-nullable reference type.

Check warning on line 53 in src/PSRule/Pipeline/OptionContextBuilder.cs

View workflow job for this annotation

GitHub Actions / Build

Cannot convert null literal to non-nullable reference type.

Check warning on line 53 in src/PSRule/Pipeline/OptionContextBuilder.cs

View workflow job for this annotation

GitHub Actions / Build

Cannot convert null literal to non-nullable reference type.

Check warning on line 53 in src/PSRule/Pipeline/OptionContextBuilder.cs

View workflow job for this annotation

GitHub Actions / Build

Cannot convert null literal to non-nullable reference type.

Check warning on line 53 in src/PSRule/Pipeline/OptionContextBuilder.cs

View workflow job for this annotation

GitHub Actions / Build

Cannot convert null literal to non-nullable reference type.
: this(include, tag, convention)
{
Workspace(option);
_BindTargetName = bindTargetName;
_BindTargetType = bindTargetType;
_BindField = bindField;
_InputTargetType = option.Input.TargetType;
}

/// <summary>
Expand All @@ -53,7 +66,7 @@
internal OptionContext Build(string languageScope)
{
languageScope = ResourceHelper.NormalizeScope(languageScope);
var context = new OptionContext();
var context = new OptionContext(_BindTargetName, _BindTargetType, _BindField, _InputTargetType);

_Scopes.Sort(_Comparer);

Expand All @@ -72,6 +85,7 @@
{
Include = GetConventions(_Scopes)
};

return context;
}

Expand Down Expand Up @@ -220,3 +234,5 @@
return result.ToArray();
}
}

#nullable restore
6 changes: 3 additions & 3 deletions src/PSRule/Pipeline/PipelineBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ protected PipelineContext PrepareContext(BindTargetMethod bindTargetName, BindTa
bindTargetName: bindTargetName,
bindTargetType: bindTargetType,
bindField: bindField,
optionBuilder: GetOptionBuilder(),
optionBuilder: GetOptionBuilder(bindTargetName, bindTargetType, bindField),
unresolved: unresolved
);
}
Expand Down Expand Up @@ -328,9 +328,9 @@ protected PathFilter GetInputFilter()
return _InputFilter;
}

private OptionContextBuilder GetOptionBuilder()
private OptionContextBuilder GetOptionBuilder(BindTargetMethod bindTargetName, BindTargetMethod bindTargetType, BindTargetMethod bindField)
{
return new OptionContextBuilder(Option, _Include, _Tag, _Convention);
return new OptionContextBuilder(Option, _Include, _Tag, _Convention, bindTargetName, bindTargetType, bindField);
}

protected void ConfigureBinding(PSRuleOption option)
Expand Down
6 changes: 0 additions & 6 deletions src/PSRule/Pipeline/PipelineContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ internal sealed class PipelineContext : IDisposable, IBindingContext
internal readonly List<SuppressionGroupVisitor> SuppressionGroup;
internal readonly IHostContext HostContext;
internal readonly PipelineInputStream Reader;
internal readonly BindTargetMethod BindTargetName;
internal readonly BindTargetMethod BindTargetType;
internal readonly BindTargetMethod BindField;
internal readonly string RunId;

internal readonly Stopwatch RunTime;
Expand All @@ -70,9 +67,6 @@ private PipelineContext(PSRuleOption option, IHostContext hostContext, PipelineI
Option = option;
HostContext = hostContext;
Reader = reader;
BindTargetName = bindTargetName;
BindTargetType = bindTargetType;
BindField = bindField;
_LanguageMode = option.Execution.LanguageMode ?? ExecutionOption.Default.LanguageMode.Value;
_PathExpressionCache = new Dictionary<string, PathExpression>();
LocalizedDataCache = new Dictionary<string, Hashtable>();
Expand Down
Loading
Loading