Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Autorest C# - IOT #13558

Merged
merged 3 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ internal CloudToDeviceMethodResponse() { }
public partial class Components10Jnwi5SchemasDigitaltwininterfacespatchPropertiesInterfacesAdditionalproperties
{
public Components10Jnwi5SchemasDigitaltwininterfacespatchPropertiesInterfacesAdditionalproperties() { }
public System.Collections.Generic.IDictionary<string, Azure.Iot.Hub.Service.Models.Components17Cpi2FSchemasDigitaltwininterfacespatchPropertiesInterfacesAdditionalpropertiesPropertiesAdditionalproperties> Properties { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, Azure.Iot.Hub.Service.Models.Components17Cpi2FSchemasDigitaltwininterfacespatchPropertiesInterfacesAdditionalpropertiesPropertiesAdditionalproperties> Properties { get { throw null; } }
}
public partial class Components17Cpi2FSchemasDigitaltwininterfacespatchPropertiesInterfacesAdditionalpropertiesPropertiesAdditionalproperties
{
Expand All @@ -171,20 +171,20 @@ public Components17Cpi2FSchemasDigitaltwininterfacespatchPropertiesInterfacesAdd
public partial class ConfigurationContent
{
public ConfigurationContent() { }
public System.Collections.Generic.IDictionary<string, object> DeviceContent { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, object> ModuleContent { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, object> ModulesContent { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, object> DeviceContent { get { throw null; } }
public System.Collections.Generic.IDictionary<string, object> ModuleContent { get { throw null; } }
public System.Collections.Generic.IDictionary<string, object> ModulesContent { get { throw null; } }
}
public partial class ConfigurationMetrics
{
public ConfigurationMetrics() { }
public System.Collections.Generic.IDictionary<string, string> Queries { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, long> Results { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, string> Queries { get { throw null; } }
public System.Collections.Generic.IDictionary<string, long> Results { get { throw null; } }
}
public partial class ConfigurationQueriesTestInput
{
public ConfigurationQueriesTestInput() { }
public System.Collections.Generic.IDictionary<string, string> CustomMetricQueries { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, string> CustomMetricQueries { get { throw null; } }
public string TargetCondition { get { throw null; } set { } }
}
public partial class ConfigurationQueriesTestResponse
Expand Down Expand Up @@ -236,7 +236,7 @@ public DeviceIdentity() { }
public string Etag { get { throw null; } set { } }
public string GenerationId { get { throw null; } set { } }
public System.DateTimeOffset? LastActivityTime { get { throw null; } set { } }
public System.Collections.Generic.IList<string> ParentScopes { get { throw null; } set { } }
public System.Collections.Generic.IList<string> ParentScopes { get { throw null; } }
public Azure.Iot.Hub.Service.Models.DeviceStatus? Status { get { throw null; } set { } }
public string StatusReason { get { throw null; } set { } }
public System.DateTimeOffset? StatusUpdatedTime { get { throw null; } set { } }
Expand Down Expand Up @@ -518,7 +518,7 @@ internal DeviceRegistryOperationWarning() { }
public partial class DigitalTwinInterfacesPatch
{
public DigitalTwinInterfacesPatch() { }
public System.Collections.Generic.IDictionary<string, Azure.Iot.Hub.Service.Models.Components10Jnwi5SchemasDigitaltwininterfacespatchPropertiesInterfacesAdditionalproperties> Interfaces { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, Azure.Iot.Hub.Service.Models.Components10Jnwi5SchemasDigitaltwininterfacespatchPropertiesInterfacesAdditionalproperties> Interfaces { get { throw null; } }
}
public partial class DigitalTwinInterfacesPatchInterfacesPropertiesAdditionalPropertiesDesired
{
Expand All @@ -535,11 +535,11 @@ public ExportImportDevice() { }
public string Id { get { throw null; } set { } }
public Azure.Iot.Hub.Service.Models.ExportImportDeviceImportMode? ImportMode { get { throw null; } set { } }
public string ModuleId { get { throw null; } set { } }
public System.Collections.Generic.IList<string> ParentScopes { get { throw null; } set { } }
public System.Collections.Generic.IList<string> ParentScopes { get { throw null; } }
public Azure.Iot.Hub.Service.Models.PropertyContainer Properties { get { throw null; } set { } }
public Azure.Iot.Hub.Service.Models.ExportImportDeviceStatus? Status { get { throw null; } set { } }
public string StatusReason { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, object> Tags { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, object> Tags { get { throw null; } }
public string TwinETag { get { throw null; } set { } }
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
Expand Down Expand Up @@ -851,8 +851,8 @@ public ModuleIdentity() { }
public partial class PropertyContainer
{
public PropertyContainer() { }
public System.Collections.Generic.IDictionary<string, object> Desired { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, object> Reported { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, object> Desired { get { throw null; } }
public System.Collections.Generic.IDictionary<string, object> Reported { get { throw null; } }
}
public partial class PurgeMessageQueueResult
{
Expand Down Expand Up @@ -942,7 +942,7 @@ public TwinConfiguration() { }
public System.DateTimeOffset? CreatedTimeUtc { get { throw null; } set { } }
public string Etag { get { throw null; } set { } }
public string Id { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, string> Labels { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, string> Labels { get { throw null; } }
public System.DateTimeOffset? LastUpdatedTimeUtc { get { throw null; } set { } }
public Azure.Iot.Hub.Service.Models.ConfigurationMetrics Metrics { get { throw null; } set { } }
public int? Priority { get { throw null; } set { } }
Expand Down Expand Up @@ -981,20 +981,20 @@ public TwinData() { }
public string Etag { get { throw null; } set { } }
public System.DateTimeOffset? LastActivityTime { get { throw null; } set { } }
public string ModuleId { get { throw null; } set { } }
public System.Collections.Generic.IList<string> ParentScopes { get { throw null; } set { } }
public System.Collections.Generic.IList<string> ParentScopes { get { throw null; } }
public Azure.Iot.Hub.Service.Models.TwinProperties Properties { get { throw null; } set { } }
public Azure.Iot.Hub.Service.Models.TwinStatus? Status { get { throw null; } set { } }
public string StatusReason { get { throw null; } set { } }
public System.DateTimeOffset? StatusUpdateTime { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, object> Tags { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, object> Tags { get { throw null; } }
public long? Version { get { throw null; } set { } }
public Azure.Iot.Hub.Service.Models.X509Thumbprint X509Thumbprint { get { throw null; } set { } }
}
public partial class TwinProperties
{
public TwinProperties() { }
public System.Collections.Generic.IDictionary<string, object> Desired { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, object> Reported { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, object> Desired { get { throw null; } }
public System.Collections.Generic.IDictionary<string, object> Reported { get { throw null; } }
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public readonly partial struct TwinStatus : System.IEquatable<Azure.Iot.Hub.Service.Models.TwinStatus>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<EnableApiCompat>false</EnableApiCompat>
<!-- These supressions should be removed in a production library -->
<NoWarn>$(NoWarn);CA1812;1591</NoWarn>
<TemporaryUsePreviousGeneratorVersion>true</TemporaryUsePreviousGeneratorVersion><!-- https://github.com/Azure/azure-sdk-for-net/issues/13506 -->
</PropertyGroup>

<!-- Nuget properties -->
Expand Down
30 changes: 8 additions & 22 deletions sdk/iot/Azure.Iot.Hub.Service/src/DevicesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,12 @@ public virtual Task<Response<BulkRegistryOperationResponse>> CreateIdentitiesWit
Authentication = x.Key.Authentication,
Capabilities = x.Key.Capabilities,
DeviceScope = x.Key.DeviceScope,
ParentScopes = x.Key.ParentScopes,
Status = string.Equals(ExportImportDeviceStatus.Disabled.ToString(), x.Key.Status?.ToString(), StringComparison.OrdinalIgnoreCase)
? ExportImportDeviceStatus.Disabled
: ExportImportDeviceStatus.Enabled,
StatusReason = x.Key.StatusReason,
Tags = x.Value.Tags,
Properties = new PropertyContainer(x.Value.Properties?.Desired, x.Value.Properties?.Reported),
ImportMode = ExportImportDeviceImportMode.Create
});
}.WithTags(x.Value.Tags).WithPropertiesFrom(x.Value.Properties).WithParentScopes(x.Key.ParentScopes));
Copy link
Member

@abhipsaMisra abhipsaMisra Jul 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per # 1: So this is because these fields, being collections, are readonly by default, and so they need additional helper methods?
https://github.com/pakrym/azure-sdk-for-net/blob/pakrym/update-generator-IOT/sdk/iot/Azure.Iot.Hub.Service/src/Generated/Models/ExportImportDevice.cs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, what was the driving force behind this change? Should collections not be settable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following guideline:

https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/guidelines-for-collections#collection-properties-and-return-values

❌ DO NOT provide settable collection properties.

Users can replace the contents of the collection by clearing the collection first and then adding the new contents.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class can probably be internal. Will check and let you know. If it is internal, we would not need the extensions right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand why not. We would still need to create instances of this class and the code-gen for properties doesn't change depending on the class being internal or not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I want to add here that this is still work in progress and we are yet to make some classes internal which we do not expose. Just curious, is there something you use to be able to detect these in the API surface?


return _registryManagerClient.BulkDeviceCrudAsync(registryOperations, cancellationToken);
}
Expand All @@ -181,15 +178,12 @@ public virtual Response<BulkRegistryOperationResponse> CreateIdentitiesWithTwin(
Authentication = x.Key.Authentication,
Capabilities = x.Key.Capabilities,
DeviceScope = x.Key.DeviceScope,
ParentScopes = x.Key.ParentScopes,
Status = string.Equals(ExportImportDeviceStatus.Disabled.ToString(), x.Key.Status?.ToString(), StringComparison.OrdinalIgnoreCase)
? ExportImportDeviceStatus.Disabled
: ExportImportDeviceStatus.Enabled,
StatusReason = x.Key.StatusReason,
Tags = x.Value.Tags,
Properties = new PropertyContainer(x.Value.Properties?.Desired, x.Value.Properties?.Reported),
ImportMode = ExportImportDeviceImportMode.Create
});
}.WithTags(x.Value.Tags).WithPropertiesFrom(x.Value.Properties).WithParentScopes(x.Key.ParentScopes));

return _registryManagerClient.BulkDeviceCrud(registryOperations, cancellationToken);
}
Expand All @@ -209,13 +203,12 @@ public virtual Task<Response<BulkRegistryOperationResponse>> CreateIdentitiesAsy
Authentication = x.Authentication,
Capabilities = x.Capabilities,
DeviceScope = x.DeviceScope,
ParentScopes = x.ParentScopes,
Status = string.Equals(ExportImportDeviceStatus.Disabled.ToString(), x.Status?.ToString(), StringComparison.OrdinalIgnoreCase)
? ExportImportDeviceStatus.Disabled
: ExportImportDeviceStatus.Enabled,
StatusReason = x.StatusReason,
ImportMode = ExportImportDeviceImportMode.Create
});
}.WithParentScopes(x.ParentScopes));

return _registryManagerClient.BulkDeviceCrudAsync(registryOperations, cancellationToken);
}
Expand All @@ -235,13 +228,12 @@ public virtual Response<BulkRegistryOperationResponse> CreateIdentities(IEnumera
Authentication = x.Authentication,
Capabilities = x.Capabilities,
DeviceScope = x.DeviceScope,
ParentScopes = x.ParentScopes,
Status = string.Equals(ExportImportDeviceStatus.Disabled.ToString(), x.Status?.ToString(), StringComparison.OrdinalIgnoreCase)
? ExportImportDeviceStatus.Disabled
: ExportImportDeviceStatus.Enabled,
StatusReason = x.StatusReason,
ImportMode = ExportImportDeviceImportMode.Create
});
}.WithParentScopes(x.ParentScopes));

return _registryManagerClient.BulkDeviceCrud(registryOperations, cancellationToken);
}
Expand All @@ -265,14 +257,13 @@ public virtual Task<Response<BulkRegistryOperationResponse>> UpdateIdentitiesAsy
Authentication = x.Authentication,
Capabilities = x.Capabilities,
DeviceScope = x.DeviceScope,
ParentScopes = x.ParentScopes,
ETag = x.Etag,
Status = string.Equals(ExportImportDeviceStatus.Disabled.ToString(), x.Status?.ToString(), StringComparison.OrdinalIgnoreCase)
? ExportImportDeviceStatus.Disabled
: ExportImportDeviceStatus.Enabled,
StatusReason = x.StatusReason,
ImportMode = precondition == BulkIfMatchPrecondition.Unconditional ? ExportImportDeviceImportMode.Update : ExportImportDeviceImportMode.UpdateIfMatchETag
});
}.WithParentScopes(x.ParentScopes));

return _registryManagerClient.BulkDeviceCrudAsync(registryOperations, cancellationToken);
}
Expand All @@ -296,14 +287,13 @@ public virtual Response<BulkRegistryOperationResponse> UpdateIdentities(
Authentication = x.Authentication,
Capabilities = x.Capabilities,
DeviceScope = x.DeviceScope,
ParentScopes = x.ParentScopes,
ETag = x.Etag,
Status = string.Equals(ExportImportDeviceStatus.Disabled.ToString(), x.Status?.ToString(), StringComparison.OrdinalIgnoreCase)
? ExportImportDeviceStatus.Disabled
: ExportImportDeviceStatus.Enabled,
StatusReason = x.StatusReason,
ImportMode = precondition == BulkIfMatchPrecondition.Unconditional ? ExportImportDeviceImportMode.Update : ExportImportDeviceImportMode.UpdateIfMatchETag
});
}.WithParentScopes(x.ParentScopes));

return _registryManagerClient.BulkDeviceCrud(registryOperations, cancellationToken);
}
Expand Down Expand Up @@ -513,11 +503,9 @@ public virtual Task<Response<BulkRegistryOperationResponse>> UpdateTwinsAsync(
.Select(x => new ExportImportDevice()
{
Id = x.DeviceId,
Tags = x.Tags,
Properties = new PropertyContainer(x.Properties?.Desired, x.Properties?.Reported),
TwinETag = x.Etag,
ImportMode = precondition == BulkIfMatchPrecondition.Unconditional ? ExportImportDeviceImportMode.UpdateTwin : ExportImportDeviceImportMode.UpdateTwinIfMatchETag
});
}.WithTags(x.Tags).WithPropertiesFrom(x.Properties));

return _registryManagerClient.BulkDeviceCrudAsync(registryOperations, cancellationToken);
}
Expand All @@ -538,13 +526,11 @@ public virtual Response<BulkRegistryOperationResponse> UpdateTwins(
.Select(x => new ExportImportDevice()
{
Id = x.DeviceId,
Tags = x.Tags,
Properties = new PropertyContainer(x.Properties?.Desired, x.Properties?.Reported),
TwinETag = x.Etag,
ImportMode = precondition == BulkIfMatchPrecondition.Unconditional
? ExportImportDeviceImportMode.UpdateTwin
: ExportImportDeviceImportMode.UpdateTwinIfMatchETag
});
}.WithTags(x.Tags).WithPropertiesFrom(x.Properties));

return _registryManagerClient.BulkDeviceCrud(registryOperations, cancellationToken);
}
Expand Down
62 changes: 62 additions & 0 deletions sdk/iot/Azure.Iot.Hub.Service/src/ExportImportDeviceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using Azure.Iot.Hub.Service.Models;

namespace Azure.Iot.Hub.Service
{
/// <summary>
/// Helper functions for mutating the <see cref="ExportImportDevice"/> instance.
/// </summary>
internal static class ExportImportDeviceExtensions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this class handcrafted, or generated by autorest?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handcrafted.

{
/// <summary>
/// Initializes the <see cref="ExportImportDevice.Tags"/> property using provided values.
/// </summary>
public static ExportImportDevice WithTags(this ExportImportDevice device, IDictionary<string, object> tags)
{
foreach (var tag in tags)
{
device.Tags.Add(tag);
}
return device;
}

/// <summary>
/// Initializes the <see cref="ExportImportDevice.Properties"/> property using provided values.
/// </summary>
public static ExportImportDevice WithPropertiesFrom(this ExportImportDevice device, TwinProperties properties)
{

var container = new PropertyContainer();
if (properties != null)
{
foreach (var property in properties.Desired)
{
container.Desired.Add(property);
}
foreach (var property in properties.Reported)
{
container.Reported.Add(property);
}
}

device.Properties = container;

return device;
}

/// <summary>
/// Initializes the <see cref="ExportImportDevice.ParentScopes"/> property using provided values.
/// </summary>
public static ExportImportDevice WithParentScopes(this ExportImportDevice device, IList<string> parentScopes)
{
foreach (var parentScope in parentScopes)
{
device.ParentScopes.Add(parentScope);
}
return device;
}
}
}
Loading