work towards path parameters
This commit is contained in:
parent
2e4570a560
commit
a24543063b
|
|
@ -107,27 +107,39 @@ public sealed class HttpServer {
|
||||||
Assert(mi.IsPublic, $"Method tagged with HttpEndpointAttribute must be public! ({GetFancyMethodName()})");
|
Assert(mi.IsPublic, $"Method tagged with HttpEndpointAttribute must be public! ({GetFancyMethodName()})");
|
||||||
|
|
||||||
var methodParams = mi.GetParameters();
|
var methodParams = mi.GetParameters();
|
||||||
|
// check the mandatory prefix parameters
|
||||||
Assert(methodParams.Length >= expectedEndpointParameterTypes.Length);
|
Assert(methodParams.Length >= expectedEndpointParameterTypes.Length);
|
||||||
for (int i = 0; i < expectedEndpointParameterTypes.Length; i++) {
|
for (int i = 0; i < expectedEndpointParameterTypes.Length; i++) {
|
||||||
Assert(methodParams[i].ParameterType.IsAssignableFrom(expectedEndpointParameterTypes[i]),
|
Assert(methodParams[i].ParameterType.IsAssignableFrom(expectedEndpointParameterTypes[i]),
|
||||||
$"Parameter at index {i} of {GetFancyMethodName()} is of a type that cannot contain the expected type {expectedEndpointParameterTypes[i].FullName}.");
|
$"Parameter at index {i} of {GetFancyMethodName()} is of a type that cannot contain the expected type {expectedEndpointParameterTypes[i].FullName}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check return type
|
||||||
Assert(mi.ReturnType == typeof(Task), $"Return type of {GetFancyMethodName()} is not {typeof(Task)}!");
|
Assert(mi.ReturnType == typeof(Task), $"Return type of {GetFancyMethodName()} is not {typeof(Task)}!");
|
||||||
|
|
||||||
|
// check the rest of the method parameters
|
||||||
var qparams = new List<QueryParameterInfo>();
|
var qparams = new List<QueryParameterInfo>();
|
||||||
for (int i = expectedEndpointParameterTypes.Length; i < methodParams.Length; i++) {
|
for (int i = expectedEndpointParameterTypes.Length; i < methodParams.Length; i++) {
|
||||||
var par = methodParams[i];
|
var par = methodParams[i];
|
||||||
var attr = par.GetCustomAttribute<ParameterAttribute>(false);
|
var attr = par.GetCustomAttribute<ParameterAttribute>(false);
|
||||||
qparams.Add(new(
|
var pathAttr = par.GetCustomAttribute<PathParameterAttribute>(false);
|
||||||
attr?.Name ?? par.Name ?? throw new ArgumentException($"C# variable name of parameter at index {i} of method {GetFancyMethodName()} is null!"),
|
|
||||||
par.ParameterType,
|
|
||||||
attr?.IsOptional ?? false)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!stringToTypeParameterConverters.ContainsKey(par.ParameterType)) {
|
if (attr != null && pathAttr != null) {
|
||||||
throw new MissingParameterConverterException($"Parameter converter for type {par.ParameterType} has not been registered (yet)!");
|
throw new ArgumentException($"A method argument cannot be tagged with both {nameof(ParameterAttribute)} and {nameof(PathParameterAttribute)}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pathAttr != null) { // parameter is a path param
|
||||||
|
|
||||||
|
} else { // parameter is a normal one
|
||||||
|
qparams.Add(new(
|
||||||
|
attr?.Name ?? par.Name ?? throw new ArgumentException($"C# variable name of parameter at index {i} of method {GetFancyMethodName()} is null!"),
|
||||||
|
par.ParameterType,
|
||||||
|
attr?.IsOptional ?? false)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!stringToTypeParameterConverters.ContainsKey(par.ParameterType)) {
|
||||||
|
throw new MissingParameterConverterException($"Parameter converter for type {par.ParameterType} has not been registered (yet)!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,6 @@
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = false)]
|
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = false)]
|
||||||
public sealed class ParameterAttribute : Attribute {
|
public sealed class ParameterAttribute : Attribute {
|
||||||
// See the attribute guidelines at
|
|
||||||
// http://go.microsoft.com/fwlink/?LinkId=85236
|
|
||||||
|
|
||||||
public string Name { get; }
|
public string Name { get; }
|
||||||
public bool IsOptional { get; }
|
public bool IsOptional { get; }
|
||||||
public ParameterAttribute(string name, bool isOptional = false) {
|
public ParameterAttribute(string name, bool isOptional = false) {
|
||||||
|
|
|
||||||
24
SimpleHttpServer/Types/PathParameterAttribute.cs
Normal file
24
SimpleHttpServer/Types/PathParameterAttribute.cs
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
namespace SimpleHttpServer.Types;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies the name of a http endpoint path parameter. Path parameter names must be in the format $1, $2, $3, ..., and the end of the path may be $*
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = false)]
|
||||||
|
public sealed class PathParameterAttribute : Attribute {
|
||||||
|
public string Name { get; }
|
||||||
|
public PathParameterAttribute(string name) {
|
||||||
|
if (string.IsNullOrWhiteSpace(name)) {
|
||||||
|
throw new ArgumentException($"'{nameof(name)}' cannot be null or whitespace.", nameof(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!name.StartsWith('$')) {
|
||||||
|
throw new ArgumentException($"'{nameof(name)}' must start with $.", nameof(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name.Contains(' ')) {
|
||||||
|
throw new ArgumentException($"'{nameof(name)}' must not contain spaces.", nameof(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user