Skip to content

Commit 8528e66

Browse files
authored
Refactoring improvements (Azure#3136)
1 parent cebf93b commit 8528e66

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3756
-3618
lines changed

src/PSRule.Rules.Azure/Configuration/ConfigurationOption.cs

+171-172
Large diffs are not rendered by default.

src/PSRule.Rules.Azure/Configuration/DeploymentOption.cs

+145-146
Original file line numberDiff line numberDiff line change
@@ -5,186 +5,185 @@
55
using System.Collections;
66
using System.ComponentModel;
77

8-
namespace PSRule.Rules.Azure.Configuration
8+
namespace PSRule.Rules.Azure.Configuration;
9+
10+
/// <summary>
11+
/// Options that affect the properties of the <c>deployment()</c> object during expansion.
12+
/// </summary>
13+
public sealed class DeploymentOption : IEquatable<DeploymentOption>
914
{
15+
private const string DEFAULT_NAME = "ps-rule-test-deployment";
16+
17+
internal static readonly DeploymentOption Default = new()
18+
{
19+
Name = DEFAULT_NAME
20+
};
21+
1022
/// <summary>
11-
/// Options that affect the properties of the <c>deployment()</c> object during expansion.
23+
/// Creates an empty deployment option.
1224
/// </summary>
13-
public sealed class DeploymentOption : IEquatable<DeploymentOption>
25+
public DeploymentOption()
1426
{
15-
private const string DEFAULT_NAME = "ps-rule-test-deployment";
27+
Name = null;
28+
}
1629

17-
internal static readonly DeploymentOption Default = new()
18-
{
19-
Name = DEFAULT_NAME
20-
};
30+
internal DeploymentOption(DeploymentOption option)
31+
{
32+
if (option == null)
33+
return;
2134

22-
/// <summary>
23-
/// Creates an empty deployment option.
24-
/// </summary>
25-
public DeploymentOption()
26-
{
27-
Name = null;
28-
}
35+
Name = option.Name;
36+
}
2937

30-
internal DeploymentOption(DeploymentOption option)
31-
{
32-
if (option == null)
33-
return;
38+
internal DeploymentOption(string name)
39+
{
40+
Name = name ?? DEFAULT_NAME;
41+
}
3442

35-
Name = option.Name;
36-
}
43+
/// <inheritdoc/>
44+
public override bool Equals(object obj)
45+
{
46+
return obj is DeploymentOption option && Equals(option);
47+
}
3748

38-
internal DeploymentOption(string name)
39-
{
40-
Name = name ?? DEFAULT_NAME;
41-
}
49+
/// <inheritdoc/>
50+
public bool Equals(DeploymentOption other)
51+
{
52+
return other != null &&
53+
Name == other.Name;
54+
}
4255

43-
/// <inheritdoc/>
44-
public override bool Equals(object obj)
45-
{
46-
return obj is DeploymentOption option && Equals(option);
47-
}
56+
/// <summary>
57+
/// Compares two deployment options to determine if they are equal.
58+
/// </summary>
59+
public static bool operator ==(DeploymentOption o1, DeploymentOption o2)
60+
{
61+
return Equals(o1, o2);
62+
}
4863

49-
/// <inheritdoc/>
50-
public bool Equals(DeploymentOption other)
51-
{
52-
return other != null &&
53-
Name == other.Name;
54-
}
64+
/// <summary>
65+
/// Compares two deployment options to determine if they are not equal.
66+
/// </summary>
67+
public static bool operator !=(DeploymentOption o1, DeploymentOption o2)
68+
{
69+
return !Equals(o1, o2);
70+
}
5571

56-
/// <summary>
57-
/// Compares two deployment options to determine if they are equal.
58-
/// </summary>
59-
public static bool operator ==(DeploymentOption o1, DeploymentOption o2)
60-
{
61-
return Equals(o1, o2);
62-
}
72+
/// <summary>
73+
/// Compares two deployment options to determine if they are equal.
74+
/// </summary>
75+
public static bool Equals(DeploymentOption o1, DeploymentOption o2)
76+
{
77+
return (object.Equals(null, o1) && object.Equals(null, o2)) ||
78+
(!object.Equals(null, o1) && o1.Equals(o2));
79+
}
6380

64-
/// <summary>
65-
/// Compares two deployment options to determine if they are not equal.
66-
/// </summary>
67-
public static bool operator !=(DeploymentOption o1, DeploymentOption o2)
81+
/// <inheritdoc/>
82+
public override int GetHashCode()
83+
{
84+
unchecked // Overflow is fine
6885
{
69-
return !Equals(o1, o2);
86+
var hash = 17;
87+
hash = hash * 23 + (Name != null ? Name.GetHashCode() : 0);
88+
return hash;
7089
}
90+
}
7191

72-
/// <summary>
73-
/// Compares two deployment options to determine if they are equal.
74-
/// </summary>
75-
public static bool Equals(DeploymentOption o1, DeploymentOption o2)
92+
internal static DeploymentOption Combine(DeploymentOption o1, DeploymentOption o2)
93+
{
94+
var result = new DeploymentOption()
7695
{
77-
return (object.Equals(null, o1) && object.Equals(null, o2)) ||
78-
(!object.Equals(null, o1) && o1.Equals(o2));
79-
}
96+
Name = o1?.Name ?? o2?.Name,
97+
};
98+
return result;
99+
}
80100

81-
/// <inheritdoc/>
82-
public override int GetHashCode()
83-
{
84-
unchecked // Overflow is fine
85-
{
86-
var hash = 17;
87-
hash = hash * 23 + (Name != null ? Name.GetHashCode() : 0);
88-
return hash;
89-
}
90-
}
101+
/// <summary>
102+
/// The name of the deployment.
103+
/// </summary>
104+
[DefaultValue(null)]
105+
public string Name { get; set; }
91106

92-
internal static DeploymentOption Combine(DeploymentOption o1, DeploymentOption o2)
107+
internal static DeploymentOption FromHashtable(Hashtable hashtable)
108+
{
109+
var option = new DeploymentOption();
110+
if (hashtable != null)
93111
{
94-
var result = new DeploymentOption()
95-
{
96-
Name = o1?.Name ?? o2?.Name,
97-
};
98-
return result;
112+
var index = PSRuleOption.BuildIndex(hashtable);
113+
if (index.TryPopValue("Name", out string s))
114+
option.Name = s;
99115
}
116+
return option;
117+
}
118+
}
100119

101-
/// <summary>
102-
/// The name of the deployment.
103-
/// </summary>
104-
[DefaultValue(null)]
105-
public string Name { get; set; }
120+
/// <summary>
121+
/// A reference to a deployment.
122+
/// </summary>
123+
public sealed class DeploymentReference
124+
{
125+
private DeploymentReference() { }
106126

107-
internal static DeploymentOption FromHashtable(Hashtable hashtable)
108-
{
109-
var option = new DeploymentOption();
110-
if (hashtable != null)
111-
{
112-
var index = PSRuleOption.BuildIndex(hashtable);
113-
if (index.TryPopValue("Name", out string s))
114-
option.Name = s;
115-
}
116-
return option;
117-
}
127+
private DeploymentReference(string name)
128+
{
129+
Name = name;
130+
FromName = true;
118131
}
119132

120133
/// <summary>
121-
/// A reference to a deployment.
134+
/// The name of the deployment.
122135
/// </summary>
123-
public sealed class DeploymentReference
124-
{
125-
private DeploymentReference() { }
136+
public string Name { get; set; }
126137

127-
private DeploymentReference(string name)
128-
{
129-
Name = name;
130-
FromName = true;
131-
}
132-
133-
/// <summary>
134-
/// The name of the deployment.
135-
/// </summary>
136-
public string Name { get; set; }
137-
138-
/// <summary>
139-
/// Determines if the reference is created from a display name.
140-
/// </summary>
141-
public bool FromName { get; private set; }
138+
/// <summary>
139+
/// Determines if the reference is created from a display name.
140+
/// </summary>
141+
public bool FromName { get; private set; }
142142

143-
/// <summary>
144-
/// Create a deployment reference from a hashtable.
145-
/// </summary>
146-
public static implicit operator DeploymentReference(Hashtable hashtable)
147-
{
148-
return FromHashtable(hashtable);
149-
}
143+
/// <summary>
144+
/// Create a deployment reference from a hashtable.
145+
/// </summary>
146+
public static implicit operator DeploymentReference(Hashtable hashtable)
147+
{
148+
return FromHashtable(hashtable);
149+
}
150150

151-
/// <summary>
152-
/// Create a deployment reference from a name.
153-
/// </summary>
154-
public static implicit operator DeploymentReference(string deploymentName)
155-
{
156-
return FromString(deploymentName);
157-
}
151+
/// <summary>
152+
/// Create a deployment reference from a name.
153+
/// </summary>
154+
public static implicit operator DeploymentReference(string deploymentName)
155+
{
156+
return FromString(deploymentName);
157+
}
158158

159-
/// <summary>
160-
/// Create a deployment reference from a hashtable.
161-
/// </summary>
162-
public static DeploymentReference FromHashtable(Hashtable hashtable)
159+
/// <summary>
160+
/// Create a deployment reference from a hashtable.
161+
/// </summary>
162+
public static DeploymentReference FromHashtable(Hashtable hashtable)
163+
{
164+
var option = new DeploymentReference();
165+
if (hashtable != null)
163166
{
164-
var option = new DeploymentReference();
165-
if (hashtable != null)
166-
{
167-
var index = PSRuleOption.BuildIndex(hashtable);
168-
if (index.TryPopValue("Name", out string s))
169-
option.Name = s;
170-
}
171-
return option;
167+
var index = PSRuleOption.BuildIndex(hashtable);
168+
if (index.TryPopValue("Name", out string s))
169+
option.Name = s;
172170
}
171+
return option;
172+
}
173173

174-
/// <summary>
175-
/// Create a deployment reference from a name.
176-
/// </summary>
177-
public static DeploymentReference FromString(string deploymentName)
178-
{
179-
return new DeploymentReference(deploymentName);
180-
}
174+
/// <summary>
175+
/// Create a deployment reference from a name.
176+
/// </summary>
177+
public static DeploymentReference FromString(string deploymentName)
178+
{
179+
return new DeploymentReference(deploymentName);
180+
}
181181

182-
/// <summary>
183-
/// Convert the reference to an option.
184-
/// </summary>
185-
public DeploymentOption ToDeploymentOption()
186-
{
187-
return new DeploymentOption(Name);
188-
}
182+
/// <summary>
183+
/// Convert the reference to an option.
184+
/// </summary>
185+
public DeploymentOption ToDeploymentOption()
186+
{
187+
return new DeploymentOption(Name);
189188
}
190189
}

0 commit comments

Comments
 (0)