You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
HttpMethod provides a constructor (string method = ...; ... = new HttpMethod(method);), but the 99.999% case is that one of the static properties on HttpMethod suffices, and folks end up writing their own mapping tables to use those singletons. We can just provide that mapping once.
API Proposal
namespace System.Net.Http;
public class HttpMethod
{
// Case insensitive - to match our logic in Networking stack
+ public static HttpMethod From(ReadOnlySpan<char> method);
// Alternative names: Parse / Get / Create
// public static HttpMethod From(string method); ... OPTIONAL, Networking team thinks it is NOT necessary
/*
method switch
{
"GET" => s_getMethod,
"PUT" => s_putMethod,
"POST" => s_postMethod,
"DELETE" => s_deleteMethod,
"HEAD" => s_headMethod,
"OPTIONS" => s_optionsMethod,
"TRACE" => s_traceMethod,
"PATCH" => s_patchMethod,
"CONNECT" => s_connectMethod,
_ => new HttpMethod(method);
};
*/
}
Since spans can now be matched against strings, From(ReadOnlySpan<char> method) overload might make sense too.
Alternatively, as @KeterSCP suggested, it might as well be better to implement ISpanParsable<HttpMethod> instead.
We think that the method should be Parse instead of From.
It's probably reasonable for the method to do case normalization and maybe trimming ("get " -> "GET")
It's mildly unfortunate for the 0.01% case when it hits the unknown case that it could be string -> ROS-char -> new allocated string, but since it's expected to be 0.01%, it's fine.
Maybe it should implement IParsable/ISpanParsable as well if it has a Parse method? That just seems like general goodness so that serialization frameworks would automatically be able to deserialize this type.
Activity
ShreyasJejurkar commentedon Jul 19, 2023
I can take this one for implementation once it's approved! 🙌
KeterSCP commentedon Jul 19, 2023
Have you thought about naming it
Parse?Yeah, I know that there is not much parsing involved, but still it seems to be more consistent with other APIs
neon-sunset commentedon Jul 19, 2023
Since spans can now be matched against strings,
From(ReadOnlySpan<char> method)overload might make sense too.Alternatively, as @KeterSCP suggested, it might as well be better to implement
ISpanParsable<HttpMethod>instead.karelz commentedon Jul 20, 2023
Triage: Makes sense, top post updated with discussion results.
bartonjs commentedon Jul 20, 2023
Video
Parseinstead ofFrom.MihaZupan commentedon Jul 20, 2023
If you give us garbage we could just let the constructor throw and have the caller fix their substring/slice 🤷♂️ Don't care too much about it though.
Neme12 commentedon Jul 20, 2023
Maybe it should implement
IParsable/ISpanParsableas well if it has aParsemethod? That just seems like general goodness so that serialization frameworks would automatically be able to deserialize this type.