fix: assorted fixes

This commit is contained in:
zaaarf 2024-01-23 12:35:31 +01:00
parent 145040c2c6
commit ff54940b4a
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
2 changed files with 30 additions and 32 deletions

View file

@ -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];

View file

@ -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<String, Route.Param[]> 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));
}