Skip to content

Commit a79a0b1

Browse files
committed
Merge branch 'feature/AttributeValueCollection.GetValue-defaults' into develop
Previously, the `AttributeValueCollection` extension methods, such as `GetBoolean()` and `GetDateTime()`, required an explicitly defined default value. This has been updated so that the `defaultValue` parameter is now optional, and will otherwise default to `default` (e.g., `0` for `GetBoolean()` and `DateTime.MinValue` for `GetDateTime()`). Callers can still explicitly define a `defaultValue` if it's relevant, but it's no longer required if the default value is acceptable. As part of this, I updated the unit tests to evaluate the implicit defaults, and also updated all calls to the `AttributeValueCollection` extensions to use the implicit defaults, if appropriate (i.e., if they were already setting the default value).
2 parents 66ccd9d + e021303 commit a79a0b1

File tree

8 files changed

+23
-16
lines changed

8 files changed

+23
-16
lines changed

OnTopic.AspNetCore.Mvc/Controllers/SitemapController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ private IEnumerable<XElement> AddTopic(Topic topic, bool includeMetadata = false
176176
| Validate topic
177177
\-----------------------------------------------------------------------------------------------------------------------*/
178178
if (topic is null) return topics;
179-
if (topic.Attributes.GetBoolean("NoIndex", false)) return topics;
180-
if (topic.Attributes.GetBoolean("IsDisabled", false)) return topics;
179+
if (topic.Attributes.GetBoolean("NoIndex")) return topics;
180+
if (topic.Attributes.GetBoolean("IsDisabled")) return topics;
181181
if (ExcludeContentTypes.Any(c => topic.ContentType.Equals(c, StringComparison.OrdinalIgnoreCase))) return topics;
182182

183183
/*------------------------------------------------------------------------------------------------------------------------

OnTopic.Tests/AttributeValueCollectionTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public void GetInteger_IncorrectValue_ReturnsDefault() {
7777
topic.Attributes.SetValue("Number3", "Invalid");
7878

7979
Assert.AreEqual<int>(5, topic.Attributes.GetInteger("Number3", 5));
80+
Assert.AreEqual<int>(0, topic.Attributes.GetInteger("Number3"));
8081

8182
}
8283

@@ -92,6 +93,7 @@ public void GetInteger_IncorrectKey_ReturnsDefault() {
9293
var topic = TopicFactory.Create("Test", "Container");
9394

9495
Assert.AreEqual<int>(5, topic.Attributes.GetInteger("InvalidKey", 5));
96+
Assert.AreEqual<int>(0, topic.Attributes.GetInteger("InvalidKey"));
9597

9698
}
9799

@@ -126,6 +128,7 @@ public void GetDouble_IncorrectValue_ReturnsDefault() {
126128
topic.Attributes.SetValue("Number3", "Invalid");
127129

128130
Assert.AreEqual<double>(5.0, topic.Attributes.GetDouble("Number3", 5.0));
131+
Assert.AreEqual<double>(0, topic.Attributes.GetDouble("Number3"));
129132

130133
}
131134

@@ -141,6 +144,7 @@ public void GetDouble_IncorrectKey_ReturnsDefault() {
141144
var topic = TopicFactory.Create("Test", "Container");
142145

143146
Assert.AreEqual<double>(5.0, topic.Attributes.GetDouble("InvalidKey", 5.0));
147+
Assert.AreEqual<double>(0, topic.Attributes.GetDouble("InvalidKey"));
144148

145149
}
146150

@@ -178,6 +182,7 @@ public void GetDateTime_IncorrectValue_ReturnsDefault() {
178182
topic.Attributes.SetDateTime("DateTime2", dateTime2);
179183

180184
Assert.AreEqual<DateTime>(dateTime1, topic.Attributes.GetDateTime("DateTime3", dateTime1));
185+
Assert.AreEqual<DateTime>(new DateTime(), topic.Attributes.GetDateTime("DateTime3"));
181186

182187
}
183188

@@ -197,6 +202,7 @@ public void GetDateTime_IncorrectKey_ReturnsDefault() {
197202
topic.Attributes.SetDateTime("DateTime2", dateTime2);
198203

199204
Assert.AreEqual<DateTime>(dateTime1, topic.Attributes.GetDateTime("DateTime3", dateTime1));
205+
Assert.AreEqual<DateTime>(new DateTime(), topic.Attributes.GetDateTime("DateTime3"));
200206

201207
}
202208

@@ -234,6 +240,7 @@ public void GetBoolean_IncorrectValue_ReturnDefault() {
234240

235241
Assert.IsTrue(topic.Attributes.GetBoolean("IsValue", true));
236242
Assert.IsFalse(topic.Attributes.GetBoolean("IsValue", false));
243+
Assert.IsFalse(topic.Attributes.GetBoolean("IsValue"));
237244

238245
}
239246

@@ -250,6 +257,7 @@ public void GetBoolean_IncorrectKey_ReturnDefault() {
250257

251258
Assert.IsTrue(topic.Attributes.GetBoolean("InvalidKey", true));
252259
Assert.IsFalse(topic.Attributes.GetBoolean("InvalidKey", false));
260+
Assert.IsFalse(topic.Attributes.GetBoolean("InvalidKey"));
253261

254262
}
255263

OnTopic.Tests/Entities/CustomTopic.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public string TextAttribute {
4646
/// </summary>
4747
[AttributeSetter]
4848
public bool BooleanAttribute {
49-
get => Attributes.GetBoolean("BooleanAttribute", false);
49+
get => Attributes.GetBoolean("BooleanAttribute");
5050
set => SetAttributeValue("BooleanAttribute", value? "1" : "0");
5151
}
5252

@@ -58,7 +58,7 @@ public bool BooleanAttribute {
5858
/// </summary>
5959
[AttributeSetter]
6060
public int NumericAttribute {
61-
get => Attributes.GetInteger("NumericAttribute", 0);
61+
get => Attributes.GetInteger("NumericAttribute");
6262
set {
6363
Contract.Requires<ArgumentOutOfRangeException>(
6464
value >= 0,
@@ -76,7 +76,7 @@ public int NumericAttribute {
7676
/// </summary>
7777
[AttributeSetter]
7878
public DateTime DateTimeAttribute {
79-
get => Attributes.GetDateTime("DateTimeAttribute", DateTime.MinValue);
79+
get => Attributes.GetDateTime("DateTimeAttribute");
8080
set {
8181
Contract.Requires<ArgumentOutOfRangeException>(
8282
value.Year > 2000,

OnTopic/Attributes/AttributeValueCollectionExtensions.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
\=============================================================================================================================*/
66
using System;
77
using System.Globalization;
8-
using OnTopic.Collections;
98
using OnTopic.Internal.Diagnostics;
109
using OnTopic.Repositories;
1110

@@ -41,7 +40,7 @@ public static class AttributeValueCollectionExtensions {
4140
public static bool GetBoolean(
4241
this AttributeValueCollection attributes,
4342
string name,
44-
bool defaultValue,
43+
bool defaultValue = default,
4544
bool inheritFromParent = false,
4645
bool inheritFromDerived = true
4746
) {
@@ -79,7 +78,7 @@ out var result
7978
public static int GetInteger(
8079
this AttributeValueCollection attributes,
8180
string name,
82-
int defaultValue,
81+
int defaultValue = default,
8382
bool inheritFromParent = false,
8483
bool inheritFromDerived = true
8584
) {
@@ -117,7 +116,7 @@ out var result
117116
public static double GetDouble(
118117
this AttributeValueCollection attributes,
119118
string name,
120-
double defaultValue,
119+
double defaultValue = default,
121120
bool inheritFromParent = false,
122121
bool inheritFromDerived = true
123122
) {
@@ -155,7 +154,7 @@ out var result
155154
public static DateTime GetDateTime(
156155
this AttributeValueCollection attributes,
157156
string name,
158-
DateTime defaultValue,
157+
DateTime defaultValue = default,
159158
bool inheritFromParent = false,
160159
bool inheritFromDerived = true
161160
) {

OnTopic/Metadata/AttributeDescriptor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public string? DisplayGroup {
147147
/// </remarks>
148148
[AttributeSetter]
149149
public bool IsRequired {
150-
get => Attributes.GetBoolean("IsRequired", false);
150+
get => Attributes.GetBoolean("IsRequired");
151151
set => SetAttributeValue("IsRequired", value ? "1" : "0");
152152
}
153153

@@ -193,7 +193,7 @@ public string? DefaultValue {
193193
/// </remarks>
194194
[AttributeSetter]
195195
public virtual bool IsExtendedAttribute {
196-
get => Attributes.GetBoolean("IsExtendedAttribute", Attributes.GetBoolean("StoreInBlob", false));
196+
get => Attributes.GetBoolean("IsExtendedAttribute", Attributes.GetBoolean("StoreInBlob"));
197197
set => SetAttributeValue("IsExtendedAttribute", value ? "1" : "0");
198198
}
199199

OnTopic/Metadata/AttributeTypes/TokenizedTopicListAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public TokenizedTopicListAttribute(
4242
\-------------------------------------------------------------------------------------------------------------------------*/
4343
/// <inheritdoc />
4444
public override ModelType ModelType =>
45-
Attributes.GetBoolean("SaveAsRelationship", false)? ModelType.Relationship : ModelType.ScalarValue;
45+
Attributes.GetBoolean("SaveAsRelationship")? ModelType.Relationship : ModelType.ScalarValue;
4646

4747
} //Class
4848
} //Namespace

OnTopic/Metadata/ContentTypeDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public ContentTypeDescriptor(
9999
/// </remarks>
100100
[AttributeSetter]
101101
public bool DisableChildTopics {
102-
get => Attributes.GetBoolean("DisableChildTopics", false);
102+
get => Attributes.GetBoolean("DisableChildTopics");
103103
set => SetAttributeValue("DisableChildTopics", value ? "1" : "0");
104104
}
105105

OnTopic/Topic.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public string? View {
326326
/// </value>
327327
[AttributeSetter]
328328
public bool IsHidden {
329-
get => Attributes.GetBoolean("IsHidden", false);
329+
get => Attributes.GetBoolean("IsHidden");
330330
set => SetAttributeValue("IsHidden", value ? "1" : "0");
331331
}
332332

@@ -341,7 +341,7 @@ public bool IsHidden {
341341
/// </value>
342342
[AttributeSetter]
343343
public bool IsDisabled {
344-
get => Attributes.GetBoolean("IsDisabled", false);
344+
get => Attributes.GetBoolean("IsDisabled");
345345
set => SetAttributeValue("IsDisabled", value ? "1" : "0");
346346
}
347347

0 commit comments

Comments
 (0)