Skip to content

Commit 23f9ce2

Browse files
committed
Introduced new SetDouble() extension
The new `SetDouble()` extension method allows an `AttributeValue.Value` string to be set via a `double`. This provides consistency with `GetDouble()` (148a1b1). Includes a unit test to confirm functionality.
1 parent 148a1b1 commit 23f9ce2

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

OnTopic.Tests/AttributeValueCollectionTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ public void GetInteger_IncorrectKey_ReturnsDefault() {
9494

9595
}
9696

97+
/*==========================================================================================================================
98+
| TEST: GET DOUBLE: CORRECT VALUE: IS RETURNED
99+
\-------------------------------------------------------------------------------------------------------------------------*/
100+
/// <summary>
101+
/// Ensures that double values can be set and retrieved as expected.
102+
/// </summary>
103+
[TestMethod]
104+
public void GetDouble_CorrectValue_IsReturned() {
105+
106+
var topic = TopicFactory.Create("Test", "Container");
107+
108+
topic.Attributes.SetDouble("Number1", 1);
109+
110+
Assert.AreEqual<double>(1.0, topic.Attributes.GetDouble("Number1", 5.0));
111+
112+
}
113+
97114
/*==========================================================================================================================
98115
| TEST: GET DOUBLE: INCORRECT VALUE: RETURNS DEFAULT
99116
\-------------------------------------------------------------------------------------------------------------------------*/

OnTopic/Attributes/AttributeValueCollectionExtensions.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,48 @@ public static void SetInteger(
252252
isDirty, true
253253
);
254254

255+
/*==========================================================================================================================
256+
| METHOD: SET DOUBLE
257+
\-------------------------------------------------------------------------------------------------------------------------*/
258+
/// <summary>
259+
/// Helper method that either adds a new <see cref="AttributeValue"/> object or updates the value of an existing one,
260+
/// depending on whether that value already exists.
261+
/// </summary>
262+
/// <param name="attributes">The instance of the <see cref="AttributeValueCollection"/> this extension is bound to.</param>
263+
/// <param name="key">The string identifier for the <see cref="AttributeValue"/>.</param>
264+
/// <param name="value">The double value for the <see cref="AttributeValue"/>.</param>
265+
/// <param name="isDirty">
266+
/// Specified whether the value should be marked as <see cref="AttributeValue.IsDirty"/>. By default, it will be marked as
267+
/// dirty if the value is new or has changed from a previous value. By setting this parameter, that behavior is
268+
/// overwritten to accept whatever value is submitted. This can be used, for instance, to prevent an update from being
269+
/// persisted to the data store on <see cref="ITopicRepository.Save(Topic, Boolean, Boolean)"/>.
270+
/// </param>
271+
/// <requires
272+
/// description="The key must be specified for the AttributeValue key/value pair."
273+
/// exception="T:System.ArgumentNullException">
274+
/// !String.IsNullOrWhiteSpace(key)
275+
/// </requires>
276+
/// <requires
277+
/// description="The value must be specified for the AttributeValue key/value pair."
278+
/// exception="T:System.ArgumentNullException">
279+
/// !String.IsNullOrWhiteSpace(value)
280+
/// </requires>
281+
/// <requires
282+
/// description="The key should be an alphanumeric sequence; it should not contain spaces or symbols"
283+
/// exception="T:System.ArgumentException">
284+
/// !value.Contains(" ")
285+
/// </requires>
286+
public static void SetDouble(
287+
this AttributeValueCollection attributes,
288+
string key,
289+
double value,
290+
bool? isDirty = null
291+
) => attributes?.SetValue(
292+
key,
293+
value.ToString(CultureInfo.InvariantCulture),
294+
isDirty, true
295+
);
296+
255297
/*==========================================================================================================================
256298
| METHOD: SET DATETIME
257299
\-------------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)