Skip to content

Commit

Permalink
Consolidate TwinCollection.ClearMetadata() and ClearAllMetadata() (#2901
Browse files Browse the repository at this point in the history
)

* Consolidate ClearMetadata() and ClearAllMetadata()

* Consolidate ClearMetadata() and ClearAllMetadata() in hub service client

* Cleanup
  • Loading branch information
ngastelum-ms committed Oct 20, 2022
1 parent 12e47a1 commit a920528
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 38 deletions.
25 changes: 6 additions & 19 deletions iothub/service/src/Twin/Models/TwinCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public IEnumerator GetEnumerator()
///
/// For example a <see cref="List{T}"/> would return a <see cref="TwinCollectionArray"/>, with the metadata intact, when used with
/// a <see cref="TwinCollection"/> returned from a <c>RegistryManager</c> client. If you need this method to always return a
/// <see cref="JToken"/> please see the <see cref="ClearAllMetadata"/> method for more information.
/// <see cref="JToken"/> please see the <see cref="ClearMetadata"/> method for more information.
/// </remarks>
private bool TryGetMemberInternal(string propertyName, out object result)
{
Expand Down Expand Up @@ -284,33 +284,20 @@ private void TryClearMetadata(string propertyName)
}

/// <summary>
/// Clears metadata out of the twin collection.
/// Clears all metadata out of the twin collection as well as the base metadata object.
/// </summary>
/// <remarks>
/// This will only clear the metadata from the twin collection but will not change the base metadata object. This allows you to still use
/// methods such as <see cref="GetMetadata"/>. If you need to remove all metadata, please use <see cref="ClearAllMetadata"/>.
/// This will remove all metadata from the base metadata object as well as the metadata for the twin collection.
/// This will also clear the underlying metadata object which will affect methods such as
/// <see cref="GetMetadata"/> and <see cref="GetLastUpdatedVersion"/>.
/// </remarks>
public void ClearMetadata()
{
TryClearMetadata(MetadataName);
TryClearMetadata(LastUpdatedName);
TryClearMetadata(LastUpdatedVersionName);
TryClearMetadata(VersionName);
}

/// <summary>
/// Clears all metadata out of the twin collection as well as the base metadata object.
/// </summary>
/// <remarks>
/// This will remove all metadata from the base metadata object as well as the metadata for the twin collection. The difference from the
/// <see cref="ClearMetadata"/> method is this will also clear the underlying metadata object which will affect methods such as
/// <see cref="GetMetadata"/> and <see cref="GetLastUpdatedVersion"/>.
/// This method would be useful if you are performing any operations that require <see cref="TryGetMemberInternal(string, out object)"/>
/// to return a <see cref="JToken"/> regardless of the client you are using.
/// </remarks>
public void ClearAllMetadata()
{
ClearMetadata();

// GitHub Issue: https://github.com/Azure/azure-iot-sdk-csharp/issues/1971
// When we clear the metadata from the underlying collection we need to also clear
// the _metadata object so the TryGetMemberInternal will return a JObject instead of a new TwinCollection
Expand Down
25 changes: 6 additions & 19 deletions provisioning/service/src/Twin/TwinCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public IEnumerator GetEnumerator()
///
/// For example a <see cref="List{T}"/> would return a <see cref="TwinCollectionArray"/>, with the metadata intact, when used with
/// a <see cref="TwinCollection"/> returned from a `IotHubServiceClient`.`TwinsClient`. If you need this method to always return a
/// <see cref="JToken"/> please see the <see cref="ClearAllMetadata"/> method for more information.
/// <see cref="JToken"/> please see the <see cref="ClearMetadata"/> method for more information.
/// </remarks>
private bool TryGetMemberInternal(string propertyName, out object result)
{
Expand Down Expand Up @@ -286,33 +286,20 @@ private void TryClearMetadata(string propertyName)
}

/// <summary>
/// Clears metadata out of the twin collection.
/// Clears all metadata out of the twin collection as well as the base metadata object.
/// </summary>
/// <remarks>
/// This will only clear the metadata from the twin collection but will not change the base metadata object. This allows you to still use
/// methods such as <see cref="GetMetadata"/>. If you need to remove all metadata, please use <see cref="ClearAllMetadata"/>.
/// This will remove all metadata from the base metadata object as well as the metadata for the twin collection.
/// This will also clear the underlying metadata object which will affect methods such as
/// <see cref="GetMetadata"/> and <see cref="GetLastUpdatedVersion"/>.
/// </remarks>
public void ClearMetadata()
{
TryClearMetadata(MetadataName);
TryClearMetadata(LastUpdatedName);
TryClearMetadata(LastUpdatedVersionName);
TryClearMetadata(VersionName);
}

/// <summary>
/// Clears all metadata out of the twin collection as well as the base metadata object.
/// </summary>
/// <remarks>
/// This will remove all metadata from the base metadata object as well as the metadata for the twin collection. The difference from the
/// <see cref="ClearMetadata"/> method is this will also clear the underlying metadata object which will affect methods such as
/// <see cref="GetMetadata"/> and <see cref="GetLastUpdatedVersion"/>.
/// This method would be useful if you are performing any operations that require <see cref="TryGetMemberInternal(string, out object)"/>
/// to return a <see cref="JToken"/> regardless of the client you are using.
/// </remarks>
public void ClearAllMetadata()
{
ClearMetadata();

// GitHub Issue: https://github.com/Azure/azure-iot-sdk-csharp/issues/1971
// When we clear the metadata from the underlying collection we need to also clear
// the _metadata object so the TryGetMemberInternal will return a JObject instead of a new TwinCollection
Expand Down

0 comments on commit a920528

Please sign in to comment.