Skip to content

Commit

Permalink
Fixed handling of multi-line descriptions Azure#2973 (Azure#2975)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Jul 6, 2024
1 parent 6091771 commit 7788c3e
Show file tree
Hide file tree
Showing 16 changed files with 430 additions and 211 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers
- Bug fixes:
- Rollback Az.Resources to v6.7.0.
[#2970](https://github.com/Azure/PSRule.Rules.Azure/issues/2970)
- Fixed handling of multi-line descriptions for policy definition and assignment exports by @BernieWhite.
[#2973](https://github.com/Azure/PSRule.Rules.Azure/issues/2973)

## v1.38.0-B0068 (pre-release)

Expand Down
36 changes: 36 additions & 0 deletions src/PSRule.Rules.Azure/Common/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace PSRule.Rules.Azure
{
internal static class StringExtensions
{
private static readonly string[] LINE_SEPARATORS = new string[] { "\r\n", "\n", "\r" };
private static readonly char[] RAW_LINE_SEPARATORS = new char[] { '\r', '\n' };

/// <summary>
/// Convert the first character of the string to lower-case.
/// </summary>
Expand Down Expand Up @@ -83,5 +86,38 @@ internal static bool IsExpressionString(this string str)
str[1] != '[' &&
str[str.Length - 1] == ']';
}

/// <summary>
/// Get the first line of a string.
/// If the string contains new line characters, only the first line is returned.
/// </summary>
/// <param name="str">The string to use.</param>
/// <returns>A formatted string.</returns>
internal static string ToFirstLine(this string str)
{
if (string.IsNullOrEmpty(str))
return string.Empty;

var firstLineEnd = str.IndexOfAny(RAW_LINE_SEPARATORS);
return firstLineEnd == -1
? str
: str.Substring(0, firstLineEnd);
}

/// <summary>
/// Replace new line separators with the system default.
/// </summary>
/// <param name="str">The string to replace.</param>
/// <param name="replacement">Replace the new line with the supplied sequence. By default this will be the new line separator for the current operating system.</param>
/// <returns>A formatted string with new line separators replaced.</returns>
internal static string ReplaceNewLineSeparator(this string str, string replacement)
{
if (str == null || str.Length == 0) return str;

replacement ??= Environment.NewLine;

var s = str.Split(LINE_SEPARATORS, StringSplitOptions.None);
return string.Join(replacement, s);
}
}
}
10 changes: 10 additions & 0 deletions src/PSRule.Rules.Azure/Data/Policy/ILazyValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Rules.Azure.Data.Policy
{
internal interface ILazyValue
{
object GetValue(PolicyAssignmentVisitor.PolicyAssignmentContext context);
}
}
14 changes: 14 additions & 0 deletions src/PSRule.Rules.Azure/Data/Policy/IParameterValue.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.Rules.Azure.Data.Policy
{
internal interface IParameterValue
{
string Name { get; }

ParameterType Type { get; }

object GetValue(PolicyAssignmentVisitor.PolicyAssignmentContext context);
}
}
36 changes: 36 additions & 0 deletions src/PSRule.Rules.Azure/Data/Policy/LazyParameter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Newtonsoft.Json.Linq;
using PSRule.Rules.Azure.Data.Template;

namespace PSRule.Rules.Azure.Data.Policy
{
internal sealed class LazyParameter<T> : ILazyValue, IParameterValue
{
private readonly JToken _LazyValue;
private T _Value;
private bool _Resolved;

public LazyParameter(string name, ParameterType type, JToken defaultValue)
{
Name = name;
Type = type;
_LazyValue = defaultValue;
}

public string Name { get; }

public ParameterType Type { get; }

public object GetValue(PolicyAssignmentVisitor.PolicyAssignmentContext context)
{
if (!_Resolved)
{
_Value = TemplateVisitor.ExpandToken<T>(context, _LazyValue);
_Resolved = true;
}
return _Value;
}
}
}
202 changes: 0 additions & 202 deletions src/PSRule.Rules.Azure/Data/Policy/Models.cs

This file was deleted.

26 changes: 26 additions & 0 deletions src/PSRule.Rules.Azure/Data/Policy/ParameterType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace PSRule.Rules.Azure.Data.Policy
{
[JsonConverter(typeof(StringEnumConverter))]
internal enum ParameterType
{
String,

Array,

Object,

Boolean,

Integer,

Float,

DateTime
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Rules.Azure.Data.Policy
{
internal sealed class PolicyAssignmentDataExportVisitor : PolicyAssignmentVisitor
{

}
}
5 changes: 0 additions & 5 deletions src/PSRule.Rules.Azure/Data/Policy/PolicyAssignmentVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1986,9 +1986,4 @@ private static PolicyDefinitionEmptyConditionException ThrowEmptyConditionExpand
return new PolicyDefinitionEmptyConditionException(string.Format(Thread.CurrentThread.CurrentCulture, PSRuleResources.EmptyConditionExpandResult, policyDefinitionId, context.AssignmentId), context.AssignmentFile, context.AssignmentId, policyDefinitionId);
}
}

internal sealed class PolicyAssignmentDataExportVisitor : PolicyAssignmentVisitor
{

}
}
Loading

0 comments on commit 7788c3e

Please sign in to comment.