Skip to content

Commit

Permalink
Format only change for ClientMethodWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Oct 18, 2023
1 parent 347522b commit 7e06ccb
Showing 1 changed file with 41 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -47,18 +45,17 @@ class ClientMethodWriter {
this.timeout = method.timeout();
this.useConfig = ProcessingContext.typeElement("io.avaje.config.Config") != null;

this.segmentPropertyMap =
method.pathSegments().segments().stream()
.filter(Segment::isProperty)
.collect(toMap(Segment::name, s -> Util.sanitizeName(s.name()).toUpperCase()));
this.segmentPropertyMap = method.pathSegments().segments().stream()
.filter(Segment::isProperty)
.collect(toMap(Segment::name, s -> Util.sanitizeName(s.name()).toUpperCase()));
}

void addImportTypes(ControllerReader reader) {
reader.addImportTypes(returnType.importTypes());
method.throwsList().stream()
.map(UType::parse)
.map(UType::importTypes)
.forEach(reader::addImportTypes);
.map(UType::parse)
.map(UType::importTypes)
.forEach(reader::addImportTypes);

for (final MethodParam param : method.params()) {
reader.addImportTypes(param.utype().importTypes());
Expand All @@ -75,12 +72,11 @@ private void methodStart(Append writer) {
}
method.checkArgumentNames();

segmentPropertyMap.forEach(
(k, v) -> {
writer.append(" private static final String %s = ", v);
final String getProperty = useConfig ? "Config.get(" : "System.getProperty(";
writer.append(getProperty).append("\"%s\");", k).eol();
});
segmentPropertyMap.forEach((k, v) -> {
writer.append(" private static final String %s = ", v);
final String getProperty = useConfig ? "Config.get(" : "System.getProperty(";
writer.append(getProperty).append("\"%s\");", k).eol();
});

writer.append(" // %s %s", webMethod, method.webMethodPath()).eol();
writer.append(" @Override").eol();
Expand All @@ -96,9 +92,9 @@ private void methodStart(Append writer) {
final var isVarArg = Util.isVarArg(param.element(), i);

final var paramType =
"java.util.function.Supplier<?extendsjava.io.InputStream>".equals(param.utype().full())
? "Supplier<? extends InputStream>"
: param.utype().shortType();
"java.util.function.Supplier<?extendsjava.io.InputStream>".equals(param.utype().full())
? "Supplier<? extends InputStream>"
: param.utype().shortType();

if (param.overrideVarNameError()) {
writer.append("/** !Error! */ ");
Expand Down Expand Up @@ -144,7 +140,6 @@ void write() {
}

private void writeTimeout(RequestTimeoutPrism p) {

writer.append(" .requestTimeout(of(%s, %s))", p.value(), p.chronoUnit()).eol();
}

Expand Down Expand Up @@ -220,11 +215,10 @@ private void writeResponse(UType type) {

void writeGeneric(UType type) {
if (useJsonb && type.isGeneric()) {
final var params =
type.importTypes().stream()
.skip(1)
.map(Util::shortName)
.collect(Collectors.joining(".class, "));
final var params = type.importTypes().stream()
.skip(1)
.map(Util::shortName)
.collect(Collectors.joining(".class, "));

writer.append("Types.newParameterizedType(%s.class, %s.class)", Util.shortName(type.mainType()), params);
} else {
Expand Down Expand Up @@ -256,8 +250,8 @@ private void writeQueryParams(PathSegments pathSegments) {
writer.append(" // Refer to: https://avaje.io/http/client/import#error").eol();
writer.eol();
logError(
"Explicit @QueryParam/@Header annotations required when using @Client.Import on an interface that has already been compiled.",
method.element());
"Explicit @QueryParam/@Header annotations required when using @Client.Import on an interface that has already been compiled.",
method.element());
}
}

Expand Down Expand Up @@ -331,27 +325,27 @@ private void writeBody() {

private void writeErrorMapper() {
method.throwsList().stream()
.map(ProcessingContext::asElement)
.filter(
e ->
isAssignable2Interface(
e.getQualifiedName().toString(), "java.lang.RuntimeException"))
.filter(
e ->
ElementFilter.constructorsIn(e.getEnclosedElements()).stream()
.filter(c -> c.getParameters().size() == 1)
.map(c -> c.getParameters().get(0).asType().toString())
.map(Util::trimAnnotations)
.anyMatch("io.avaje.http.client.HttpException"::equals))
.findFirst()
.map(TypeElement::getQualifiedName)
.map(Object::toString)
.map(Util::shortName)
.ifPresent(
exception ->
writer
.append(" .errorMapper(%s::new)", exception)
.eol());
.map(ProcessingContext::asElement)
.filter(
e ->
isAssignable2Interface(
e.getQualifiedName().toString(), "java.lang.RuntimeException"))
.filter(
e ->
ElementFilter.constructorsIn(e.getEnclosedElements()).stream()
.filter(c -> c.getParameters().size() == 1)
.map(c -> c.getParameters().get(0).asType().toString())
.map(Util::trimAnnotations)
.anyMatch("io.avaje.http.client.HttpException"::equals))
.findFirst()
.map(TypeElement::getQualifiedName)
.map(Object::toString)
.map(Util::shortName)
.ifPresent(
exception ->
writer
.append(" .errorMapper(%s::new)", exception)
.eol());
}

/**
Expand All @@ -376,11 +370,8 @@ private void writePaths(Set<PathSegments.Segment> segments) {
if (segment.isLiteral()) {
writer.append(".path(\"").append(segment.literalSection()).append("\")");
} else if (segment.isProperty()) {

writer.append(".path(").append(segmentPropertyMap.get(segment.name())).append(")");

} else {

writer.append(".path(").append(segment.name()).append(")");
// TODO: matrix params
}
Expand Down

0 comments on commit 7e06ccb

Please sign in to comment.