/
RouteHandling.cs
134 lines (115 loc) · 4.15 KB
/
RouteHandling.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System.Reflection;
using JasperFx.CodeGeneration;
using JasperFx.CodeGeneration.Frames;
using JasperFx.CodeGeneration.Model;
using JasperFx.Core.Reflection;
using Lamar;
using Microsoft.AspNetCore.Http;
namespace Wolverine.Http.CodeGen;
internal class ReadStringRouteValue : SyncFrame
{
public ReadStringRouteValue(string name)
{
Variable = new Variable(typeof(string), name, this);
}
public Variable Variable { get; }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
{
writer.Write($"var {Variable.Usage} = (string)httpContext.GetRouteValue(\"{Variable.Usage}\");");
Next?.GenerateCode(method, writer);
}
}
internal class ParsedRouteArgumentFrame : SyncFrame
{
public ParsedRouteArgumentFrame(ParameterInfo parameter)
{
Variable = new Variable(parameter.ParameterType, parameter.Name!, this);
}
public ParsedRouteArgumentFrame(Type variableType, string parameterName)
{
Variable = new Variable(variableType, parameterName, this);
}
public Variable Variable { get; }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
{
var alias = Variable.VariableType.FullNameInCode();
if (Variable.VariableType.IsEnum)
{
writer.Write(
$"BLOCK:if (!{alias}.TryParse<{alias}>((string)httpContext.GetRouteValue(\"{Variable.Usage}\"), true, out {alias} {Variable.Usage}))");
}
else
{
writer.Write(
$"BLOCK:if (!{alias}.TryParse((string)httpContext.GetRouteValue(\"{Variable.Usage}\"), out var {Variable.Usage}))");
}
writer.WriteLine(
$"httpContext.Response.{nameof(HttpResponse.StatusCode)} = 404;");
writer.WriteLine(method.ToExitStatement());
writer.FinishBlock();
writer.BlankLine();
Next?.GenerateCode(method, writer);
}
}
internal class RouteParameterStrategy : IParameterStrategy
{
#region sample_supported_route_parameter_types
public static readonly Dictionary<Type, string> TypeOutputs = new()
{
{ typeof(bool), "bool" },
{ typeof(byte), "byte" },
{ typeof(sbyte), "sbyte" },
{ typeof(char), "char" },
{ typeof(decimal), "decimal" },
{ typeof(float), "float" },
{ typeof(short), "short" },
{ typeof(int), "int" },
{ typeof(double), "double" },
{ typeof(long), "long" },
{ typeof(ushort), "ushort" },
{ typeof(uint), "uint" },
{ typeof(ulong), "ulong" },
{ typeof(Guid), typeof(Guid).FullName! },
{ typeof(DateTime), typeof(DateTime).FullName! },
{ typeof(DateTimeOffset), typeof(DateTimeOffset).FullName! }
};
#endregion
public static readonly Dictionary<Type, string> TypeRouteConstraints = new()
{
{ typeof(bool), "bool" },
{ typeof(string), "alpha" },
{ typeof(decimal), "decimal" },
{ typeof(float), "float" },
{ typeof(int), "int" },
{ typeof(double), "double" },
{ typeof(long), "long" },
{ typeof(Guid), "guid" },
{ typeof(DateTime), "datetime" }
};
public bool TryMatch(HttpChain chain, IContainer container, ParameterInfo parameter, out Variable variable)
{
return chain.FindRouteVariable(parameter, out variable);
}
public static bool CanParse(Type argType)
{
return TypeOutputs.ContainsKey(argType) || argType.IsEnum;
}
public static void TryApplyRouteVariables(HttpChain chain, MethodCall call)
{
for (int i = 0; i < call.Arguments.Length; i++)
{
// Don't override them at all of course
if (call.Arguments[i] == null)
{
var parameter = call.Method.GetParameters()[i];
if (parameter.ParameterType == typeof(string) || CanParse(parameter.ParameterType))
{
if (chain.FindRouteVariable(parameter.ParameterType, parameter.Name, out var variable))
{
call.Arguments[i] = variable;
}
}
}
}
}
}