-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement array as header and query, and format different type to str…
…ing (#4213) Fix #4205 Fixes #3973 Problem to solve: When any type except string (e.g. int, TimeSpan and so on) as header/query, it is not correct covert to correct format, and when there is an array as header/query, it does not serialize correct in connection format (such as cvs, pipe and so on). - call TypeFormatters.ConvertToString to covert this type to correct format - when it is an array, we need to convert to string in correct collection format.
- Loading branch information
Showing
33 changed files
with
2,843 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
....Generator.CSharp.ClientModel/src/Providers/PipelineRequestHeadersExtensionsDefinition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.ClientModel.Primitives; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.Generator.CSharp.ClientModel.Snippets; | ||
using Microsoft.Generator.CSharp.Expressions; | ||
using Microsoft.Generator.CSharp.Primitives; | ||
using Microsoft.Generator.CSharp.Providers; | ||
using Microsoft.Generator.CSharp.Snippets; | ||
using static Microsoft.Generator.CSharp.Snippets.Snippet; | ||
|
||
namespace Microsoft.Generator.CSharp.ClientModel.Providers | ||
{ | ||
internal class PipelineRequestHeadersExtensionsDefinition : TypeProvider | ||
{ | ||
private const string _setDelimited = "SetDelimited"; | ||
private ParameterProvider _pipelineRequestHeadersParam; | ||
public PipelineRequestHeadersExtensionsDefinition() | ||
{ | ||
_pipelineRequestHeadersParam = new ParameterProvider("headers", FormattableStringHelpers.Empty, typeof(PipelineRequestHeaders)); | ||
} | ||
private readonly CSharpType _t = typeof(IEnumerable<>).GetGenericArguments()[0]; | ||
protected override TypeSignatureModifiers GetDeclarationModifiers() | ||
{ | ||
return TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static; | ||
} | ||
|
||
protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "Internal", $"{Name}.cs"); | ||
|
||
protected override string BuildName() => "PipelineRequestHeadersExtensions"; | ||
|
||
protected override MethodProvider[] BuildMethods() | ||
{ | ||
return | ||
[ | ||
BuildSetDelimited(false), | ||
BuildSetDelimited(true), | ||
]; | ||
} | ||
|
||
private MethodProvider BuildSetDelimited(bool hasFormat) | ||
{ | ||
var nameParameter = new ParameterProvider("name", $"The name.", typeof(string)); | ||
var valueParameter = new ParameterProvider("value", $"The value.", new CSharpType(typeof(IEnumerable<>), _t)); | ||
var delimiterParameter = new ParameterProvider("delimiter", $"The delimiter.", typeof(string)); | ||
var formatParameter = new ParameterProvider("format", $"The format.", typeof(string)); | ||
var modifiers = MethodSignatureModifiers.Public | MethodSignatureModifiers.Static | MethodSignatureModifiers.Extension; | ||
var parameters = hasFormat | ||
? new[] { _pipelineRequestHeadersParam, nameParameter, valueParameter, delimiterParameter, formatParameter } | ||
: new[] { _pipelineRequestHeadersParam, nameParameter, valueParameter, delimiterParameter }; | ||
MethodSignature signature = new MethodSignature( | ||
Name: _setDelimited, | ||
Modifiers: modifiers, | ||
Parameters: parameters, | ||
ReturnType: null, | ||
GenericArguments: [_t], | ||
Description: null, | ||
ReturnDescription: null); | ||
|
||
var value = valueParameter.As(_t); | ||
var v = new VariableExpression(_t, "v"); | ||
var convertToStringExpression = TypeFormattersSnippets.ConvertToString(v, hasFormat ? formatParameter : (ValueExpression?)null); | ||
var selector = new FuncExpression([v.Declaration], convertToStringExpression).As<string>(); | ||
var body = new[] | ||
{ | ||
Declare("stringValues", value.Select(selector), out var stringValues), | ||
new InvokeMethodExpression(_pipelineRequestHeadersParam, "Set", [nameParameter, StringSnippets.Join(delimiterParameter, stringValues)]).Terminate() | ||
}; | ||
|
||
return new(signature, body, this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.