Skip to content

Commit 7788c3e

Browse files
authored
Fixed handling of multi-line descriptions Azure#2973 (Azure#2975)
1 parent 6091771 commit 7788c3e

16 files changed

+430
-211
lines changed

docs/CHANGELOG-v1.md

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers
3636
- Bug fixes:
3737
- Rollback Az.Resources to v6.7.0.
3838
[#2970](https://github.com/Azure/PSRule.Rules.Azure/issues/2970)
39+
- Fixed handling of multi-line descriptions for policy definition and assignment exports by @BernieWhite.
40+
[#2973](https://github.com/Azure/PSRule.Rules.Azure/issues/2973)
3941

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

src/PSRule.Rules.Azure/Common/StringExtensions.cs

+36
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ namespace PSRule.Rules.Azure
88
{
99
internal static class StringExtensions
1010
{
11+
private static readonly string[] LINE_SEPARATORS = new string[] { "\r\n", "\n", "\r" };
12+
private static readonly char[] RAW_LINE_SEPARATORS = new char[] { '\r', '\n' };
13+
1114
/// <summary>
1215
/// Convert the first character of the string to lower-case.
1316
/// </summary>
@@ -83,5 +86,38 @@ internal static bool IsExpressionString(this string str)
8386
str[1] != '[' &&
8487
str[str.Length - 1] == ']';
8588
}
89+
90+
/// <summary>
91+
/// Get the first line of a string.
92+
/// If the string contains new line characters, only the first line is returned.
93+
/// </summary>
94+
/// <param name="str">The string to use.</param>
95+
/// <returns>A formatted string.</returns>
96+
internal static string ToFirstLine(this string str)
97+
{
98+
if (string.IsNullOrEmpty(str))
99+
return string.Empty;
100+
101+
var firstLineEnd = str.IndexOfAny(RAW_LINE_SEPARATORS);
102+
return firstLineEnd == -1
103+
? str
104+
: str.Substring(0, firstLineEnd);
105+
}
106+
107+
/// <summary>
108+
/// Replace new line separators with the system default.
109+
/// </summary>
110+
/// <param name="str">The string to replace.</param>
111+
/// <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>
112+
/// <returns>A formatted string with new line separators replaced.</returns>
113+
internal static string ReplaceNewLineSeparator(this string str, string replacement)
114+
{
115+
if (str == null || str.Length == 0) return str;
116+
117+
replacement ??= Environment.NewLine;
118+
119+
var s = str.Split(LINE_SEPARATORS, StringSplitOptions.None);
120+
return string.Join(replacement, s);
121+
}
86122
}
87123
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace PSRule.Rules.Azure.Data.Policy
5+
{
6+
internal interface ILazyValue
7+
{
8+
object GetValue(PolicyAssignmentVisitor.PolicyAssignmentContext context);
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace PSRule.Rules.Azure.Data.Policy
5+
{
6+
internal interface IParameterValue
7+
{
8+
string Name { get; }
9+
10+
ParameterType Type { get; }
11+
12+
object GetValue(PolicyAssignmentVisitor.PolicyAssignmentContext context);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Newtonsoft.Json.Linq;
5+
using PSRule.Rules.Azure.Data.Template;
6+
7+
namespace PSRule.Rules.Azure.Data.Policy
8+
{
9+
internal sealed class LazyParameter<T> : ILazyValue, IParameterValue
10+
{
11+
private readonly JToken _LazyValue;
12+
private T _Value;
13+
private bool _Resolved;
14+
15+
public LazyParameter(string name, ParameterType type, JToken defaultValue)
16+
{
17+
Name = name;
18+
Type = type;
19+
_LazyValue = defaultValue;
20+
}
21+
22+
public string Name { get; }
23+
24+
public ParameterType Type { get; }
25+
26+
public object GetValue(PolicyAssignmentVisitor.PolicyAssignmentContext context)
27+
{
28+
if (!_Resolved)
29+
{
30+
_Value = TemplateVisitor.ExpandToken<T>(context, _LazyValue);
31+
_Resolved = true;
32+
}
33+
return _Value;
34+
}
35+
}
36+
}

src/PSRule.Rules.Azure/Data/Policy/Models.cs

-202
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Converters;
6+
7+
namespace PSRule.Rules.Azure.Data.Policy
8+
{
9+
[JsonConverter(typeof(StringEnumConverter))]
10+
internal enum ParameterType
11+
{
12+
String,
13+
14+
Array,
15+
16+
Object,
17+
18+
Boolean,
19+
20+
Integer,
21+
22+
Float,
23+
24+
DateTime
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace PSRule.Rules.Azure.Data.Policy
5+
{
6+
internal sealed class PolicyAssignmentDataExportVisitor : PolicyAssignmentVisitor
7+
{
8+
9+
}
10+
}

src/PSRule.Rules.Azure/Data/Policy/PolicyAssignmentVisitor.cs

-5
Original file line numberDiff line numberDiff line change
@@ -1986,9 +1986,4 @@ private static PolicyDefinitionEmptyConditionException ThrowEmptyConditionExpand
19861986
return new PolicyDefinitionEmptyConditionException(string.Format(Thread.CurrentThread.CurrentCulture, PSRuleResources.EmptyConditionExpandResult, policyDefinitionId, context.AssignmentId), context.AssignmentFile, context.AssignmentId, policyDefinitionId);
19871987
}
19881988
}
1989-
1990-
internal sealed class PolicyAssignmentDataExportVisitor : PolicyAssignmentVisitor
1991-
{
1992-
1993-
}
19941989
}

0 commit comments

Comments
 (0)