Skip to content

Commit

Permalink
Refactoring for nullable (microsoft#1870)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Jul 13, 2024
1 parent 9b1c93f commit 809f0e4
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 112 deletions.
25 changes: 25 additions & 0 deletions src/PSRule.Types/Definitions/ISourceExtent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Definitions;

/// <summary>
/// A source location for a PSRule expression.
/// </summary>
public interface ISourceExtent
{
/// <summary>
/// The source file path.
/// </summary>
string File { get; }

/// <summary>
/// The first line of the expression.
/// </summary>
int? Line { get; }

/// <summary>
/// The first position of the expression.
/// </summary>
int? Position { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace PSRule.Definitions;
[DebuggerDisplay("{Text}")]
public sealed class InfoString
{
private string _Text;
private string _Markdown;
private string? _Text;
private string? _Markdown;

internal InfoString() { }

internal InfoString(string text, string markdown = null)
internal InfoString(string text, string? markdown = null)
{
Text = text;
Markdown = markdown ?? text;
Expand All @@ -33,7 +33,7 @@ public bool HasValue
/// <summary>
/// A plain text representation.
/// </summary>
public string Text
public string? Text
{
get { return _Text; }
set
Expand All @@ -46,7 +46,7 @@ public string Text
/// <summary>
/// A markdown formatted representation if set. Otherwise this is the same as <see cref="Text"/>.
/// </summary>
public string Markdown
public string? Markdown
{
get { return _Markdown; }
set
Expand All @@ -59,7 +59,7 @@ public string Markdown
/// <summary>
/// Create an info string when not null or empty.
/// </summary>
internal static InfoString Create(string text, string markdown = null)
internal static InfoString? Create(string text, string? markdown = null)
{
return string.IsNullOrEmpty(text) && string.IsNullOrEmpty(markdown) ? null : new InfoString(text, markdown);
}
Expand Down
21 changes: 0 additions & 21 deletions src/PSRule/Definitions/SourceExtent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,6 @@

namespace PSRule.Definitions;

/// <summary>
/// A source location for a PSRule expression.
/// </summary>
public interface ISourceExtent
{
/// <summary>
/// The source file path.
/// </summary>
string File { get; }

/// <summary>
/// The first line of the expression.
/// </summary>
int? Line { get; }

/// <summary>
/// The first position of the expression.
/// </summary>
int? Position { get; }
}

internal sealed class SourceExtent : ISourceExtent
{
internal SourceExtent(string file, int? line)
Expand Down
20 changes: 10 additions & 10 deletions src/PSRule/Pipeline/Dependencies/LockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ public sealed class LockFile
/// </summary>
/// <param name="path">An alternative path to the lock file.</param>
/// <returns>Returns an instance of the lock file or a default instance if the file does not exist.</returns>
public static LockFile Read(string path)
public static LockFile Read(string? path)

Check warning on line 34 in src/PSRule/Pipeline/Dependencies/LockFile.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 34 in src/PSRule/Pipeline/Dependencies/LockFile.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
path = Environment.GetRootedPath(path);
path = Path.GetExtension(path) == ".json" ? path : Path.Combine(path, DEFAULT_FILE);
LockFile result = null;
LockFile? result = null;

Check warning on line 38 in src/PSRule/Pipeline/Dependencies/LockFile.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 38 in src/PSRule/Pipeline/Dependencies/LockFile.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
if (File.Exists(path))
{
var json = File.ReadAllText(path, Encoding.UTF8);
result = JsonConvert.DeserializeObject<LockFile>(json, new JsonSerializerSettings
{
Converters = new List<JsonConverter>
{
Converters =
[
new SemanticVersionConverter()
},
],
});
return result;
return result ?? new LockFile();
}
result ??= new LockFile();
result.Modules ??= new Dictionary<string, LockEntry>(StringComparer.OrdinalIgnoreCase);
Expand All @@ -57,18 +57,18 @@ public static LockFile Read(string path)
/// Write the lock file to disk.
/// </summary>
/// <param name="path">An alternative path to the lock file.</param>
public void Write(string path)
public void Write(string? path)

Check warning on line 60 in src/PSRule/Pipeline/Dependencies/LockFile.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 60 in src/PSRule/Pipeline/Dependencies/LockFile.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
Version = 1;

path = Environment.GetRootedPath(path);
path = Path.GetExtension(path) == "json" ? path : Path.Combine(path, DEFAULT_FILE);
var json = JsonConvert.SerializeObject(this, Formatting.Indented, new JsonSerializerSettings
{
Converters = new List<JsonConverter>
{
Converters =
[
new SemanticVersionConverter()
}
]
});
File.WriteAllText(path, json, Encoding.UTF8);
}
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule/Runtime/RuleConditionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal static RuleConditionResult Create(IEnumerable<object> value)
var baseObject = ExpressionHelpers.GetBaseObject(v);
if (!(TryAssertResult(baseObject, out var result) || TryBoolean(baseObject, out result)))
{
RunspaceContext.CurrentThread.ErrorInvaildRuleResult();
RunspaceContext.CurrentThread.ErrorInvalidRuleResult();
hasErrors = true;
}
else if (result)
Expand Down
Loading

0 comments on commit 809f0e4

Please sign in to comment.