-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1059 from micronaut-projects/generator-improve
Improvements for the generator
- Loading branch information
Showing
50 changed files
with
2,376 additions
and
1,439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
openapi-generator/src/main/java/io/micronaut/openapi/generator/Formatting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.openapi.generator; | ||
|
||
import com.samskivert.mustache.Mustache; | ||
import com.samskivert.mustache.Template; | ||
|
||
import java.io.IOException; | ||
import java.io.Writer; | ||
import java.util.Arrays; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* A class with lambdas to format mustache-generated code and formatting utility functions. | ||
*/ | ||
public enum Formatting { | ||
/* This class is not supposed to be initialized */; | ||
|
||
/** | ||
* Remove whitespace on the right of the line. | ||
* @param line The line to be trimmed. | ||
* @return The trimmed line. | ||
*/ | ||
public static String rightTrim(String line) { | ||
int end = 0; | ||
for (int i = line.length(); i > 0; --i) { | ||
if (!Character.isWhitespace(line.charAt(i - 1))) { | ||
end = i; | ||
break; | ||
} | ||
} | ||
return line.substring(0, end); | ||
} | ||
|
||
/** | ||
* Remove whitespace from both sides of the line. | ||
* @param line The line to be trimmed. | ||
* @return The trimmed line. | ||
*/ | ||
public static String trim(String line) { | ||
int start = line.length(); | ||
for (int i = 0; i < line.length(); ++i) { | ||
if (!Character.isWhitespace(line.charAt(i))) { | ||
start = i; | ||
break; | ||
} | ||
} | ||
int end = start; | ||
for (int i = line.length(); i > start; --i) { | ||
if (!Character.isWhitespace(line.charAt(i - 1))) { | ||
end = i; | ||
break; | ||
} | ||
} | ||
return line.substring(start, end); | ||
} | ||
|
||
/** | ||
* A formatter that is responsible for removing extra empty lines in mustache files. | ||
*/ | ||
public static class LineFormatter implements Mustache.Lambda { | ||
|
||
private final int maxEmptyLines; | ||
|
||
/** | ||
* Create the lambda. | ||
* | ||
* @param maxEmptyLines maximal empty lines. | ||
*/ | ||
public LineFormatter(int maxEmptyLines) { | ||
this.maxEmptyLines = maxEmptyLines; | ||
} | ||
|
||
@Override | ||
public void execute(Template.Fragment fragment, Writer writer) throws IOException { | ||
String text = fragment.execute(); | ||
String finalWhitespace = getFinalWhitespace(text); | ||
|
||
String lines = | ||
Arrays.stream(text.split("\n")) | ||
.map(Formatting::rightTrim) | ||
.filter(new LineSkippingPredicate()) | ||
.collect(Collectors.joining("\n")); | ||
if (!lines.isEmpty() && maxEmptyLines == 0) { | ||
lines = "\n" + lines + "\n"; | ||
} | ||
if (!lines.isEmpty()) { | ||
lines = lines + finalWhitespace; | ||
} | ||
writer.write(lines); | ||
} | ||
|
||
private String getFinalWhitespace(String text) { | ||
int i = text.length(); | ||
while (i > 0 && Character.isWhitespace(text.charAt(i - 1)) && text.charAt(i - 1) != '\n') { | ||
--i; | ||
} | ||
return text.substring(i); | ||
} | ||
|
||
private class LineSkippingPredicate implements Predicate<String> { | ||
private int emptyLines; | ||
|
||
@Override | ||
public boolean test(String s) { | ||
if (s.isBlank()) { | ||
++emptyLines; | ||
} else { | ||
emptyLines = 0; | ||
} | ||
return emptyLines <= maxEmptyLines; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* A formatter that collects everything in a single line. | ||
*/ | ||
public static class SingleLineFormatter implements Mustache.Lambda { | ||
|
||
@Override | ||
public void execute(Template.Fragment fragment, Writer writer) throws IOException { | ||
String text = | ||
fragment.execute() | ||
.replaceAll("\\s+", " ") | ||
.replaceAll("(?<=<)\\s+|\\s+(?=>)", ""); | ||
writer.write(Formatting.trim(text)); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* A lambda that allows indenting its contents. | ||
*/ | ||
public static class IndentFormatter implements Mustache.Lambda { | ||
|
||
private final String indent; | ||
|
||
public IndentFormatter(int indentSize) { | ||
indent = " ".repeat(Math.max(0, indentSize)); | ||
} | ||
|
||
@Override | ||
public void execute(Template.Fragment fragment, Writer writer) throws IOException { | ||
String text = | ||
Arrays.stream(fragment.execute().split("\n")) | ||
.map(line -> indent + line) | ||
.collect(Collectors.joining("\n")); | ||
writer.write(text); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...i-generator/src/main/resources/templates/java-micronaut/client/params/bodyParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{{#isBodyParam}}@Body {{>common/params/beanValidation}}{{>client/params/type}} {{paramName}}{{/isBodyParam}} | ||
{{#isBodyParam}}@Body {{>common/params/validation}}{{>client/params/type}} {{paramName}}{{/isBodyParam}} |
Oops, something went wrong.