Skip to content

Commit

Permalink
Merge branch 'main' into MissingDecorators
Browse files Browse the repository at this point in the history
  • Loading branch information
skywing918 authored Dec 12, 2024
2 parents c10252d + 339dd17 commit 3bc31c3
Show file tree
Hide file tree
Showing 258 changed files with 1,506 additions and 984 deletions.
2 changes: 1 addition & 1 deletion eng/emitters/scripts/Generate-APIView-CodeFile.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
param (
[Parameter(mandatory = $true)]
$ArtifactPath,
$NpmDevopsFeedRegistry = "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-js/npm/registry/"
$NpmDevopsFeedRegistry = "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-js@Local/npm/registry/"
)

Set-StrictMode -Version 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ foreach ($directory in $directories) {
$outputDir = $directory.FullName.Substring(0, $directory.FullName.IndexOf("src") - 1)
$subPath = $outputDir.Substring($cadlRanchRoot.Length + 1)

if ($subPath.Contains($(Join-Path 'srv-driven' 'v1'))) {
continue
}

Write-Host "Regenerating $subPath" -ForegroundColor Cyan

$specFile = Join-Path $specsDirectory $subPath "client.tsp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private ScmMethodProvider BuildConvenienceMethod(MethodProvider protocolMethod,
methodModifier,
GetResponseType(Operation.Responses, true, isAsync, out var responseBodyType),
null,
isAsync ? [.. ConvenienceMethodParameters, ScmKnownParameters.CancellationToken] : ConvenienceMethodParameters);
[.. ConvenienceMethodParameters, ScmKnownParameters.CancellationToken]);

MethodBodyStatement[] methodBody;

Expand All @@ -84,15 +84,15 @@ private ScmMethodProvider BuildConvenienceMethod(MethodProvider protocolMethod,
methodBody =
[
.. GetStackVariablesForProtocolParamConversion(ConvenienceMethodParameters, out var declarations),
Return(This.Invoke(protocolMethod.Signature, [.. GetProtocolMethodArguments(ConvenienceMethodParameters, declarations, isAsync)], isAsync))
Return(This.Invoke(protocolMethod.Signature, [.. GetProtocolMethodArguments(ConvenienceMethodParameters, declarations)], isAsync))
];
}
else
{
methodBody =
[
.. GetStackVariablesForProtocolParamConversion(ConvenienceMethodParameters, out var paramDeclarations),
Declare("result", This.Invoke(protocolMethod.Signature, [.. GetProtocolMethodArguments(ConvenienceMethodParameters, paramDeclarations, isAsync)], isAsync).ToApi<ClientResponseApi>(), out ClientResponseApi result),
Declare("result", This.Invoke(protocolMethod.Signature, [.. GetProtocolMethodArguments(ConvenienceMethodParameters, paramDeclarations)], isAsync).ToApi<ClientResponseApi>(), out ClientResponseApi result),
.. GetStackVariablesForReturnValueConversion(result, responseBodyType, isAsync, out var resultDeclarations),
Return(result.FromValue(GetResultConversion(result, result.GetRawResponse(), responseBodyType, resultDeclarations), result.GetRawResponse())),
];
Expand Down Expand Up @@ -314,8 +314,7 @@ private ValueExpression GetResultConversion(ClientResponseApi result, HttpRespon

private IReadOnlyList<ValueExpression> GetProtocolMethodArguments(
IReadOnlyList<ParameterProvider> convenienceMethodParameters,
Dictionary<string, ValueExpression> declarations,
bool isAsync)
Dictionary<string, ValueExpression> declarations)
{
List<ValueExpression> conversions = new List<ValueExpression>();
bool addedSpreadSource = false;
Expand Down Expand Up @@ -362,12 +361,10 @@ private IReadOnlyList<ValueExpression> GetProtocolMethodArguments(
conversions.Add(param);
}
}

// RequestOptions argument
conversions.Add(IHttpRequestOptionsApiSnippets.FromCancellationToken(ScmKnownParameters.CancellationToken));

conversions.Add(
isAsync
? IHttpRequestOptionsApiSnippets.FromCancellationToken(ScmKnownParameters.CancellationToken)
: ScmKnownParameters.RequestOptions.PositionalReference(Null));
return conversions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public async Task CanAddMethodSameName()
var helloAgainMethod = clientProviderMethods.FirstOrDefault(m
=> m.Signature.Name == "HelloAgain" && m.Signature.Parameters.Count > 0 && m.Signature.Parameters[0].Name == "p1");
Assert.IsNotNull(helloAgainMethod);
Assert.AreEqual(1, helloAgainMethod!.Signature.Parameters.Count);
Assert.AreEqual(2, helloAgainMethod!.Signature.Parameters.Count);

// The custom code view should contain the method
var customCodeView = clientProvider.CustomCodeView;
Expand Down Expand Up @@ -187,7 +187,7 @@ public async Task CanReplaceStructMethod(bool isStructCustomized)
Assert.AreEqual("HelloAgain", customMethods[0].Signature.Name);

var customMethodParams = customMethods[0].Signature.Parameters;
Assert.AreEqual(1, customMethodParams.Count);
Assert.AreEqual(2, customMethodParams.Count);
Assert.AreEqual("p1", customMethodParams[0].Name);
Assert.AreEqual("MyStruct", customMethodParams[0].Type.Name);
Assert.AreEqual(isStructCustomized ? "Sample.TestClient" : string.Empty, customMethodParams[0].Type.Namespace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public void ValidateClientWithSpread(InputClient inputClient)

var convenienceMethods = methods.Where(m => m.Signature.Parameters.Any(p => p.Type.Equals(typeof(string)))).ToList();
Assert.AreEqual(2, convenienceMethods.Count);
Assert.AreEqual(1, convenienceMethods[0].Signature.Parameters.Count);
Assert.AreEqual(2, convenienceMethods[0].Signature.Parameters.Count);

Assert.AreEqual(new CSharpType(typeof(string)), convenienceMethods[0].Signature.Parameters[0].Type);
Assert.AreEqual("p1", convenienceMethods[0].Signature.Parameters[0].Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Sample
/// <summary></summary>
public partial class TestClient
{
public virtual ClientResult HelloAgain(MyStruct? p1)
public virtual ClientResult HelloAgain(MyStruct? p1, CancellationToken cancellationToken = default)
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Sample
/// <summary></summary>
public partial class TestClient
{
public virtual ClientResult HelloAgain(MyStruct? p1)
public virtual ClientResult HelloAgain(MyStruct? p1, CancellationToken cancellationToken = default)
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
#nullable disable

using System.ClientModel;
using System.ClientModel.Primitives;
using System.Threading;
using Sample.Models;

namespace Sample
{
/// <summary></summary>
public partial class TestClient
{
public virtual global::System.ClientModel.ClientResult Operation(global::Sample.Models.InputEnum queryParam)
public virtual global::System.ClientModel.ClientResult Operation(global::Sample.Models.InputEnum queryParam, global::System.Threading.CancellationToken cancellationToken = default)
{
return this.Operation(queryParam.ToString(), options: null);
return this.Operation(queryParam.ToString(), cancellationToken.CanBeCanceled ? new global::System.ClientModel.Primitives.RequestOptions { CancellationToken = cancellationToken } : null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ public void TestDefaultCSharpMethodCollection(InputOperation inputOperation)
if (spreadInputParameter != null)
{
var spreadModelProperties = _spreadModel.Properties;
Assert.AreEqual(spreadModelProperties.Count + 1, convenienceMethodParams.Count);
// model properties + 2 (parameter and cancellation token)
Assert.AreEqual(spreadModelProperties.Count + 2, convenienceMethodParams.Count);
Assert.AreEqual("p1", convenienceMethodParams[0].Name);
Assert.AreEqual(spreadModelProperties[0].Name, convenienceMethodParams[1].Name);
}
}

[TestCaseSource(nameof(DefaultCSharpMethodCollectionTestCases))]
public void AsyncMethodsHaveOptionalCancellationToken(InputOperation inputOperation)
public void ConvenienceMethodsHaveOptionalCancellationToken(InputOperation inputOperation)
{
var inputClient = InputFactory.Client("TestClient", operations: [inputOperation]);

Expand All @@ -87,6 +88,19 @@ public void AsyncMethodsHaveOptionalCancellationToken(InputOperation inputOperat
Assert.IsTrue(lastParameter.Type.Equals(typeof(CancellationToken)));
Assert.IsFalse(lastParameter.Type.IsNullable);
Assert.AreEqual(Snippet.Default, lastParameter.DefaultValue);

var syncConvenienceMethod = methodCollection.FirstOrDefault(m
=> !m.Signature.Parameters.Any(p => p.Name == "content")
&& m.Signature.Name == inputOperation.Name.ToCleanName());
Assert.IsNotNull(syncConvenienceMethod);

var syncConvenienceMethodParameters = syncConvenienceMethod!.Signature.Parameters;
Assert.IsNotNull(syncConvenienceMethodParameters);

lastParameter = syncConvenienceMethodParameters.Last();
Assert.IsTrue(lastParameter.Type.Equals(typeof(CancellationToken)));
Assert.IsFalse(lastParameter.Type.IsNullable);
Assert.AreEqual(Snippet.Default, lastParameter.DefaultValue);
}

public static IEnumerable<TestCaseData> DefaultCSharpMethodCollectionTestCases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal override void Write(CodeWriter writer)
{
writer.AppendRaw("$\"");
var argumentCount = 0;
foreach ((var span, bool isLiteral) in StringExtensions.GetPathParts(Format))
foreach ((var span, bool isLiteral) in StringExtensions.GetFormattableStringFormatParts(Format))
{
if (isLiteral)
{
Expand All @@ -51,7 +51,7 @@ internal override void Write(CodeWriter writer)
private static void Validate(string format, IReadOnlyList<ValueExpression> args)
{
var count = 0;
foreach (var (_, isLiteral) in StringExtensions.GetPathParts(format))
foreach (var (_, isLiteral) in StringExtensions.GetFormattableStringFormatParts(format))
{
if (!isLiteral)
count++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,18 @@ public static string ToCleanName(this string name, bool isCamelCase = true)
[return: NotNullIfNotNull(nameof(name))]
public static string ToVariableName(this string name) => ToCleanName(name, isCamelCase: false);

public static GetPathPartsEnumerator GetPathParts(string? path) => new GetPathPartsEnumerator(path);
public static GetPathPartsEnumerator GetFormattableStringFormatParts(string? format) => new GetPathPartsEnumerator(format);

public static GetPathPartsEnumerator GetFormattableStringFormatParts(ReadOnlySpan<char> format) => new GetPathPartsEnumerator(format);

public ref struct GetPathPartsEnumerator
{
private ReadOnlySpan<char> _path;
public Part Current { get; private set; }

public GetPathPartsEnumerator(ReadOnlySpan<char> path)
public GetPathPartsEnumerator(ReadOnlySpan<char> format)
{
_path = path;
_path = format;
Current = default;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,29 @@ public XmlDocStatement(string startTag, string endTag, IEnumerable<FormattableSt
{
StartTag = startTag;
EndTag = endTag;
_lines = EscapeLines(lines);
_lines = NormalizeLines(lines);
InnerStatements = innerStatements;
}

private List<FormattableString> EscapeLines(IEnumerable<FormattableString> lines)
private List<FormattableString> NormalizeLines(IEnumerable<FormattableString> lines)
{
List<FormattableString> escapedLines = new List<FormattableString>();
List<FormattableString> result = new List<FormattableString>();

// break lines if they have line breaks
foreach (var line in lines)
{
escapedLines.Add(FormattableStringFactory.Create(EscapeLine(line.Format), EscapeArguments(line.GetArguments())));
var breakLines = FormattableStringHelpers.BreakLines(line);
result.AddRange(breakLines);
}
return escapedLines;

// escape lines if they have invalid characters
for (int i = 0; i < result.Count; i++)
{
var line = result[i];
result[i] = FormattableStringFactory.Create(EscapeLine(line.Format), EscapeArguments(line.GetArguments()));
}

return result;
}

private static object?[] EscapeArguments(object?[] objects)
Expand Down
Loading

0 comments on commit 3bc31c3

Please sign in to comment.