-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
0d09e46
commit 249478c
Showing
26 changed files
with
753 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 30 additions & 24 deletions
54
src/PSRule.Rules.Azure/Data/Template/FunctionDescriptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,44 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using PSRule.Rules.Azure.Resources; | ||
|
||
namespace PSRule.Rules.Azure.Data.Template | ||
namespace PSRule.Rules.Azure.Data.Template; | ||
|
||
/// <summary> | ||
/// A wrapper for the function definition that can be invoked. | ||
/// </summary> | ||
[DebuggerDisplay("Function: {Name}")] | ||
internal sealed class FunctionDescriptor(string name, ExpressionFn fn, bool delayBinding = false) : IFunctionDescriptor | ||
{ | ||
/// <summary> | ||
/// A wrapper for the function definition that can be invoked. | ||
/// </summary> | ||
[DebuggerDisplay("Function: {Name}")] | ||
internal sealed class FunctionDescriptor : IFunctionDescriptor | ||
private readonly ExpressionFn _Fn = fn; | ||
private readonly bool _DelayBinding = delayBinding; | ||
|
||
/// <inheritdoc/> | ||
public string Name { get; } = name; | ||
|
||
/// <inheritdoc/> | ||
public object Invoke(ITemplateContext context, DebugSymbol debugSymbol, ExpressionFnOuter[] args) | ||
{ | ||
private readonly ExpressionFn _Fn; | ||
private readonly bool _DelayBinding; | ||
var parameters = new object[args.Length]; | ||
for (var i = 0; i < args.Length; i++) | ||
parameters[i] = _DelayBinding ? args[i] : ExpressionHelpers.UnwrapLiteralString(args[i](context)); | ||
|
||
public FunctionDescriptor(string name, ExpressionFn fn, bool delayBinding = false) | ||
context.DebugSymbol = debugSymbol; | ||
try | ||
{ | ||
Name = name; | ||
_Fn = fn; | ||
_DelayBinding = delayBinding; | ||
return ExpressionHelpers.WrapLiteralString(_Fn(context, parameters)); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public string Name { get; } | ||
|
||
/// <inheritdoc/> | ||
public object Invoke(ITemplateContext context, DebugSymbol debugSymbol, ExpressionFnOuter[] args) | ||
catch (TemplateFunctionException ex) | ||
{ | ||
var parameters = new object[args.Length]; | ||
for (var i = 0; i < args.Length; i++) | ||
parameters[i] = _DelayBinding ? args[i] : args[i](context); | ||
|
||
context.DebugSymbol = debugSymbol; | ||
return _Fn(context, parameters); | ||
throw new TemplateFunctionException(Name, ex.ErrorType, string.Format(Thread.CurrentThread.CurrentCulture, PSRuleResources.FunctionGenericError, Name, ex.Message), ex); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new TemplateFunctionException(Name, FunctionErrorType.Unknown, string.Format(Thread.CurrentThread.CurrentCulture, PSRuleResources.FunctionGenericError, Name, ex.Message), ex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.