Skip to content

Commit

Permalink
update internal files to work with autorest (#22972)
Browse files Browse the repository at this point in the history
* combine extension classes

* updates to more comments

* make autorest attributes internal

* update api

* update based on pr feedback

* update internal types to work with autorest

* updates after merge
  • Loading branch information
m-nash authored Jul 30, 2021
1 parent e493995 commit 562d181
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 101 deletions.
2 changes: 2 additions & 0 deletions common/ManagementCoreShared/ClientContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable enable

using System;
using Azure.Core;
using Azure.Core.Pipeline;
Expand Down
35 changes: 0 additions & 35 deletions common/ManagementCoreShared/ReferenceTypeAttribute.cs

This file was deleted.

55 changes: 4 additions & 51 deletions common/ManagementCoreShared/SharedExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable enable

using System;
using System.Collections.Generic;
using System.Threading;
Expand All @@ -13,35 +15,6 @@ namespace Azure.ResourceManager.Core
/// </summary>
internal static class SharedExtensions
{
/// <summary>
/// Add a provider resource to an existing resource id.
/// </summary>
/// <param name="identifier"> The id to append to. </param>
/// <param name="providerNamespace"> The provider namespace of the added resource. </param>
/// <param name="resourceType"> The simple type of the added resource, without slashes (/),
/// for example, 'virtualMachines'. </param>
/// <param name="resourceName"> The name of the resource.</param>
/// <returns> The combined resource id. </returns>
public static ResourceIdentifier AppendProviderResource(this ResourceIdentifier identifier, string providerNamespace, string resourceType, string resourceName)
{
ValidateProviderResourceParameters(providerNamespace, resourceType, resourceName);
return new ResourceIdentifier(identifier, providerNamespace, resourceType, resourceName);
}

/// <summary>
/// Add a provider resource to an existing resource id.
/// </summary>
/// <param name="identifier"> The id to append to. </param>
/// <param name="childResourceType"> The simple type of the child resource, without slashes (/),
/// for example, 'subnets'. </param>
/// <param name="childResourceName"> The name of the resource. </param>
/// <returns> The combined resource id. </returns>
public static ResourceIdentifier AppendChildResource(this ResourceIdentifier identifier, string childResourceType, string childResourceName)
{
ValidateChildResourceParameters(childResourceType, childResourceName);
return new ResourceIdentifier(identifier, childResourceType, childResourceName);
}

/// <summary>
/// An extension method for supporting replacing one dictionary content with another one.
/// This is used to support resource tags.
Expand Down Expand Up @@ -70,10 +43,11 @@ public static List<T> Trim<T>(this List<T> list, int numberToTrim)
return list;
}

public static async Task<TSource> FirstOrDefaultAsync<TSource>(
public static async Task<TSource?> FirstOrDefaultAsync<TSource>(
this AsyncPageable<TSource> source,
Func<TSource, bool> predicate,
CancellationToken token = default)
where TSource : notnull
{
if (source == null)
throw new ArgumentNullException(nameof(source));
Expand All @@ -94,26 +68,5 @@ public static async Task<TSource> FirstOrDefaultAsync<TSource>(

return default;
}

private static void ValidateProviderResourceParameters(string providerNamespace, string resourceType, string resourceName)
{
ValidatePathSegment(providerNamespace, nameof(providerNamespace));
ValidatePathSegment(resourceType, nameof(resourceType));
ValidatePathSegment(resourceName, nameof(resourceName));
}

private static void ValidateChildResourceParameters(string childResourceType, string childResourceName)
{
ValidatePathSegment(childResourceType, nameof(childResourceType));
ValidatePathSegment(childResourceName, nameof(childResourceName));
}

private static void ValidatePathSegment(string segment, string parameterName)
{
if (string.IsNullOrWhiteSpace(segment))
throw new ArgumentNullException(parameterName);
if (segment.Contains("/"))
throw new ArgumentOutOfRangeException(parameterName, $"{parameterName} must be a single path segment");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ public static partial class ResourceListOperations
}
public static partial class ResourceManagerExtensions
{
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public static Azure.ResourceManager.ResourceIdentifier AppendChildResource(this Azure.ResourceManager.ResourceIdentifier identifier, string childResourceType, string childResourceName) { throw null; }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public static Azure.ResourceManager.ResourceIdentifier AppendProviderResource(this Azure.ResourceManager.ResourceIdentifier identifier, string providerNamespace, string resourceType, string resourceName) { throw null; }
public static string GetCorrelationId(this Azure.Response response) { throw null; }
public static Azure.Response WaitForCompletion(this Azure.Operation operation, System.Threading.CancellationToken cancellationToken) { throw null; }
public static Azure.Response WaitForCompletion(this Azure.Operation operation, System.TimeSpan pollingInterval, System.Threading.CancellationToken cancellationToken) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
<Compile Include="$(AzureCoreSharedSources)ForwardsClientCallsAttribute.cs" Link="Shared\Core\%(RecursiveDir)\%(Filename)%(Extension)" />
<Compile Include="$(AzureCoreSharedSources)HashCodeBuilder.cs" Link="Shared\Core\%(RecursiveDir)\%(Filename)%(Extension)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,5 @@ namespace Azure.ResourceManager.Core
[AttributeUsage(AttributeTargets.Constructor)]
internal class InitializationConstructorAttribute : Attribute
{
/// <summary>
/// Instatiate a new InitializationConstructor attribute.
/// </summary>
public InitializationConstructorAttribute()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;

namespace Azure.ResourceManager.Core
{
Expand All @@ -24,7 +23,8 @@ public PropertyReferenceTypeAttribute(Type[] skipTypes)
/// <summary>
/// Instatiate a new reference type attribute.
/// </summary>
public PropertyReferenceTypeAttribute() : this(null)
public PropertyReferenceTypeAttribute()
: this(Array.Empty<Type>())
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Azure.ResourceManager.Core
{
/// <summary>
/// An attribute class indicating a reference type for code generation.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
internal class ReferenceTypeAttribute : Attribute
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,5 @@ namespace Azure.ResourceManager.Core
[AttributeUsage(AttributeTargets.Constructor)]
internal class SerializationConstructorAttribute : Attribute
{
/// <summary>
/// Instatiate a new SerializationConstructor attribute.
/// </summary>
public SerializationConstructorAttribute()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
Expand All @@ -13,6 +14,37 @@ namespace Azure.ResourceManager.Core
/// </summary>
public static class ResourceManagerExtensions
{
/// <summary>
/// Add a provider resource to an existing resource id.
/// </summary>
/// <param name="identifier"> The id to append to. </param>
/// <param name="providerNamespace"> The provider namespace of the added resource. </param>
/// <param name="resourceType"> The simple type of the added resource, without slashes (/),
/// for example, 'virtualMachines'. </param>
/// <param name="resourceName"> The name of the resource.</param>
/// <returns> The combined resource id. </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public static ResourceIdentifier AppendProviderResource(this ResourceIdentifier identifier, string providerNamespace, string resourceType, string resourceName)
{
ValidateProviderResourceParameters(providerNamespace, resourceType, resourceName);
return new ResourceIdentifier(identifier, providerNamespace, resourceType, resourceName);
}

/// <summary>
/// Add a provider resource to an existing resource id.
/// </summary>
/// <param name="identifier"> The id to append to. </param>
/// <param name="childResourceType"> The simple type of the child resource, without slashes (/),
/// for example, 'subnets'. </param>
/// <param name="childResourceName"> The name of the resource. </param>
/// <returns> The combined resource id. </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public static ResourceIdentifier AppendChildResource(this ResourceIdentifier identifier, string childResourceType, string childResourceName)
{
ValidateChildResourceParameters(childResourceType, childResourceName);
return new ResourceIdentifier(identifier, childResourceType, childResourceName);
}

/// <summary>
/// Waits for the completion of the long running operations.
/// </summary>
Expand Down Expand Up @@ -86,5 +118,26 @@ public static string GetCorrelationId(this Response response)
response.Headers.TryGetValue("x-ms-correlation-request-id", out correlationId);
return correlationId;
}

private static void ValidateProviderResourceParameters(string providerNamespace, string resourceType, string resourceName)
{
ValidatePathSegment(providerNamespace, nameof(providerNamespace));
ValidatePathSegment(resourceType, nameof(resourceType));
ValidatePathSegment(resourceName, nameof(resourceName));
}

private static void ValidateChildResourceParameters(string childResourceType, string childResourceName)
{
ValidatePathSegment(childResourceType, nameof(childResourceType));
ValidatePathSegment(childResourceName, nameof(childResourceName));
}

private static void ValidatePathSegment(string segment, string parameterName)
{
if (string.IsNullOrWhiteSpace(segment))
throw new ArgumentNullException(parameterName);
if (segment.Contains("/"))
throw new ArgumentOutOfRangeException(parameterName, $"{parameterName} must be a single path segment");
}
}
}

0 comments on commit 562d181

Please sign in to comment.