diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/ScmTypeFactory.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/ScmTypeFactory.cs index 6802373ef9..686a0215d3 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/ScmTypeFactory.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/ScmTypeFactory.cs @@ -13,6 +13,8 @@ namespace Microsoft.Generator.CSharp.ClientModel { internal class ScmTypeFactory : TypeFactory { + private Dictionary _operations = new Dictionary(); + /// /// This method will attempt to retrieve the of the input type. /// @@ -71,33 +73,39 @@ public override ParameterProvider CreateCSharpParam(InputParameter inputParamete } /// - /// Creates a for the given operation. If the operation is a operation, + /// Creates a for the given operation. If the operation is a operation, /// a method collection will be created consisting of a method. Otherwise, null will be returned. /// /// The input operation to create methods for. /// The enclosing type of the operation. - 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; } /// - /// Returns the of the given operation. - /// By default, the operation kind is . + /// Returns the of the given operation. + /// By default, the operation kind is . /// - 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, }; } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/InputOperationKinds.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/InputOperationKinds.cs new file mode 100644 index 0000000000..8be4e8b796 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/InputOperationKinds.cs @@ -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 +{ + /// + /// The kind of operation. + /// + public readonly partial struct InputOperationKinds : IEquatable + { + internal const string DefaultValue = "Default"; + internal const string LongRunningValue = "LongRunning"; + internal const string PagingValue = "Paging"; + + private readonly string _value; + + /// Initializes a new instance of . + /// is null. + public InputOperationKinds(string value) + { + _value = value ?? throw new ArgumentNullException(nameof(value)); + } + + /// + /// Default operation kind. + /// + public static InputOperationKinds Default { get; } = new InputOperationKinds(DefaultValue); + + /// + /// LongRunning operation kind. + /// + public static InputOperationKinds LongRunning { get; } = new InputOperationKinds(LongRunningValue); + + /// + /// Paging operation kind. + /// + public static InputOperationKinds Paging { get; } = new InputOperationKinds(PagingValue); + + /// Determines if two values are the same. + public static bool operator ==(InputOperationKinds left, InputOperationKinds right) => left.Equals(right); + /// Determines if two values are not the same. + public static bool operator !=(InputOperationKinds left, InputOperationKinds right) => !left.Equals(right); + + /// Converts a string to a . + /// The string value to convert. + public static implicit operator InputOperationKinds(string value) => new(value); + + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public override bool Equals(object? obj) => obj is InputOperationKinds other && Equals(other); + + /// + public bool Equals(InputOperationKinds other) => string.Equals(_value, other._value, StringComparison.InvariantCultureIgnoreCase); + + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public override int GetHashCode() => _value?.GetHashCode() ?? 0; + + /// + public override string ToString() => _value; + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/OperationKinds.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/OperationKinds.cs deleted file mode 100644 index 359e2a37bd..0000000000 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/src/InputTypes/OperationKinds.cs +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.ComponentModel; - -namespace Microsoft.Generator.CSharp.Input -{ - /// - /// The kind of operation. - /// - public readonly partial struct OperationKinds : IEquatable - { - internal const string DefaultValue = "Default"; - internal const string LongRunningValue = "LongRunning"; - internal const string PagingValue = "Paging"; - - private readonly string _value; - - /// Initializes a new instance of . - /// is null. - public OperationKinds(string value) - { - _value = value ?? throw new ArgumentNullException(nameof(value)); - } - - /// - /// Default operation kind. - /// - public static OperationKinds Default { get; } = new OperationKinds(DefaultValue); - - /// - /// LongRunning operation kind. - /// - public static OperationKinds LongRunning { get; } = new OperationKinds(LongRunningValue); - - /// - /// Paging operation kind. - /// - public static OperationKinds Paging { get; } = new OperationKinds(PagingValue); - - /// Determines if two values are the same. - public static bool operator ==(OperationKinds left, OperationKinds right) => left.Equals(right); - /// Determines if two values are not the same. - public static bool operator !=(OperationKinds left, OperationKinds right) => !left.Equals(right); - - /// Converts a string to a . - /// The string value to convert. - public static implicit operator OperationKinds(string value) => new(value); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override bool Equals(object? obj) => obj is OperationKinds other && Equals(other); - - /// - public bool Equals(OperationKinds other) => string.Equals(_value, other._value, StringComparison.InvariantCultureIgnoreCase); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override int GetHashCode() => _value?.GetHashCode() ?? 0; - - /// - public override string ToString() => _value; - } -} diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/test/OperationKindsTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/test/OperationKindsTests.cs index 27aae102b6..727195c85a 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/test/OperationKindsTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.Input/test/OperationKindsTests.cs @@ -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); } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/TypeFactory.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/TypeFactory.cs index 6b7dc04da9..1996117011 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/TypeFactory.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/TypeFactory.cs @@ -25,14 +25,14 @@ public abstract class TypeFactory public abstract ParameterProvider CreateCSharpParam(InputParameter parameter); /// - /// Factory method for creating a based on an input operation . + /// Factory method for creating a based on an input operation . /// /// The to convert. /// The that will contain the methods. - /// An instance of containing the chain of methods + /// An instance of containing the chain of methods /// associated with the input operation, or null if no methods are constructed. /// - public abstract CSharpMethodCollection? CreateCSharpMethodCollection(InputOperation operation, TypeProvider enclosingType); + public abstract MethodProviderCollection? CreateMethodProviders(InputOperation operation, TypeProvider enclosingType); /// /// Factory method for retrieving the serialization format for a given input type. diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ClientProvider.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ClientProvider.cs index 41789fe513..6bbc1bc515 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ClientProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ClientProvider.cs @@ -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); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/CSharpMethodCollection.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/MethodProviderCollection.cs similarity index 72% rename from packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/CSharpMethodCollection.cs rename to packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/MethodProviderCollection.cs index ccd08328b7..1eb482caee 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/CSharpMethodCollection.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/MethodProviderCollection.cs @@ -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 { /// /// Represents an immutable collection of methods that are associated with an operation . /// - public sealed class CSharpMethodCollection : IReadOnlyList + public sealed class MethodProviderCollection : IReadOnlyList { private readonly IReadOnlyList _cSharpMethods; - private CSharpMethodCollection(IReadOnlyList methods) + private MethodProviderCollection(IEnumerable? methods) { - _cSharpMethods = methods ?? Array.Empty(); + _cSharpMethods = methods?.ToList() ?? []; } /// - /// Builds a default for the given + /// Builds a default for the given /// with a single method that creates a message. /// /// The to convert. /// The that will contain the methods. - 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() { 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] @@ -63,20 +62,6 @@ private static MethodProvider BuildCreateMessageMethod(InputOperation operation, return new MethodProvider(methodSignature, methodBody, enclosingType); } - /// - /// Returns all methods in the collection of a specific kind. - /// - internal List GetMethods() - { - var methods = new List(); - foreach (var method in _cSharpMethods) - { - methods.Add(method); - } - - return methods; - } - public IEnumerator GetEnumerator() { return _cSharpMethods.GetEnumerator(); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/CSharpMethodCollectionTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/CSharpMethodCollectionTests.cs index e4d12ee87d..a7eee0a2b8 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/CSharpMethodCollectionTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/CSharpMethodCollectionTests.cs @@ -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); diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Mocks/MockTypeFactory.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Mocks/MockTypeFactory.cs index ffca911560..208f8a6098 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Mocks/MockTypeFactory.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Mocks/MockTypeFactory.cs @@ -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(); } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/TypeFactoryTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/TypeFactoryTests.cs index e0e105254c..d5f225f5d9 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/TypeFactoryTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/TypeFactoryTests.cs @@ -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(); }