Skip to content

Commit

Permalink
Get rid of a previous NRE, and nearby updates
Browse files Browse the repository at this point in the history
  • Loading branch information
drwill-ms committed Oct 19, 2022
1 parent 6f0fbd7 commit 4704ee7
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 61 deletions.
2 changes: 1 addition & 1 deletion iothub/service/src/Twin/Models/Twin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Twin(string deviceId)
/// <param name="twinProperties">Properties of the twin.</param>
public Twin(TwinProperties twinProperties)
{
Properties = twinProperties;
Properties = twinProperties ?? new();
}

/// <summary>
Expand Down
22 changes: 15 additions & 7 deletions iothub/service/src/Twin/Models/TwinCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public int Count
/// </summary>
/// <param name="propertyName">Name of the property to get.</param>
/// <returns>Value for the given property name.</returns>
/// <exception cref="InvalidOperationException">When the specified <paramref name="propertyName"/> does not exist in the collection.</exception>
public dynamic this[string propertyName]
{
get
Expand All @@ -136,7 +137,7 @@ public int Count
return value;
}

throw new ArgumentOutOfRangeException(nameof(propertyName), $"Unexpected property name '{propertyName}'.");
throw new InvalidOperationException($"Unexpected property name '{propertyName}'.");
}
set => TrySetMemberInternal(propertyName, value);
}
Expand All @@ -157,14 +158,18 @@ public TwinMetadata GetMetadata()
}

/// <summary>
/// Gets the last updated time for this property.
/// Gets the last time this property was updated in UTC.
/// </summary>
/// <returns>Date-time instance representing the last updated time for this property.</returns>
/// <exception cref="System.NullReferenceException">Thrown when the metadata object is null.
/// An example would be when the this class is created with the default constructor.</exception>
public DateTimeOffset GetLastUpdatedOnUtc()
{
return (DateTime)_metadata[LastUpdatedName];
if (_metadata != null
&& _metadata.TryGetValue(LastUpdatedName, out JToken lastUpdatedName)
&& (DateTimeOffset)lastUpdatedName is DateTimeOffset lastUpdatedOnUtc)
{
return lastUpdatedOnUtc;
}

return default;
}

/// <summary>
Expand Down Expand Up @@ -254,7 +259,10 @@ private bool TryGetMemberInternal(string propertyName, out object result)

private bool TrySetMemberInternal(string propertyName, object value)
{
JToken valueJToken = value == null ? null : JToken.FromObject(value);
JToken valueJToken = value == null
? null
: JToken.FromObject(value);

if (JObject.TryGetValue(propertyName, out _))
{
JObject[propertyName] = valueJToken;
Expand Down
22 changes: 15 additions & 7 deletions iothub/service/src/Twin/Models/TwinCollectionArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using Newtonsoft.Json.Linq;
using static Microsoft.Azure.Devices.TwinCollection;

namespace Microsoft.Azure.Devices
{
Expand All @@ -24,16 +25,17 @@ internal TwinCollectionArray(JArray jArray, JObject metadata)
/// </summary>
/// <param name="propertyName">Property name to look up.</param>
/// <returns>Property value, if present.</returns>
/// <exception cref="InvalidOperationException">When the specified <paramref name="propertyName"/> does not exist in the collection.</exception>
public dynamic this[string propertyName]
{
get
{
return propertyName switch
{
TwinCollection.MetadataName => GetMetadata(),
TwinCollection.LastUpdatedName => GetLastUpdatedOnUtc(),
TwinCollection.LastUpdatedVersionName => GetLastUpdatedVersion(),
_ => throw new ArgumentException($"{nameof(TwinCollectionArray)} does not contain a definition for '{propertyName}'."),
MetadataName => GetMetadata(),
LastUpdatedName => GetLastUpdatedOnUtc(),
LastUpdatedVersionName => GetLastUpdatedVersion(),
_ => throw new InvalidOperationException($"{nameof(TwinCollectionArray)} does not contain a definition for '{propertyName}'."),
};
}
}
Expand All @@ -50,10 +52,16 @@ public TwinMetadata GetMetadata()
/// <summary>
/// Gets the last updated time for this property.
/// </summary>
/// <returns>Date-time instance representing the last updated time for this property.</returns>
public DateTimeOffset GetLastUpdatedOnUtc()
{
return (DateTimeOffset)_metadata[TwinCollection.LastUpdatedName];
if (_metadata != null
&& _metadata.TryGetValue(LastUpdatedName, out JToken lastUpdatedName)
&& (DateTimeOffset)lastUpdatedName is DateTimeOffset lastUpdatedOnUtc)
{
return lastUpdatedOnUtc;
}

return default;
}

/// <summary>
Expand All @@ -62,7 +70,7 @@ public DateTimeOffset GetLastUpdatedOnUtc()
/// <returns>Last updated version if present, null otherwise.</returns>
public long? GetLastUpdatedVersion()
{
return (long?)_metadata[TwinCollection.LastUpdatedVersionName];
return (long?)_metadata?[LastUpdatedVersionName];
}
}
}
25 changes: 16 additions & 9 deletions iothub/service/src/Twin/Models/TwinCollectionValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Microsoft.CSharp.RuntimeBinder;
using Newtonsoft.Json.Linq;
using static Microsoft.Azure.Devices.TwinCollection;

namespace Microsoft.Azure.Devices
{
Expand All @@ -25,16 +25,17 @@ internal TwinCollectionValue(JValue jValue, JObject metadata)
/// </summary>
/// <param name="propertyName">Property name to look up.</param>
/// <returns>Property value, if present.</returns>
/// <exception cref="InvalidOperationException">When the specified <paramref name="propertyName"/> does not exist in the collection.</exception>
public dynamic this[string propertyName]
{
get
{
return propertyName switch
{
TwinCollection.MetadataName => GetMetadata(),
TwinCollection.LastUpdatedName => GetLastUpdatedOnUtc(),
TwinCollection.LastUpdatedVersionName => GetLastUpdatedVersion(),
_ => throw new RuntimeBinderException($"{nameof(TwinCollectionValue)} does not contain a definition for '{propertyName}'."),
MetadataName => GetMetadata(),
LastUpdatedName => GetLastUpdatedOnUtc(),
LastUpdatedVersionName => GetLastUpdatedVersion(),
_ => throw new InvalidOperationException($"{nameof(TwinCollectionValue)} does not contain a definition for '{propertyName}'."),
};
}
}
Expand All @@ -49,12 +50,18 @@ public TwinMetadata GetMetadata()
}

/// <summary>
/// Gets the last updated time for this property.
/// Gets the time when this property was last updated in UTC.
/// </summary>
/// <returns>Date-time instance representing the last updated time for this property.</returns>
public DateTimeOffset GetLastUpdatedOnUtc()
{
return (DateTimeOffset)_metadata[TwinCollection.LastUpdatedName];
if (_metadata != null
&& _metadata.TryGetValue(LastUpdatedName, out JToken lastUpdatedName)
&& (DateTimeOffset)lastUpdatedName is DateTimeOffset lastUpdatedOnUtc)
{
return lastUpdatedOnUtc;
}

return default;
}

/// <summary>
Expand All @@ -63,7 +70,7 @@ public DateTimeOffset GetLastUpdatedOnUtc()
/// <returns>Last updated version if present, null otherwise.</returns>
public long? GetLastUpdatedVersion()
{
return (long?)_metadata[TwinCollection.LastUpdatedVersionName];
return (long?)_metadata?[LastUpdatedVersionName];
}
}
}
28 changes: 13 additions & 15 deletions provisioning/service/src/Twin/TwinCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,9 @@ public TwinCollection(JObject twinJson, JObject metadataJson)
/// <summary>
/// Gets the version of the twin collection.
/// </summary>
public long Version
{
get
{
return !JObject.TryGetValue(VersionName, out JToken versionToken)
? default(long)
: (long)versionToken;
}
}
public long Version => !JObject.TryGetValue(VersionName, out JToken versionToken)
? default
: (long)versionToken;

/// <summary>
/// Gets the count of properties in the collection.
Expand Down Expand Up @@ -169,14 +163,18 @@ public TwinMetadata GetMetadata()
}

/// <summary>
/// Gets the LastUpdated time for this property.
/// Gets the time when this property was last updated in UTC.
/// </summary>
/// <returns>DateTime instance representing the LastUpdated time for this property.</returns>
/// <exception cref="NullReferenceException">Thrown when the TwinCollection metadata is null.
/// An example would be when the twin collection class is created with the default constructor.</exception>
public DateTime GetLastUpdatedOnUtc()
public DateTimeOffset GetLastUpdatedOnUtc()
{
return (DateTime)_metadata[LastUpdatedName];
if (_metadata != null
&& _metadata.TryGetValue(LastUpdatedName, out JToken lastUpdatedName)
&& (DateTimeOffset)lastUpdatedName is DateTimeOffset lastUpdatedOnUtc)
{
return lastUpdatedOnUtc;
}

return default;
}

/// <summary>
Expand Down
26 changes: 17 additions & 9 deletions provisioning/service/src/Twin/TwinCollectionArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using Newtonsoft.Json.Linq;
using static Microsoft.Azure.Devices.Provisioning.Service.TwinCollection;

namespace Microsoft.Azure.Devices.Provisioning.Service
{
Expand All @@ -24,16 +25,17 @@ internal TwinCollectionArray(JArray jArray, JObject metadata)
/// </summary>
/// <param name="propertyName">Property Name to lookup.</param>
/// <returns>Property value, if present</returns>
/// <exception cref="InvalidOperationException">When the specified <paramref name="propertyName"/> does not exist in the collection.</exception>
public dynamic this[string propertyName]
{
get
{
return propertyName switch
{
TwinCollection.MetadataName => GetMetadata(),
TwinCollection.LastUpdatedName => GetLastUpdatedOnUtc(),
TwinCollection.LastUpdatedVersionName => GetLastUpdatedVersion(),
_ => throw new ArgumentException($"{nameof(TwinCollectionArray)} does not contain a definition for '{propertyName}'."),
MetadataName => GetMetadata(),
LastUpdatedName => GetLastUpdatedOnUtc(),
LastUpdatedVersionName => GetLastUpdatedVersion(),
_ => throw new InvalidOperationException($"{nameof(TwinCollectionArray)} does not contain a definition for '{propertyName}'."),
};
}
}
Expand All @@ -48,12 +50,18 @@ public TwinMetadata GetMetadata()
}

/// <summary>
/// Gets the last updated time for this property.
/// Gets the time when this property was last updated in UTC.
/// </summary>
/// <returns>DateTime instance representing the last updated time for this property.</returns>
public DateTime GetLastUpdatedOnUtc()
public DateTimeOffset GetLastUpdatedOnUtc()
{
return (DateTime)_metadata[TwinCollection.LastUpdatedName];
if (_metadata != null
&& _metadata.TryGetValue(LastUpdatedName, out JToken lastUpdatedName)
&& (DateTimeOffset)lastUpdatedName is DateTimeOffset lastUpdatedOnUtc)
{
return lastUpdatedOnUtc;
}

return default;
}

/// <summary>
Expand All @@ -62,7 +70,7 @@ public DateTime GetLastUpdatedOnUtc()
/// <returns>Last updated version if present, null otherwise.</returns>
public long? GetLastUpdatedVersion()
{
return (long?)_metadata[TwinCollection.LastUpdatedVersionName];
return (long?)_metadata[LastUpdatedVersionName];
}
}
}
28 changes: 17 additions & 11 deletions provisioning/service/src/Twin/TwinCollectionValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.CSharp.RuntimeBinder;
using Newtonsoft.Json.Linq;
using static Microsoft.Azure.Devices.Provisioning.Service.TwinCollection;

namespace Microsoft.Azure.Devices.Provisioning.Service
{
Expand All @@ -30,18 +31,17 @@ internal TwinCollectionValue(JValue jValue, JObject metadata)
/// </summary>
/// <param name="propertyName">Property Name to lookup.</param>
/// <returns>Property value, if present.</returns>
[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
Justification = "AppCompat. Changing the exception to ArgumentException might break existing applications.")]
/// <exception cref="InvalidOperationException">When the specified <paramref name="propertyName"/> does not exist in the collection.</exception>
public dynamic this[string propertyName]
{
get
{
return propertyName switch
{
TwinCollection.MetadataName => GetMetadata(),
TwinCollection.LastUpdatedName => GetLastUpdatedOnUtc(),
TwinCollection.LastUpdatedVersionName => GetLastUpdatedVersion(),
_ => throw new RuntimeBinderException($"{nameof(TwinCollectionValue)} does not contain a definition for '{propertyName}'."),
MetadataName => GetMetadata(),
LastUpdatedName => GetLastUpdatedOnUtc(),
LastUpdatedVersionName => GetLastUpdatedVersion(),
_ => throw new InvalidOperationException($"{nameof(TwinCollectionValue)} does not contain a definition for '{propertyName}'."),
};
}
}
Expand All @@ -56,12 +56,18 @@ public TwinMetadata GetMetadata()
}

/// <summary>
/// Gets the LastUpdated time for this property.
/// Gets the time when this property was last updated.
/// </summary>
/// <returns>DateTime instance representing the LastUpdated time for this property.</returns>
public DateTime GetLastUpdatedOnUtc()
public DateTimeOffset GetLastUpdatedOnUtc()
{
return (DateTime)_metadata[TwinCollection.LastUpdatedName];
if (_metadata != null
&& _metadata.TryGetValue(LastUpdatedName, out JToken lastUpdatedName)
&& (DateTimeOffset)lastUpdatedName is DateTimeOffset lastUpdatedOnUtc)
{
return lastUpdatedOnUtc;
}

return DateTimeOffset.MinValue;
}

/// <summary>
Expand All @@ -70,7 +76,7 @@ public DateTime GetLastUpdatedOnUtc()
/// <returns>LastUpdatdVersion if present, null otherwise.</returns>
public long? GetLastUpdatedVersion()
{
return (long?)_metadata[TwinCollection.LastUpdatedVersionName];
return (long?)_metadata?[LastUpdatedVersionName];
}
}
}
4 changes: 2 additions & 2 deletions provisioning/service/src/Twin/TwinMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class TwinMetadata
/// </summary>
/// <param name="lastUpdatedOn">When the property was last updated.</param>
/// <param name="lastUpdatedVersion">The version of the property when updated.</param>
public TwinMetadata(DateTime lastUpdatedOn, long? lastUpdatedVersion)
public TwinMetadata(DateTimeOffset lastUpdatedOn, long? lastUpdatedVersion)
{
LastUpdatedOnUtc = lastUpdatedOn;
LastUpdatedVersion = lastUpdatedVersion;
Expand All @@ -24,7 +24,7 @@ public TwinMetadata(DateTime lastUpdatedOn, long? lastUpdatedVersion)
/// <summary>
/// Wwhen a property was last updated.
/// </summary>
public DateTime LastUpdatedOnUtc { get; set; }
public DateTimeOffset LastUpdatedOnUtc { get; set; }

/// <remarks>
/// This should be null for Reported properties metadata and must not be null for Desired properties metadata.
Expand Down

0 comments on commit 4704ee7

Please sign in to comment.