Skip to content

Commit

Permalink
Refactoring improvements (#3136)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Oct 19, 2024
1 parent cebf93b commit 8528e66
Show file tree
Hide file tree
Showing 52 changed files with 3,756 additions and 3,618 deletions.
343 changes: 171 additions & 172 deletions src/PSRule.Rules.Azure/Configuration/ConfigurationOption.cs

Large diffs are not rendered by default.

291 changes: 145 additions & 146 deletions src/PSRule.Rules.Azure/Configuration/DeploymentOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,186 +5,185 @@
using System.Collections;
using System.ComponentModel;

namespace PSRule.Rules.Azure.Configuration
namespace PSRule.Rules.Azure.Configuration;

/// <summary>
/// Options that affect the properties of the <c>deployment()</c> object during expansion.
/// </summary>
public sealed class DeploymentOption : IEquatable<DeploymentOption>
{
private const string DEFAULT_NAME = "ps-rule-test-deployment";

internal static readonly DeploymentOption Default = new()
{
Name = DEFAULT_NAME
};

/// <summary>
/// Options that affect the properties of the <c>deployment()</c> object during expansion.
/// Creates an empty deployment option.
/// </summary>
public sealed class DeploymentOption : IEquatable<DeploymentOption>
public DeploymentOption()
{
private const string DEFAULT_NAME = "ps-rule-test-deployment";
Name = null;
}

internal static readonly DeploymentOption Default = new()
{
Name = DEFAULT_NAME
};
internal DeploymentOption(DeploymentOption option)
{
if (option == null)
return;

/// <summary>
/// Creates an empty deployment option.
/// </summary>
public DeploymentOption()
{
Name = null;
}
Name = option.Name;
}

internal DeploymentOption(DeploymentOption option)
{
if (option == null)
return;
internal DeploymentOption(string name)
{
Name = name ?? DEFAULT_NAME;
}

Name = option.Name;
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
return obj is DeploymentOption option && Equals(option);
}

internal DeploymentOption(string name)
{
Name = name ?? DEFAULT_NAME;
}
/// <inheritdoc/>
public bool Equals(DeploymentOption other)
{
return other != null &&
Name == other.Name;
}

/// <inheritdoc/>
public override bool Equals(object obj)
{
return obj is DeploymentOption option && Equals(option);
}
/// <summary>
/// Compares two deployment options to determine if they are equal.
/// </summary>
public static bool operator ==(DeploymentOption o1, DeploymentOption o2)
{
return Equals(o1, o2);
}

/// <inheritdoc/>
public bool Equals(DeploymentOption other)
{
return other != null &&
Name == other.Name;
}
/// <summary>
/// Compares two deployment options to determine if they are not equal.
/// </summary>
public static bool operator !=(DeploymentOption o1, DeploymentOption o2)
{
return !Equals(o1, o2);
}

/// <summary>
/// Compares two deployment options to determine if they are equal.
/// </summary>
public static bool operator ==(DeploymentOption o1, DeploymentOption o2)
{
return Equals(o1, o2);
}
/// <summary>
/// Compares two deployment options to determine if they are equal.
/// </summary>
public static bool Equals(DeploymentOption o1, DeploymentOption o2)
{
return (object.Equals(null, o1) && object.Equals(null, o2)) ||
(!object.Equals(null, o1) && o1.Equals(o2));
}

/// <summary>
/// Compares two deployment options to determine if they are not equal.
/// </summary>
public static bool operator !=(DeploymentOption o1, DeploymentOption o2)
/// <inheritdoc/>
public override int GetHashCode()
{
unchecked // Overflow is fine
{
return !Equals(o1, o2);
var hash = 17;
hash = hash * 23 + (Name != null ? Name.GetHashCode() : 0);
return hash;
}
}

/// <summary>
/// Compares two deployment options to determine if they are equal.
/// </summary>
public static bool Equals(DeploymentOption o1, DeploymentOption o2)
internal static DeploymentOption Combine(DeploymentOption o1, DeploymentOption o2)
{
var result = new DeploymentOption()
{
return (object.Equals(null, o1) && object.Equals(null, o2)) ||
(!object.Equals(null, o1) && o1.Equals(o2));
}
Name = o1?.Name ?? o2?.Name,
};
return result;
}

/// <inheritdoc/>
public override int GetHashCode()
{
unchecked // Overflow is fine
{
var hash = 17;
hash = hash * 23 + (Name != null ? Name.GetHashCode() : 0);
return hash;
}
}
/// <summary>
/// The name of the deployment.
/// </summary>
[DefaultValue(null)]
public string Name { get; set; }

internal static DeploymentOption Combine(DeploymentOption o1, DeploymentOption o2)
internal static DeploymentOption FromHashtable(Hashtable hashtable)
{
var option = new DeploymentOption();
if (hashtable != null)
{
var result = new DeploymentOption()
{
Name = o1?.Name ?? o2?.Name,
};
return result;
var index = PSRuleOption.BuildIndex(hashtable);
if (index.TryPopValue("Name", out string s))
option.Name = s;
}
return option;
}
}

/// <summary>
/// The name of the deployment.
/// </summary>
[DefaultValue(null)]
public string Name { get; set; }
/// <summary>
/// A reference to a deployment.
/// </summary>
public sealed class DeploymentReference
{
private DeploymentReference() { }

internal static DeploymentOption FromHashtable(Hashtable hashtable)
{
var option = new DeploymentOption();
if (hashtable != null)
{
var index = PSRuleOption.BuildIndex(hashtable);
if (index.TryPopValue("Name", out string s))
option.Name = s;
}
return option;
}
private DeploymentReference(string name)
{
Name = name;
FromName = true;
}

/// <summary>
/// A reference to a deployment.
/// The name of the deployment.
/// </summary>
public sealed class DeploymentReference
{
private DeploymentReference() { }
public string Name { get; set; }

private DeploymentReference(string name)
{
Name = name;
FromName = true;
}

/// <summary>
/// The name of the deployment.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Determines if the reference is created from a display name.
/// </summary>
public bool FromName { get; private set; }
/// <summary>
/// Determines if the reference is created from a display name.
/// </summary>
public bool FromName { get; private set; }

/// <summary>
/// Create a deployment reference from a hashtable.
/// </summary>
public static implicit operator DeploymentReference(Hashtable hashtable)
{
return FromHashtable(hashtable);
}
/// <summary>
/// Create a deployment reference from a hashtable.
/// </summary>
public static implicit operator DeploymentReference(Hashtable hashtable)
{
return FromHashtable(hashtable);
}

/// <summary>
/// Create a deployment reference from a name.
/// </summary>
public static implicit operator DeploymentReference(string deploymentName)
{
return FromString(deploymentName);
}
/// <summary>
/// Create a deployment reference from a name.
/// </summary>
public static implicit operator DeploymentReference(string deploymentName)
{
return FromString(deploymentName);
}

/// <summary>
/// Create a deployment reference from a hashtable.
/// </summary>
public static DeploymentReference FromHashtable(Hashtable hashtable)
/// <summary>
/// Create a deployment reference from a hashtable.
/// </summary>
public static DeploymentReference FromHashtable(Hashtable hashtable)
{
var option = new DeploymentReference();
if (hashtable != null)
{
var option = new DeploymentReference();
if (hashtable != null)
{
var index = PSRuleOption.BuildIndex(hashtable);
if (index.TryPopValue("Name", out string s))
option.Name = s;
}
return option;
var index = PSRuleOption.BuildIndex(hashtable);
if (index.TryPopValue("Name", out string s))
option.Name = s;
}
return option;
}

/// <summary>
/// Create a deployment reference from a name.
/// </summary>
public static DeploymentReference FromString(string deploymentName)
{
return new DeploymentReference(deploymentName);
}
/// <summary>
/// Create a deployment reference from a name.
/// </summary>
public static DeploymentReference FromString(string deploymentName)
{
return new DeploymentReference(deploymentName);
}

/// <summary>
/// Convert the reference to an option.
/// </summary>
public DeploymentOption ToDeploymentOption()
{
return new DeploymentOption(Name);
}
/// <summary>
/// Convert the reference to an option.
/// </summary>
public DeploymentOption ToDeploymentOption()
{
return new DeploymentOption(Name);
}
}
Loading

0 comments on commit 8528e66

Please sign in to comment.