-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ability to use array types as query string values in Wolverine.HTTP. C…
…loses GH-1137
- Loading branch information
1 parent
374293d
commit 9721c36
Showing
5 changed files
with
197 additions
and
23 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
56 changes: 56 additions & 0 deletions
56
src/Http/Wolverine.Http/CodeGen/ParsedArrayQueryStringValue.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,56 @@ | ||
using System.Reflection; | ||
using JasperFx.CodeGeneration; | ||
using JasperFx.CodeGeneration.Frames; | ||
using JasperFx.Core.Reflection; | ||
|
||
namespace Wolverine.Http.CodeGen; | ||
|
||
internal class ParsedArrayQueryStringValue : SyncFrame | ||
{ | ||
public ParsedArrayQueryStringValue(ParameterInfo parameter) | ||
{ | ||
Variable = new QuerystringVariable(parameter.ParameterType, parameter.Name!, this); | ||
} | ||
|
||
public QuerystringVariable Variable { get; } | ||
|
||
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) | ||
{ | ||
var elementType = Variable.VariableType.GetElementType(); | ||
if (elementType == typeof(string)) | ||
{ | ||
writer.Write($"var {Variable.Usage} = httpContext.Request.Query[\"{Variable.Usage}\"].ToArray();"); | ||
} | ||
else | ||
{ | ||
var collectionAlias = typeof(List<>).MakeGenericType(elementType).FullNameInCode(); | ||
var elementAlias = elementType.FullNameInCode(); | ||
|
||
writer.Write($"var {Variable.Usage}_List = new {collectionAlias}();"); | ||
|
||
writer.Write($"BLOCK:foreach (var {Variable.Usage}Value in httpContext.Request.Query[\"{Variable.Usage}\"])"); | ||
|
||
if (elementType.IsEnum) | ||
{ | ||
writer.Write($"BLOCK:if ({elementAlias}.TryParse<{elementAlias}>({Variable.Usage}Value, out var {Variable.Usage}ValueParsed))"); | ||
} | ||
else if (elementType.IsBoolean()) | ||
{ | ||
writer.Write($"BLOCK:if ({elementAlias}.TryParse({Variable.Usage}Value, out var {Variable.Usage}ValueParsed))"); | ||
} | ||
else | ||
{ | ||
writer.Write($"BLOCK:if ({elementAlias}.TryParse({Variable.Usage}Value, System.Globalization.CultureInfo.InvariantCulture, out var {Variable.Usage}ValueParsed))"); | ||
} | ||
|
||
writer.Write($"{Variable.Usage}_List.Add({Variable.Usage}ValueParsed);"); | ||
writer.FinishBlock(); // parsing block | ||
|
||
writer.FinishBlock(); // foreach blobck | ||
|
||
writer.Write($"var {Variable.Usage} = {Variable.Usage}_List.ToArray();"); | ||
} | ||
|
||
Next?.GenerateCode(method, writer); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using JasperFx.Core; | ||
using Marten; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Wolverine.Http; | ||
|
||
namespace WolverineWebApi; | ||
|
||
public static class QuerystringEndpoints | ||
{ | ||
|
||
[WolverineGet("/querystring/enum")] | ||
public static string UsingEnumQuerystring(Direction direction) | ||
{ | ||
return direction.ToString(); | ||
} | ||
|
||
[WolverineGet("/querystring/explicit")] | ||
public static string UsingEnumQuerystring([FromQuery(Name = "name")]string value) | ||
{ | ||
return value ?? ""; | ||
} | ||
|
||
[WolverineGet("/querystring/enum/nullable")] | ||
public static string UsingNullableEnumQuerystring(Direction? direction) | ||
{ | ||
return direction?.ToString() ?? "none"; | ||
} | ||
|
||
[WolverineGet("/querystring/stringarray")] | ||
public static string StringArray(string[]? values) | ||
{ | ||
if (values == null || values.IsEmpty()) return "none"; | ||
|
||
return values.Join(","); | ||
} | ||
|
||
[WolverineGet("/querystring/intarray")] | ||
public static string IntArray(int[]? values) | ||
{ | ||
if (values == null || values.IsEmpty()) return "none"; | ||
|
||
return values.OrderBy(x => x).Select(x => x.ToString()).Join(","); | ||
} | ||
} |
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