Advertisement
Guest User

Untitled

a guest
Feb 28th, 2023
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | Software | 0 0
  1. @moxy
  2. @attribute required string RouteTemplate
  3. @moxy
  4.  
  5. {{
  6. $routeParameterTypesAndNames = [];
  7. $urlSegmentsForRouteAttribute = [];
  8. $getRouteMethodFormatParts = [];
  9. $getRouteMethodParameterSources = [];
  10.  
  11. $segments = RouteTemplate | regex.split("/");
  12.  
  13. for $segment in $segments
  14. $match = $segment | regex.match `\{(\w+)\s*:?(\w+)\}?`;
  15. if !$match[0]
  16. $urlSegmentsForRouteAttribute = $urlSegmentsForRouteAttribute | array.add($segment)
  17. $getRouteMethodFormatParts = $getRouteMethodFormatParts | array.add($segment)
  18. else
  19. $paramName = $match[1]
  20. $paramType = $match[2]
  21.  
  22. $getRouteMethodFormatParts = $getRouteMethodFormatParts | array.add("{" + $paramName + "}")
  23. $getRouteMethodParameterSources = $getRouteMethodParameterSources | array.add($paramType + " " + $paramName);
  24.  
  25. $routeParameterTypesAndNames = $routeParameterTypesAndNames | array.add({ Name: $paramName, Type: $paramType})
  26. if $match[2] != "string"
  27. $urlSegmentsForRouteAttribute = $urlSegmentsForRouteAttribute | array.add($segment)
  28. else
  29. $urlSegmentsForRouteAttribute = $urlSegmentsForRouteAttribute | array.add('{' + $match[1] + '}')
  30. end
  31. end
  32. end
  33.  
  34.  
  35. capture $parametersSource
  36. for $currentParameter in $routeParameterTypesAndNames
  37. "[Parameter] public " + $currentParameter.Type + " " + $currentParameter.Name + " { get; set; }\r\n"
  38. end
  39. end
  40.  
  41. $routeAttributeString = $urlSegmentsForRouteAttribute | array.join("/");
  42. $getRouteSignatureSource = $getRouteMethodParameterSources | array.join(", ")
  43. $getRouteFormat = $getRouteMethodFormatParts | array.join("/")
  44. }}
  45.  
  46. namespace {{ moxy.Class.Namespace }}
  47. {
  48. using Microsoft.AspNetCore.Components;
  49. [Route("{{ $routeAttributeString }}")]
  50. partial class {{ moxy.Class.Name }}
  51. {
  52. public static string GetPageUrl({{ $getRouteSignatureSource }}) => $"{{$getRouteFormat}}";
  53. {{ $parametersSource }}
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement