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

Fixed reason reported for startsWith #1818 #1827

Merged
merged 1 commit into from
May 24, 2024
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
6 changes: 6 additions & 0 deletions docs/CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers

## Unreleased

What's changed since pre-release v3.0.0-B0198:

- Bug fixes:
- Fixed reason reported for `startsWith` by @BernieWhite.
[#1818](https://github.com/microsoft/PSRule/issues/1818)

## v3.0.0-B0198 (pre-release)

What's changed since pre-release v3.0.0-B0153:
Expand Down
4 changes: 2 additions & 2 deletions src/PSRule/Common/ExpressionContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace PSRule;

internal static class ExpressionContextExtensions
{
public static bool ExpressionTrace(this ExpressionContext context, string name, object operand, object value)
public static bool ExpressionTrace(this IExpressionContext context, string name, object operand, object value)
{
var type = context.Kind == Definitions.ResourceKind.Rule ? 'R' : 'S';
context.Debug(PSRuleResources.LanguageExpressionTraceP3, type, name, operand, value);
return true;
}

public static bool ExpressionTrace(this ExpressionContext context, string name, object value)
public static bool ExpressionTrace(this IExpressionContext context, string name, object value)
{
var type = context.Kind == Definitions.ResourceKind.Rule ? 'R' : 'S';
context.Debug(PSRuleResources.LanguageExpressionTraceP2, type, name, value);
Expand Down
17 changes: 3 additions & 14 deletions src/PSRule/Definitions/Expressions/ExpressionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@

namespace PSRule.Definitions.Expressions;

internal interface IExpressionContext : IBindingContext
{
string LanguageScope { get; }

void Reason(IOperand operand, string text, params object[] args);

object Current { get; }

RunspaceContext Context { get; }
}

internal sealed class ExpressionContext : IExpressionContext, IBindingContext
{
private readonly Dictionary<string, PathExpression> _NameTokenCache;
Expand Down Expand Up @@ -57,21 +46,21 @@ bool IBindingContext.GetPathExpression(string path, out PathExpression expressio
return _NameTokenCache.TryGetValue(path, out expression);
}

internal void Debug(string message, params object[] args)
public void Debug(string message, params object[] args)
{
if (RunspaceContext.CurrentThread?.Writer == null)
return;

RunspaceContext.CurrentThread.Writer.WriteDebug(message, args);
}

internal void PushScope(RunspaceScope scope)
public void PushScope(RunspaceScope scope)
{
RunspaceContext.CurrentThread.PushScope(scope);
RunspaceContext.CurrentThread.EnterLanguageScope(Source);
}

internal void PopScope(RunspaceScope scope)
public void PopScope(RunspaceScope scope)
{
RunspaceContext.CurrentThread.PopScope(scope);
}
Expand Down
14 changes: 14 additions & 0 deletions src/PSRule/Definitions/Expressions/ExpressionInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Definitions.Expressions;

internal sealed class ExpressionInfo
{
private readonly string _Path;

public ExpressionInfo(string path)
{
_Path = path;
}
}
64 changes: 0 additions & 64 deletions src/PSRule/Definitions/Expressions/FunctionBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Diagnostics;

namespace PSRule.Definitions.Expressions;

internal delegate object ExpressionFnOuter(IExpressionContext context);
internal delegate object ExpressionFn(IExpressionContext context, object[] args);

internal delegate ExpressionFnOuter ExpressionBuilderFn(IExpressionContext context, LanguageExpression.PropertyBag properties);

internal abstract class FunctionReader
{
public abstract bool TryProperty(out string propertyName);
}

internal sealed class FunctionBuilder
{
private readonly Stack<LanguageExpression.PropertyBag> _Stack;
Expand Down Expand Up @@ -59,60 +52,3 @@ private bool TryFunction(LanguageExpression.PropertyBag properties, out IFunctio
return false;
}
}

internal sealed class FunctionFactory
{
private readonly Dictionary<string, IFunctionDescriptor> _Descriptors;

public FunctionFactory()
{
_Descriptors = new Dictionary<string, IFunctionDescriptor>(StringComparer.OrdinalIgnoreCase);
foreach (var d in Functions.Builtin)
With(d);
}

public bool TryDescriptor(string name, out IFunctionDescriptor descriptor)
{
return _Descriptors.TryGetValue(name, out descriptor);
}

public void With(IFunctionDescriptor descriptor)
{
_Descriptors.Add(descriptor.Name, descriptor);
}
}

/// <summary>
/// A structure describing a specific function.
/// </summary>
[DebuggerDisplay("Function: {Name}")]
internal sealed class FunctionDescriptor : IFunctionDescriptor
{
public FunctionDescriptor(string name, ExpressionBuilderFn fn)
{
Name = name;
Fn = fn;
}

/// <inheritdoc/>
public string Name { get; }

/// <inheritdoc/>
public ExpressionBuilderFn Fn { get; }
}

/// <summary>
/// A structure describing a specific function.
/// </summary>
internal interface IFunctionDescriptor
{
/// <summary>
/// The name of the function.
/// </summary>
string Name { get; }

/// <summary>
/// The function delegate.
/// </summary>
ExpressionBuilderFn Fn { get; }
}
25 changes: 25 additions & 0 deletions src/PSRule/Definitions/Expressions/FunctionDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Diagnostics;

namespace PSRule.Definitions.Expressions;

/// <summary>
/// A structure describing a specific function.
/// </summary>
[DebuggerDisplay("Function: {Name}")]
internal sealed class FunctionDescriptor : IFunctionDescriptor
{
public FunctionDescriptor(string name, ExpressionBuilderFn fn)
{
Name = name;
Fn = fn;
}

/// <inheritdoc/>
public string Name { get; }

/// <inheritdoc/>
public ExpressionBuilderFn Fn { get; }
}
26 changes: 26 additions & 0 deletions src/PSRule/Definitions/Expressions/FunctionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Definitions.Expressions;

internal sealed class FunctionFactory
{
private readonly Dictionary<string, IFunctionDescriptor> _Descriptors;

public FunctionFactory()
{
_Descriptors = new Dictionary<string, IFunctionDescriptor>(StringComparer.OrdinalIgnoreCase);
foreach (var d in Functions.Builtin)
With(d);
}

public bool TryDescriptor(string name, out IFunctionDescriptor descriptor)
{
return _Descriptors.TryGetValue(name, out descriptor);
}

public void With(IFunctionDescriptor descriptor)
{
_Descriptors.Add(descriptor.Name, descriptor);
}
}
9 changes: 9 additions & 0 deletions src/PSRule/Definitions/Expressions/FunctionReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Definitions.Expressions;

internal abstract class FunctionReader
{
public abstract bool TryProperty(out string propertyName);
}
28 changes: 28 additions & 0 deletions src/PSRule/Definitions/Expressions/IExpressionContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using PSRule.Pipeline;
using PSRule.Runtime;

namespace PSRule.Definitions.Expressions;

internal interface IExpressionContext : IBindingContext
{
ResourceKind Kind { get; }

SourceFile Source { get; }

string LanguageScope { get; }

void Reason(IOperand operand, string text, params object[] args);

void Debug(string message, params object[] args);

object Current { get; }

RunspaceContext Context { get; }

void PushScope(RunspaceScope scope);

void PopScope(RunspaceScope scope);
}
20 changes: 20 additions & 0 deletions src/PSRule/Definitions/Expressions/IFunctionDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Definitions.Expressions;

/// <summary>
/// A structure describing a specific function.
/// </summary>
internal interface IFunctionDescriptor
{
/// <summary>
/// The name of the function.
/// </summary>
string Name { get; }

/// <summary>
/// The function delegate.
/// </summary>
ExpressionBuilderFn Fn { get; }
}
Loading
Loading