From ff54940b4ad3fa437c0ceaef1aa8ec4d8f739704 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Tue, 23 Jan 2024 12:35:31 +0100 Subject: [PATCH] fix: assorted fixes --- .../java/foo/zaaarf/routecompass/Route.java | 27 ++++++-------- .../foo/zaaarf/routecompass/RouteCompass.java | 35 +++++++++++-------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/main/java/foo/zaaarf/routecompass/Route.java b/src/main/java/foo/zaaarf/routecompass/Route.java index a890038..b35bd31 100644 --- a/src/main/java/foo/zaaarf/routecompass/Route.java +++ b/src/main/java/foo/zaaarf/routecompass/Route.java @@ -2,19 +2,21 @@ package foo.zaaarf.routecompass; import org.springframework.web.bind.annotation.RequestMethod; +import java.util.Arrays; + /** * Representation of a REST route. */ public class Route { /** - * The path of the endpoint. + * The paths of the endpoint. */ - public final String path; + public final String[] paths; /** - * The supported {@link RequestMethod}s, flattened to a string. + * The supported {@link RequestMethod}s, as strings. */ - public final String method; + public final String[] methods; /** * The media types produced by the endpoint. @@ -50,7 +52,7 @@ public class Route { /** * The one and only constructor. - * @param path the path of the endpoint + * @param paths the paths of the endpoint * @param methods the {@link RequestMethod}s accepted by the endpoint * @param consumes the media types consumed by the endpoint, may be null * @param produces the media types produced by the endpoint, may be null @@ -59,19 +61,10 @@ public class Route { * @param inputType the DTO for the request type, may be null * @param params {@link Param}s of the endpoint, may be null */ - public Route(String path, RequestMethod[] methods, String[] consumes, String[] produces, + public Route(String[] paths, RequestMethod[] methods, String[] consumes, String[] produces, boolean deprecated, DTO returnType, DTO inputType, Param... params) { - this.path = path; - - StringBuilder methodStringBuilder = new StringBuilder("["); - for(RequestMethod m : methods) - methodStringBuilder - .append(m.name()) - .append("|"); - methodStringBuilder - .deleteCharAt(methodStringBuilder.length() - 1) - .append("]"); - this.method = methodStringBuilder.toString(); + this.paths = paths; + this.methods = Arrays.stream(methods).map(Enum::name).toArray(String[]::new); if(produces != null) this.produces = produces; else this.produces = new String[0]; diff --git a/src/main/java/foo/zaaarf/routecompass/RouteCompass.java b/src/main/java/foo/zaaarf/routecompass/RouteCompass.java index 7c34739..30cd3c5 100644 --- a/src/main/java/foo/zaaarf/routecompass/RouteCompass.java +++ b/src/main/java/foo/zaaarf/routecompass/RouteCompass.java @@ -87,7 +87,7 @@ public class RouteCompass extends AbstractProcessor { try { FileObject serviceProvider = this.processingEnv.getFiler().createResource( - StandardLocation.SOURCE_OUTPUT, "", "routes" + StandardLocation.SOURCE_OUTPUT, "", "route_map" ); PrintWriter out = new PrintWriter(serviceProvider.openWriter()); @@ -98,11 +98,12 @@ public class RouteCompass extends AbstractProcessor { for(Route r : routesInClass) { out.print("\t- "); if(r.deprecated) out.print("[DEPRECATED] "); - out.print(r.method + " " + r.path); + out.print("[" + String.join("|", r.methods) + "] [" + + String.join("|", r.paths) + "]"); if(r.consumes != null && r.consumes.length > 0) - out.print("(expects: " + Arrays.toString(r.consumes) + ")"); + out.print(" (expects: " + String.join("|", r.consumes) + ")"); if(r.produces != null && r.produces.length > 0) - out.print("(returns: " + Arrays.toString(r.produces) + ")"); + out.print(" (returns: " + String.join("|", r.produces) + ")"); out.println(); BiConsumer printParam = (name, params) -> { @@ -111,7 +112,7 @@ public class RouteCompass extends AbstractProcessor { out.print(name != null ? "\t\t\t" : "\t\t"); out.print("- " + p.typeFQN + " " + p.name); if(p.defaultValue != null) - out.print(" " + "(default: " + p.defaultValue + ")"); + out.print(" (default: " + p.defaultValue + ")"); out.println(); } }; @@ -140,19 +141,23 @@ public class RouteCompass extends AbstractProcessor { * @param element the {@link Element} currently being examined * @return the full route of the endpoint */ - private String getFullRoute(TypeElement annotationType, Element element) { - try { //TODO support multiple routes + private String[] getFullRoute(TypeElement annotationType, Element element) { + try { String[] routes = this.getAnnotationFieldsValue( annotationType, element, (arr) -> Arrays.deepEquals(arr, new String[] {}), "path", "value"); - return this.getParentOrFallback(element, routes[0], (a, e) -> { - String parent = this.getFullRoute(a, e); - StringBuilder sb = new StringBuilder(parent); - if(!parent.endsWith("/")) sb.append("/"); - sb.append(routes[0]); - return sb.toString(); + return this.getParentOrFallback(element, routes, (a, e) -> { + //assume parent doesn't have multiple routes + String parent = this.getFullRoute(a, e)[0]; + for(int i = 0; i < routes.length; i++) { + StringBuilder sb = new StringBuilder(parent); + if(!parent.endsWith("/")) sb.append("/"); + sb.append(routes[i]); + routes[i] = sb.toString(); + } + return routes; }); } catch (ReflectiveOperationException ex) { throw new RuntimeException(ex); //if it fails something went very wrong @@ -249,7 +254,7 @@ public class RouteCompass extends AbstractProcessor { if(defaultValue.equals(ValueConstants.DEFAULT_NONE)) defaultValue = null; - return new Route.Param(name, defaultValue, p.asType().toString()); + return new Route.Param(name, p.asType().toString(), defaultValue); }).filter(Objects::nonNull).toArray(Route.Param[]::new); } @@ -276,7 +281,7 @@ public class RouteCompass extends AbstractProcessor { else typeElement = null; } while(typeElement != null); - return new Route.DTO(type.asType().toString(), fieldElements.stream() //TODO @JsonIgnore + return new Route.DTO(type.asType().toString(), fieldElements.stream() .map(e -> new Route.Param(e.asType().toString(), e.getSimpleName().toString(), null)) .toArray(Route.Param[]::new)); }