private ConvenienceMethod? BuildConvenienceMethod()

in src/AutoRest.CSharp/LowLevel/Output/OperationMethodChainBuilder.cs [342:403]


        private ConvenienceMethod? BuildConvenienceMethod(bool shouldRequestContextOptional, ConvenienceMethodGenerationInfo generationInfo)
        {
            if (!generationInfo.IsConvenienceMethodGenerated)
                return null;

            bool needNameChange = shouldRequestContextOptional && HasAmbiguityBetweenProtocolAndConvenience();
            string name = _restClientMethod.Name;
            if (needNameChange)
            {
                name = _restClientMethod.Name.IsLastWordSingular() ? $"{_restClientMethod.Name}Value" : $"{_restClientMethod.Name.LastWordToSingular()}Values";
            }
            var attributes = Operation.Deprecated is { } deprecated
                ? new[] { new CSharpAttribute(typeof(ObsoleteAttribute), Literal(deprecated)) }
                : null;

            var protocolToConvenience = new List<ProtocolToConvenienceParameterConverter>();
            var parameterList = new List<Parameter>();
            foreach (var parameterChain in _orderedParameters)
            {
                var protocolParameter = parameterChain.Protocol;
                var convenienceParameter = parameterChain.Convenience;
                if (convenienceParameter != null)
                {
                    if (parameterChain.IsSpreadParameter)
                    {
                        if (convenienceParameter.Type is { IsFrameworkType: false, Implementation: ModelTypeProvider model })
                        {
                            var parameters = BuildSpreadParameters(model).OrderBy(p => p.DefaultValue == null ? 0 : 1).ToArray();

                            parameterList.AddRange(parameters);
                            protocolToConvenience.Add(new ProtocolToConvenienceParameterConverter(protocolParameter!, convenienceParameter, new ConvenienceParameterSpread(model, parameters)));
                        }
                        else
                        {
                            throw new InvalidOperationException($"The parameter {convenienceParameter.Name} in method {_client?.Declaration.FullName}.{name} is marked as Spread but its type is not ModelTypeProvider (got {convenienceParameter.Type})");
                        }
                    }
                    else
                    {
                        parameterList.Add(convenienceParameter);
                        if (protocolParameter != null)
                            protocolToConvenience.Add(new ProtocolToConvenienceParameterConverter(protocolParameter, convenienceParameter, null));
                    }
                }
                else if (protocolParameter != null)
                {
                    // convenience parameter is null - such as CancellationToken in non-azure library
                    protocolToConvenience.Add(new ProtocolToConvenienceParameterConverter(protocolParameter, convenienceParameter, null));
                }
            }
            var accessibility = _restClientMethod.Accessibility | Virtual;
            if (generationInfo.IsConvenienceMethodInternal)
            {
                accessibility &= ~Public; // removes public if any
                accessibility |= Internal; // add internal
            }
            var convenienceSignature = new MethodSignature(name, FormattableStringHelpers.FromString(_restClientMethod.Summary), FormattableStringHelpers.FromString(_restClientMethod.Description), accessibility, _returnType.Convenience, null, parameterList, attributes);
            var diagnostic = Configuration.IsBranded
                ? name != _restClientMethod.Name ? new Diagnostic($"{_clientName}.{convenienceSignature.Name}") : null
                : null;
            return new ConvenienceMethod(convenienceSignature, protocolToConvenience, _returnType.ConvenienceResponseType, Operation.RequestMediaTypes, GetReturnedResponseContentType(), diagnostic, _protocolMethodPaging is not null, Operation.LongRunning is not null, Operation.Deprecated);
        }