Skip to content

Commit

Permalink
Refactoring for nullable (microsoft#1880)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Jul 28, 2024
1 parent 3856d75 commit 60f7f7d
Show file tree
Hide file tree
Showing 101 changed files with 1,182 additions and 913 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bool IYamlTypeConverter.Accepts(Type type)
}
else if (parser.TryConsume<Scalar>(out scalar))
{
result[key] = new string[] { scalar.Value };
result[key] = [scalar.Value];
}
#pragma warning restore IDE0001
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using PSRule.Pipeline;

namespace PSRule.Definitions;

/// <summary>
Expand All @@ -18,5 +16,5 @@ public interface ILanguageBlock
/// <summary>
/// The source location for the block.
/// </summary>
SourceFile Source { get; }
ISourceFile Source { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ public interface IResource : ILanguageBlock
string Name { get; }

/// <summary>
/// An optional reference identifer for the resource.
/// An optional reference identifier for the resource.
/// </summary>
ResourceId? Ref { get; }

/// <summary>
/// Any additional aliases for the resource.
/// </summary>
ResourceId[] Alias { get; }
ResourceId[]? Alias { get; }

/// <summary>
/// Any resource tags.
/// </summary>
ResourceTags Tags { get; }
IResourceTags? Tags { get; }

/// <summary>
/// Any taxonomy references.
/// </summary>
ResourceLabels Labels { get; }
IResourceLabels? Labels { get; }

/// <summary>
/// Flags for the resource.
Expand Down
11 changes: 11 additions & 0 deletions src/PSRule.Types/Definitions/IResourceAnnotations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Definitions;

/// <summary>
///
/// </summary>
public interface IResourceAnnotations : IDictionary<string, object>
{
}
30 changes: 30 additions & 0 deletions src/PSRule.Types/Definitions/IResourceHelpInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Definitions;

/// <summary>
/// Metadata about a PSRule resource.
/// </summary>
public interface IResourceHelpInfo
{
/// <summary>
/// The name of the resource.
/// </summary>
string Name { get; }

/// <summary>
/// A display name of the resource if set.
/// </summary>
string DisplayName { get; }

/// <summary>
/// A short description of the resource if set.
/// </summary>
InfoString Synopsis { get; }

/// <summary>
/// A long description of the resource if set.
/// </summary>
InfoString Description { get; }
}
18 changes: 18 additions & 0 deletions src/PSRule.Types/Definitions/IResourceLabels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Definitions;

/// <summary>
///
/// </summary>
public interface IResourceLabels : IDictionary<string, string[]>
{
/// <summary>
/// Check if the resource label matches.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
bool Contains(string key, string[] value);
}
15 changes: 15 additions & 0 deletions src/PSRule.Types/Definitions/IResourceMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Definitions;

/// <summary>
/// Additional resource metadata.
/// </summary>
public interface IResourceMetadata
{
/// <summary>
/// Annotations on the resource.
/// </summary>
public IResourceAnnotations Annotations { get;}
}
26 changes: 26 additions & 0 deletions src/PSRule.Types/Definitions/IResourceTags.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 System.Collections;

namespace PSRule.Definitions;

/// <summary>
///
/// </summary>
public interface IResourceTags : IDictionary<string, string>
{
/// <summary>
///
/// </summary>
/// <returns></returns>
Hashtable ToHashtable();

/// <summary>
/// Check if a specific resource tag exists.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
bool Contains(object key, object value);
}
4 changes: 2 additions & 2 deletions src/PSRule.Types/Definitions/ISourceExtent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Definitions;
Expand All @@ -11,7 +11,7 @@ public interface ISourceExtent
/// <summary>
/// The source file path.
/// </summary>
string File { get; }
ISourceFile File { get; }

/// <summary>
/// The first line of the expression.
Expand Down
42 changes: 42 additions & 0 deletions src/PSRule.Types/Definitions/ISourceFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Definitions;

/// <summary>
/// A source file containing resources that will be loaded and interpreted by PSRule.
/// </summary>
public interface ISourceFile
{
/// <summary>
/// The file path to the source.
/// </summary>
string Path { get; }

/// <summary>
/// The name of the module if the source was loaded from a module.
/// </summary>
string Module { get; }

/// <summary>
/// The type of source file.
/// </summary>
SourceType Type { get; }

/// <summary>
/// The base path to use for loading help content.
/// </summary>
string HelpPath { get; }

/// <summary>
/// Determines if the source file exists.
/// </summary>
/// <returns>Returns <c>true</c> when the source file exists.</returns>
bool Exists();

/// <summary>
/// Determines if the source file is a dependency.
/// </summary>
/// <returns>Returns <c>true</c> when the source file is a dependency.</returns>
bool IsDependency();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace PSRule.Definitions;
/// <summary>
/// Additional resource annotations.
/// </summary>
public sealed class ResourceAnnotations : Dictionary<string, object>
public sealed class ResourceAnnotations : Dictionary<string, object>, IResourceAnnotations
{

}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Newtonsoft.Json;

namespace PSRule.Definitions;

/// <summary>
/// Metadata about a PSRule resource.
/// </summary>
public interface IResourceHelpInfo
{
/// <summary>
/// The name of the resource.
/// </summary>
string Name { get; }

/// <summary>
/// A display name of the resource if set.
/// </summary>
string DisplayName { get; }

/// <summary>
/// A short description of the resource if set.
/// </summary>
InfoString Synopsis { get; }

/// <summary>
/// A long description of the resource if set.
/// </summary>
InfoString Description { get; }
}

internal sealed class ResourceHelpInfo : IResourceHelpInfo
{
internal ResourceHelpInfo(string name, string displayName, InfoString synopsis, InfoString description)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using PSRule.Definitions.Rules;
using PSRule.Runtime;

namespace PSRule.Definitions;

internal static class ResourceHelper
Expand All @@ -12,24 +9,26 @@ internal static class ResourceHelper

private const char SCOPE_SEPARATOR = '\\';

internal const string STANDALONE_SCOPENAME = ".";

internal static string GetIdString(string scope, string name)
{
return name.IndexOf(SCOPE_SEPARATOR) >= 0
? name
: string.Concat(
LanguageScope.Normalize(scope),
NormalizeScope(scope),
SCOPE_SEPARATOR,
name
);
}

internal static void ParseIdString(string defaultScope, string id, out string scope, out string name)
internal static void ParseIdString(string defaultScope, string id, out string? scope, out string? name)
{
ParseIdString(id, out scope, out name);
scope ??= LanguageScope.Normalize(defaultScope);
scope ??= NormalizeScope(defaultScope);
}

internal static void ParseIdString(string id, out string scope, out string name)
internal static void ParseIdString(string id, out string? scope, out string? name)
{
scope = null;
name = null;
Expand All @@ -48,7 +47,7 @@ internal static void ParseIdString(string id, out string scope, out string name)
/// <param name="name">An array of names. Qualified names (RuleIds) supplied are left intact.</param>
/// <param name="kind">The <seealso cref="ResourceIdKind"/> of the <seealso cref="ResourceId"/>.</param>
/// <returns>An array of RuleIds.</returns>
internal static ResourceId[] GetRuleId(string defaultScope, string[] name, ResourceIdKind kind)
internal static ResourceId[]? GetRuleId(string defaultScope, string[] name, ResourceIdKind kind)
{
if (name == null || name.Length == 0)
return null;
Expand All @@ -60,8 +59,9 @@ internal static ResourceId[] GetRuleId(string defaultScope, string[] name, Resou
return (result.Length == 0) ? null : result;
}

internal static ResourceId GetRuleId(string defaultScope, string name, ResourceIdKind kind)
internal static ResourceId GetRuleId(string? defaultScope, string name, ResourceIdKind kind)
{
defaultScope ??= STANDALONE_SCOPENAME;
return name.IndexOf(SCOPE_SEPARATOR) > 0 ? ResourceId.Parse(name, kind) : new ResourceId(defaultScope, name, kind);
}

Expand All @@ -70,7 +70,7 @@ internal static ResourceId GetRuleId(string defaultScope, string name, ResourceI
return string.IsNullOrEmpty(name) ? null : new ResourceId(scope, name, kind);
}

internal static bool IsObsolete(ResourceMetadata metadata)
internal static bool IsObsolete(IResourceMetadata metadata)
{
return metadata != null &&
metadata.Annotations != null &&
Expand All @@ -80,6 +80,11 @@ internal static bool IsObsolete(ResourceMetadata metadata)

internal static SeverityLevel GetLevel(SeverityLevel? level)
{
return !level.HasValue || level.Value == SeverityLevel.None ? RuleV1.DEFAULT_LEVEL : level.Value;
return !level.HasValue || level.Value == SeverityLevel.None ? SeverityLevel.Error : level.Value;
}

internal static string NormalizeScope(string? scope)
{
return scope == null || string.IsNullOrEmpty(scope) ? STANDALONE_SCOPENAME : scope;
}
}
Loading

0 comments on commit 60f7f7d

Please sign in to comment.