Skip to content

Create @MatrixParam Annotation #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions http-api/src/main/java/io/avaje/http/api/MatrixParam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.avaje.http.api;

import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/** Marks a method parameter to be a matrix parameter. */
@Target(value = {PARAMETER})
@Retention(value = RUNTIME)
public @interface MatrixParam {

/**
* The name of the matrix parameter.
*
* <p>If left blank the method parameter name is used.
*
* <p>We typically use this when the matrix parameter uses snake-case or similar that does not map
* to a valid java/kotlin parameter name.
*/
String value() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ElementReader {

private String paramName;
private ParamType paramType;
private String matrixParamName;
private boolean impliedParamType;
private String paramDefault;

Expand Down Expand Up @@ -110,6 +111,15 @@ private void readAnnotations(Element element, ParamType defaultType) {
this.paramDefault = null;
return;
}

MatrixParam matrixParam = element.getAnnotation(MatrixParam.class);
if (matrixParam != null) {
this.matrixParamName = nameFrom(matrixParam.value(), varName);
this.paramType = defaultType;
this.impliedParamType = true;
return;
}

if (paramType == null) {
this.impliedParamType = true;
if (typeHandler != null) {
Expand Down Expand Up @@ -235,15 +245,19 @@ private boolean setValue(Append writer, PathSegments segments, String shortType)
return false;
}
if (impliedParamType) {
PathSegments.Segment segment = segments.segment(varName);
var name = matrixParamName != null ? matrixParamName : varName;
PathSegments.Segment segment = segments.segment(name);
if (segment != null) {
// path or matrix parameter
boolean requiredParam = segment.isRequired(varName);
String asMethod = (typeHandler == null) ? null : (requiredParam) ? typeHandler.asMethod() : typeHandler.toMethod();
String asMethod =
(typeHandler == null)
? null
: (requiredParam) ? typeHandler.asMethod() : typeHandler.toMethod();
if (asMethod != null) {
writer.append(asMethod);
}
segment.writeGetVal(writer, varName, ctx.platform());
segment.writeGetVal(writer, name, ctx.platform());
if (asMethod != null) {
writer.append(")");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

public class PathSegments {

Expand Down Expand Up @@ -132,7 +133,9 @@ public boolean isEmpty() {

public static class Segment {

private static final Pattern PATTERN = Pattern.compile("[^a-zA-Z0-9_]|\\s");
private final String name;
private final String sanitizedName;
private final String literalSection;

/**
Expand All @@ -150,6 +153,7 @@ public static class Segment {
*/
Segment(String name) {
this.name = name;
this.sanitizedName = PATTERN.matcher(name).replaceAll("_");
this.literalSection = null;
this.matrixKeys = null;
this.matrixVarNames = null;
Expand All @@ -160,6 +164,7 @@ public static class Segment {
*/
Segment(String name, Set<String> matrixKeys) {
this.name = name;
this.sanitizedName = PATTERN.matcher(name).replaceAll("_");
this.literalSection = null;
this.matrixKeys = matrixKeys;
this.matrixVarNames = new HashSet<>();
Expand All @@ -172,8 +177,10 @@ public static class Segment {
* Create a literal path segment.
*/
public Segment(String section, boolean literalDummy) {

this.literalSection = section;
this.name = null;
this.sanitizedName = null;
this.matrixKeys = null;
this.matrixVarNames = null;
}
Expand Down Expand Up @@ -219,7 +226,7 @@ void writeGetVal(Append writer, String varName, PlatformAdapter platform) {
if (!hasMatrixParams()) {
platform.writeReadParameter(writer, ParamType.PATHPARAM, name);
} else {
writer.append("%s_segment.", name);
writer.append("%s_segment.", sanitizedName);
if (name.equals(varName)) {
writer.append("val()");
} else {
Expand All @@ -238,7 +245,7 @@ private String matrixKey(String varName) {

public void writeCreateSegment(Append writer, PlatformAdapter platform) {
writer.append(platform.indent());
writer.append(" PathSegment %s_segment = PathSegment.of(", name);
writer.append(" PathSegment %s_segment = PathSegment.of(", sanitizedName);
platform.writeReadParameter(writer, ParamType.PATHPARAM, name + "_segment");
writer.append(");").eol();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import io.avaje.http.api.Post;
import io.avaje.http.api.Produces;
import io.avaje.http.api.Put;
import io.avaje.http.api.MatrixParam;
import io.javalin.http.Context;

@Path("test/")
@Controller
public class TestController {
Expand Down Expand Up @@ -105,4 +107,15 @@ String testForm(String name, String email, String url) {
String testFormBean(MyForm form) {
return form.name + "|" + form.email + "|" + form.url;
}

@Get("/withMatrixParam/{type-1;category;vendor-34}/{range;style}")
void neo(
@MatrixParam("type-1") String type,
String category,
@MatrixParam("vendor-34") String vendor,
String range,
String style) {

System.out.println("Ever have that feeling where you're not sure if you're awake or dreaming?");
}
}