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

remove ph classes #21852

Merged
merged 3 commits into from
Jun 14, 2021
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
@@ -0,0 +1,67 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Azure.ResourceManager.Core
{
/// <summary>
/// Returns an AsyncPageable that transforms each page of contents after they are retrieved from the server
/// according to the profived transformation function
/// </summary>
/// <typeparam name="TModel"> The model returned by existing AsyncPageable methods. </typeparam>
/// <typeparam name="TOperations"> The <see cref="ResourceOperationsBase"/> to convert TModel into. </typeparam>
public class PhWrappingAsyncPageable<TModel, TOperations> : AsyncPageable<TOperations>
where TOperations : class
where TModel : class
{
private readonly Func<TModel, TOperations> _converter;
private readonly IEnumerable<AsyncPageable<TModel>> _wrapped;

/// <summary>
/// Initializes a new instance of the <see cref="PhWrappingAsyncPageable{TModel, TOperations}"/> class for mocking.
/// </summary>
protected PhWrappingAsyncPageable()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="PhWrappingAsyncPageable{TModel, TOperations}"/> class.
/// </summary>
/// <param name="wrapped"> The results to wrap. </param>
/// <param name="converter"> The function used to convert from existing type to new type. </param>
public PhWrappingAsyncPageable(AsyncPageable<TModel> wrapped, Func<TModel, TOperations> converter)
{
_wrapped = new[] { wrapped };
_converter = converter;
}

/// <summary>
/// Initializes a new instance of the <see cref="PhWrappingAsyncPageable{TModel, TOperations}"/> class.
/// </summary>
/// <param name="wrapped"> The results to wrap. </param>
/// <param name="converter"> The function used to convert from existing type to new type. </param>
public PhWrappingAsyncPageable(IEnumerable<AsyncPageable<TModel>> wrapped, Func<TModel, TOperations> converter)
{
_wrapped = wrapped;
_converter = converter;
}

/// <inheritdoc/>
public override async IAsyncEnumerable<Page<TOperations>> AsPages(
string continuationToken = null,
int? pageSizeHint = null)
{
foreach (var pageEnum in _wrapped)
{
await foreach (var page in pageEnum.AsPages().WithCancellation(CancellationToken))
{
yield return new WrappingPage<TModel, TOperations>(page, _converter);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;

namespace Azure.ResourceManager.Core
{
/// <summary>
/// This class allows performing conversions on pages of data as they are accessed - used in the prototype to convett
/// between underlying model types and the new model types that extend Resource,
/// and also for returning Operations classes for those underlying objects.
/// </summary>
/// <typeparam name="TModel"> The type parameter of the Pageable we are wrapping. </typeparam>
/// <typeparam name="TOperations"> The desired type parameter of the returned pageable. </typeparam>
public class PhWrappingPageable<TModel, TOperations> : Pageable<TOperations>
where TOperations : class
where TModel : class
{
private readonly Func<TModel, TOperations> _converter;
private readonly IEnumerable<Pageable<TModel>> _wrapped;

/// <summary>
/// Initializes a new instance of the <see cref="PhWrappingPageable{TModel, TOperations}"/> class.
/// </summary>
/// <param name="wrapped"> The results to wrap. </param>
/// <param name="converter"> The function used to convert from existing type to new type. </param>
public PhWrappingPageable(Pageable<TModel> wrapped, Func<TModel, TOperations> converter)
{
_wrapped = new[] { wrapped };
_converter = converter;
}

/// <summary>
/// Initializes a new instance of the <see cref="PhWrappingPageable{TModel, TOperations}"/> class.
/// </summary>
/// <param name="wrapped"> The results to wrap. </param>
/// <param name="converter"> The function used to convert from existing type to new type. </param>
public PhWrappingPageable(IEnumerable<Pageable<TModel>> wrapped, Func<TModel, TOperations> converter)
{
_wrapped = wrapped;
_converter = converter;
}

/// <inheritdoc/>
public override IEnumerable<Page<TOperations>> AsPages(string continuationToken = null, int? pageSizeHint = null)
{
foreach (var pages in _wrapped)
{
foreach (var page in pages.AsPages())
{
yield return new WrappingPage<TModel, TOperations>(page, _converter);
}
}
}
}
}
45 changes: 45 additions & 0 deletions sdk/resourcemanager/Proto.Client/network/Adapters/WrappingPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;

namespace Azure.ResourceManager.Core
{
/// <summary>
/// A class representing a wrapper over <see cref="Page{TOperations}"/>
/// </summary>
/// <typeparam name="TModel"> The type parameter we are wrapping. </typeparam>
/// <typeparam name="TOperations"> The desired type parameter of the returned page. </typeparam>
internal class WrappingPage<TModel, TOperations> : Page<TOperations>
where TOperations : class
where TModel : class
{
private readonly Func<TModel, TOperations> _converter;
private readonly Page<TModel> _wrapped;

/// <summary>
/// Initializes a new instance of the <see cref="WrappingPage{TModel, TOperations}"/> class.
/// </summary>
/// <param name="wrapped"> The results to wrap. </param>
/// <param name="converter"> The function used to convert from existing type to new type. </param>
internal WrappingPage(Page<TModel> wrapped, Func<TModel, TOperations> converter)
{
_wrapped = wrapped;
_converter = converter;
}

/// <inheritdoc/>
public override IReadOnlyList<TOperations> Values => _wrapped.Values.Select(_converter).ToList();

/// <inheritdoc/>
public override string ContinuationToken => _wrapped.ContinuationToken;

/// <inheritdoc/>
public override Response GetRawResponse()
{
return _wrapped.GetRawResponse();
}
}
}