Skip to content

Commit

Permalink
update to cache method providers (microsoft#3611)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-nash authored Jun 18, 2024
1 parent 8e38930 commit 04a389f
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Microsoft.Generator.CSharp.ClientModel
{
internal class ScmTypeFactory : TypeFactory
{
private Dictionary<InputOperation, MethodProviderCollection?> _operations = new Dictionary<InputOperation, MethodProviderCollection?>();

/// <summary>
/// This method will attempt to retrieve the <see cref="CSharpType"/> of the input type.
/// </summary>
Expand Down Expand Up @@ -71,33 +73,39 @@ public override ParameterProvider CreateCSharpParam(InputParameter inputParamete
}

/// <summary>
/// Creates a <see cref="CSharpMethodCollection"/> for the given operation. If the operation is a <see cref="OperationKinds.DefaultValue"/> operation,
/// Creates a <see cref="MethodProviderCollection"/> for the given operation. If the operation is a <see cref="InputOperationKinds.DefaultValue"/> operation,
/// a method collection will be created consisting of a <see cref="CSharpMethodKinds.CreateMessage"/> method. Otherwise, <c>null</c> will be returned.
/// </summary>
/// <param name="operation">The input operation to create methods for.</param>
/// <param name="enclosingType">The enclosing type of the operation.</param>
public override CSharpMethodCollection? CreateCSharpMethodCollection(InputOperation operation, TypeProvider enclosingType)
public override MethodProviderCollection? CreateMethodProviders(InputOperation operation, TypeProvider enclosingType)
{
switch (GetOperationKind(operation))
if (_operations.TryGetValue(operation, out var methods))
{
case var value when value == OperationKinds.Default:
return CSharpMethodCollection.DefaultCSharpMethodCollection(operation, enclosingType);
default:
return null;
return methods;
}

methods = GetOperationKind(operation).ToString() switch
{
"Default" => MethodProviderCollection.DefaultCSharpMethodCollection(operation, enclosingType),
_ => null,
};

_operations.Add(operation, methods);
return methods;
}

/// <summary>
/// Returns the <see cref="OperationKinds"/> of the given operation.
/// By default, the operation kind is <see cref="OperationKinds.Default"/>.
/// Returns the <see cref="InputOperationKinds"/> of the given operation.
/// By default, the operation kind is <see cref="InputOperationKinds.Default"/>.
/// </summary>
private static OperationKinds GetOperationKind(InputOperation operation)
private static InputOperationKinds GetOperationKind(InputOperation operation)
{
return operation switch
{
{ LongRunning: { } } => OperationKinds.LongRunning,
{ Paging: { } } => OperationKinds.Paging,
_ => OperationKinds.Default,
{ LongRunning: { } } => InputOperationKinds.LongRunning,
{ Paging: { } } => InputOperationKinds.Paging,
_ => InputOperationKinds.Default,
};
}

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

using System;
using System.ComponentModel;

namespace Microsoft.Generator.CSharp.Input
{
/// <summary>
/// The kind of operation.
/// </summary>
public readonly partial struct InputOperationKinds : IEquatable<InputOperationKinds>
{
internal const string DefaultValue = "Default";
internal const string LongRunningValue = "LongRunning";
internal const string PagingValue = "Paging";

private readonly string _value;

/// <summary> Initializes a new instance of <see cref="InputOperationKinds"/>. </summary>
/// <exception cref="ArgumentNullException"> <paramref name="value"/> is null. </exception>
public InputOperationKinds(string value)
{
_value = value ?? throw new ArgumentNullException(nameof(value));
}

/// <summary>
/// Default operation kind.
/// </summary>
public static InputOperationKinds Default { get; } = new InputOperationKinds(DefaultValue);

/// <summary>
/// LongRunning operation kind.
/// </summary>
public static InputOperationKinds LongRunning { get; } = new InputOperationKinds(LongRunningValue);

/// <summary>
/// Paging operation kind.
/// </summary>
public static InputOperationKinds Paging { get; } = new InputOperationKinds(PagingValue);

/// <summary> Determines if two <see cref="InputOperationKinds"/> values are the same. </summary>
public static bool operator ==(InputOperationKinds left, InputOperationKinds right) => left.Equals(right);
/// <summary> Determines if two <see cref="InputOperationKinds"/> values are not the same. </summary>
public static bool operator !=(InputOperationKinds left, InputOperationKinds right) => !left.Equals(right);

/// <summary> Converts a string to a <see cref="InputOperationKinds"/>.</summary>
/// <param name="value">The string value to convert.</param>
public static implicit operator InputOperationKinds(string value) => new(value);

/// <inheritdoc />
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object? obj) => obj is InputOperationKinds other && Equals(other);

/// <inheritdoc />
public bool Equals(InputOperationKinds other) => string.Equals(_value, other._value, StringComparison.InvariantCultureIgnoreCase);

/// <inheritdoc/>
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => _value?.GetHashCode() ?? 0;

/// <inheritdoc />
public override string ToString() => _value;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ internal class OperationKindsTests
[Test]
public void TestCustomKind()
{
var customKind = new OperationKinds("CustomKind");
var customKind = new InputOperationKinds("CustomKind");
Assert.IsTrue("CustomKind" == customKind);
}

[Test]
public void TestEquals()
{
Assert.IsTrue("Default" == OperationKinds.Default);
Assert.IsTrue("LongRunning" == OperationKinds.LongRunning);
Assert.IsTrue("Paging" == OperationKinds.Paging);
Assert.IsTrue("Default" == InputOperationKinds.Default);
Assert.IsTrue("LongRunning" == InputOperationKinds.LongRunning);
Assert.IsTrue("Paging" == InputOperationKinds.Paging);

Assert.IsFalse(OperationKinds.Default == OperationKinds.LongRunning);
Assert.IsFalse(OperationKinds.LongRunning == OperationKinds.Paging);
Assert.IsFalse(OperationKinds.Paging == OperationKinds.Default);
Assert.IsFalse(InputOperationKinds.Default == InputOperationKinds.LongRunning);
Assert.IsFalse(InputOperationKinds.LongRunning == InputOperationKinds.Paging);
Assert.IsFalse(InputOperationKinds.Paging == InputOperationKinds.Default);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public abstract class TypeFactory
public abstract ParameterProvider CreateCSharpParam(InputParameter parameter);

/// <summary>
/// Factory method for creating a <see cref="CSharpMethodCollection"/> based on an input operation <paramref name="operation"/>.
/// Factory method for creating a <see cref="MethodProviderCollection"/> based on an input operation <paramref name="operation"/>.
/// </summary>
/// <param name="operation">The <see cref="InputOperation"/> to convert.</param>
/// <param name="enclosingType">The <see cref="TypeProvider"/> that will contain the methods.</param>
/// <returns>An instance of <see cref="CSharpMethodCollection"/> containing the chain of methods
/// <returns>An instance of <see cref="MethodProviderCollection"/> containing the chain of methods
/// associated with the input operation, or <c>null</c> if no methods are constructed.
/// </returns>
public abstract CSharpMethodCollection? CreateCSharpMethodCollection(InputOperation operation, TypeProvider enclosingType);
public abstract MethodProviderCollection? CreateMethodProviders(InputOperation operation, TypeProvider enclosingType);

/// <summary>
/// Factory method for retrieving the serialization format for a given input type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override MethodProvider[] BuildMethods()
// Build methods for all the operations
foreach (var operation in _inputClient.Operations)
{
var methodCollection = CodeModelPlugin.Instance.TypeFactory.CreateCSharpMethodCollection(operation, this);
var methodCollection = CodeModelPlugin.Instance.TypeFactory.CreateMethodProviders(operation, this);
if (methodCollection != null)
{
methods.AddRange(methodCollection);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Generator.CSharp.Input;
using Microsoft.Generator.CSharp.Providers;
using Microsoft.Generator.CSharp.Snippets;

namespace Microsoft.Generator.CSharp
namespace Microsoft.Generator.CSharp.Providers
{
/// <summary>
/// Represents an immutable collection of methods that are associated with an operation <see cref="InputOperation"/>.
/// </summary>
public sealed class CSharpMethodCollection : IReadOnlyList<MethodProvider>
public sealed class MethodProviderCollection : IReadOnlyList<MethodProvider>
{
private readonly IReadOnlyList<MethodProvider> _cSharpMethods;

private CSharpMethodCollection(IReadOnlyList<MethodProvider> methods)
private MethodProviderCollection(IEnumerable<MethodProvider>? methods)
{
_cSharpMethods = methods ?? Array.Empty<MethodProvider>();
_cSharpMethods = methods?.ToList() ?? [];
}

/// <summary>
/// Builds a default <see cref="CSharpMethodCollection"/> for the given <see cref="InputOperation"/>
/// Builds a default <see cref="MethodProviderCollection"/> for the given <see cref="InputOperation"/>
/// with a single method that creates a message.
/// </summary>
/// <param name="operation">The <see cref="InputOperation"/> to convert.</param>
/// <param name="enclosingType">The <see cref="TypeProvider"/> that will contain the methods.</param>
public static CSharpMethodCollection DefaultCSharpMethodCollection(InputOperation operation, TypeProvider enclosingType)
public static MethodProviderCollection DefaultCSharpMethodCollection(InputOperation operation, TypeProvider enclosingType)
{
var createMessageMethod = BuildCreateMessageMethod(operation, enclosingType);
var cSharpMethods = new List<MethodProvider>() { createMessageMethod };
// TO-DO: Add Protocol and Convenience methods https://github.com/Azure/autorest.csharp/issues/4585, https://github.com/Azure/autorest.csharp/issues/4586
return new CSharpMethodCollection(cSharpMethods);
return new MethodProviderCollection(cSharpMethods);
}

public MethodProvider this[int index]
Expand Down Expand Up @@ -63,20 +62,6 @@ private static MethodProvider BuildCreateMessageMethod(InputOperation operation,
return new MethodProvider(methodSignature, methodBody, enclosingType);
}

/// <summary>
/// Returns all methods in the collection of a specific kind.
/// </summary>
internal List<MethodProvider> GetMethods()
{
var methods = new List<MethodProvider>();
foreach (var method in _cSharpMethods)
{
methods.Add(method);
}

return methods;
}

public IEnumerator<MethodProvider> GetEnumerator()
{
return _cSharpMethods.GetEnumerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Teardown()
public void TestDefaultCSharpMethodCollection(InputOperation inputOperation)
{

var methodCollection = CSharpMethodCollection.DefaultCSharpMethodCollection(inputOperation, new MockTypeProvider());
var methodCollection = MethodProviderCollection.DefaultCSharpMethodCollection(inputOperation, new MockTypeProvider());
Assert.IsNotNull(methodCollection);
Assert.AreEqual(1, methodCollection?.Count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.Generator.CSharp.Tests
{
internal class MockTypeFactory : TypeFactory
{
public override CSharpMethodCollection? CreateCSharpMethodCollection(InputOperation operation, TypeProvider enclosingType)
public override MethodProviderCollection? CreateMethodProviders(InputOperation operation, TypeProvider enclosingType)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public override ParameterProvider CreateCSharpParam(InputParameter parameter)
throw new NotImplementedException();
}

public override CSharpMethodCollection? CreateCSharpMethodCollection(InputOperation operation, TypeProvider enclosingType)
public override MethodProviderCollection? CreateMethodProviders(InputOperation operation, TypeProvider enclosingType)
{
throw new NotImplementedException();
}
Expand Down

0 comments on commit 04a389f

Please sign in to comment.