Lambda refactors (#16369)

* started source generator

* copy the options

* fixed visibility

* added new sample

* discarded changes to existing samples

* discarded changes to existing samples

* build new sample

* changed package name due to file path length limit

* reverted changes to manual tests

* fixed all new manual tests

* inject contexts into api

* only one JsonConstructor

* fixed spacing

* reverted samples changes

* reverted more unrelated changes

* reverted more unrelated changes

* minor refactors
This commit is contained in:
devhl-labs
2023-08-23 21:56:56 -04:00
committed by GitHub
parent da411b3f3f
commit 7e67e3aafb
21 changed files with 61 additions and 77 deletions
@@ -447,9 +447,9 @@ public class DefaultCodegen implements CodegenConfig {
.put("camelcase", new CamelCaseLambda(true).generator(this))
.put("pascalcase", new CamelCaseLambda(false).generator(this))
.put("indented", new IndentedLambda())
.put("indented_8", new IndentedLambda(8, " "))
.put("indented_12", new IndentedLambda(12, " "))
.put("indented_16", new IndentedLambda(16, " "));
.put("indented_8", new IndentedLambda(8, " ", false))
.put("indented_12", new IndentedLambda(12, " ", false))
.put("indented_16", new IndentedLambda(16, " ", false));
}
private void registerMustacheLambdas() {
@@ -412,15 +412,15 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
return super.addMustacheLambdas()
.put("camelcase_param", new CamelCaseLambda().generator(this).escapeAsParamName(true))
.put("required", new RequiredParameterLambda().generator(this))
.put("required", new RequiredParameterLambda())
.put("optional", new OptionalParameterLambda().generator(this))
.put("joinWithComma", new JoinWithCommaLambda())
.put("trimLineBreaks", new TrimLineBreaksLambda())
.put("trimTrailingWhiteSpace", new TrimTrailingWhiteSpaceLambda())
.put("trimTrailingWithNewLine", new TrimTrailingWhiteSpaceLambda(true))
.put("first", new FirstLambda(" "))
.put("firstDot", new FirstLambda("\\."))
.put("indent3", new IndentedLambda(12, " "))
.put("indent4", new IndentedLambda(16, " "));
.put("indent3", new IndentedLambda(12, " ", false))
.put("indent4", new IndentedLambda(16, " ", false));
}
@Override
@@ -331,7 +331,7 @@ abstract public class AbstractCppCodegen extends DefaultCodegen implements Codeg
@Override
protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
return super.addMustacheLambdas()
.put("multiline_comment_4", new IndentedLambda(4, " ", "///"));
.put("multiline_comment_4", new IndentedLambda(4, " ", "///", false));
}
@Override
@@ -224,7 +224,7 @@ public class ScalaPlayFrameworkServerCodegen extends AbstractScalaCodegen implem
@Override
protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
return super.addMustacheLambdas()
.put("indented_4", new IndentedLambda(4, " "));
.put("indented_4", new IndentedLambda(4, " ", false));
}
@Override
@@ -308,8 +308,8 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
@Override
protected ImmutableMap.Builder<String, Mustache.Lambda> addMustacheLambdas() {
ImmutableMap.Builder<String, Mustache.Lambda> lambdas = super.addMustacheLambdas();
lambdas.put("indented_star_1", new IndentedLambda(1, " ", "* "));
lambdas.put("indented_star_4", new IndentedLambda(5, " ", "* "));
lambdas.put("indented_star_1", new IndentedLambda(1, " ", "* ", false));
lambdas.put("indented_star_4", new IndentedLambda(5, " ", "* ", false));
return lambdas;
}
@@ -24,8 +24,8 @@ import java.io.Writer;
public class CaseFormatLambda implements Mustache.Lambda {
private CodegenConfig generator = null;
private CaseFormat initialFormat;
private CaseFormat targetFormat;
private final CaseFormat initialFormat;
private final CaseFormat targetFormat;
public CaseFormatLambda(CaseFormat target, CaseFormat targetFormat) {
this.initialFormat = target;
@@ -43,6 +43,9 @@ public class CaseFormatLambda implements Mustache.Lambda {
if (generator != null && generator.reservedWords().contains(text)) {
text = generator.escapeReservedWord(text);
}
writer.write(text);
if (text != null) {
writer.write(text);
}
}
}
@@ -33,8 +33,8 @@ import java.io.Writer;
* {@code {{#lambda.escapeDollar}}{{name}}{{/lambda.escapeDollar}} }
*/
public class EscapeChar implements Mustache.Lambda {
private String matchPattern;
private String replacement;
private final String matchPattern;
private final String replacement;
/**
* Constructs a new instance of {@link EscapeChar}, with the desired character to escape
@@ -45,10 +45,6 @@ public class FirstLambda implements Mustache.Lambda {
@Override
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
String a = fragment.execute();
String[] parts = fragment.execute().split(this.delimiter);
writer.write(Arrays.stream(parts).findFirst().orElse(""));
@@ -44,13 +44,14 @@ import java.io.Writer;
public class IndentedLambda implements Mustache.Lambda {
private final int prefixSpaceCount;
private final String prefix;
private int spaceCode;
private final int spaceCode;
private final boolean indentFirstLine;
/**
* Constructs a new instance of {@link IndentedLambda}, with an indent count of 4 spaces
*/
public IndentedLambda() {
this(4, " ", null);
this(4, " ", null, false);
}
/**
@@ -58,9 +59,10 @@ public class IndentedLambda implements Mustache.Lambda {
*
* @param prefixSpaceCount The number of indented characters to apply as a prefix to a fragment.
* @param indentionCharacter String representation of the character used in the indent (e.g. " ", "\t", ".").
* @param indentFirstLine Whether to indent the first line or not. Usually this is handled by the template already.
*/
public IndentedLambda(int prefixSpaceCount, String indentionCharacter) {
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), null);
public IndentedLambda(int prefixSpaceCount, String indentionCharacter, boolean indentFirstLine) {
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), null, indentFirstLine);
}
/**
@@ -69,19 +71,10 @@ public class IndentedLambda implements Mustache.Lambda {
* @param prefixSpaceCount The number of indented characters to apply as a prefix to a fragment.
* @param indentionCharacter String representation of the character used in the indent (e.g. " ", "\t", ".").
* @param prefix An optional prefix to prepend before the line (useful for multi-line comments).
* @param indentFirstLine Whether to indent the first line or not. Usually this is handled by the template already.
*/
public IndentedLambda(int prefixSpaceCount, String indentionCharacter, String prefix) {
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), prefix);
}
/**
* Constructs a new instance of {@link IndentedLambda}
*
* @param prefixSpaceCount The number of indented characters to apply as a prefix to a fragment.
* @param indentionCodePoint Code point of the single character used for indentation.
*/
private IndentedLambda(int prefixSpaceCount, int indentionCodePoint) {
this(prefixSpaceCount, indentionCodePoint, null);
public IndentedLambda(int prefixSpaceCount, String indentionCharacter, String prefix, boolean indentFirstLine) {
this(prefixSpaceCount, Character.codePointAt(indentionCharacter, 0), prefix, indentFirstLine);
}
/**
@@ -90,8 +83,9 @@ public class IndentedLambda implements Mustache.Lambda {
* @param prefixSpaceCount The number of indented characters to apply as a prefix to a fragment.
* @param indentionCodePoint Code point of the single character used for indentation.
* @param prefix An optional prefix to prepend before the line (useful for multi-line comments).
* @param indentFirstLine Whether to indent the first line or not. Usually this is handled by the template already.
*/
private IndentedLambda(int prefixSpaceCount, int indentionCodePoint, String prefix) {
private IndentedLambda(int prefixSpaceCount, int indentionCodePoint, String prefix, boolean indentFirstLine) {
if (prefixSpaceCount <= 0) {
throw new IllegalArgumentException("prefixSpaceCount must be greater than 0");
}
@@ -103,6 +97,7 @@ public class IndentedLambda implements Mustache.Lambda {
this.prefixSpaceCount = prefixSpaceCount;
this.spaceCode = indentionCodePoint;
this.prefix = prefix;
this.indentFirstLine = indentFirstLine;
}
@Override
@@ -119,7 +114,7 @@ public class IndentedLambda implements Mustache.Lambda {
String line = lines[i];
// Mustache will apply correct indentation to the first line of a template (to match declaration location).
// So, we want to skip the first line.
if (i > 0) {
if (this.indentFirstLine || i > 0) {
sb.append(prefixedIndention);
if (prefix != null) sb.append(prefix);
}
@@ -19,7 +19,6 @@ package org.openapitools.codegen.templating.mustache;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;
import org.openapitools.codegen.CodegenConfig;
import java.io.IOException;
import java.io.Writer;
@@ -38,16 +37,11 @@ import java.io.Writer;
* </pre>
*/
public class JoinWithCommaLambda implements Mustache.Lambda {
private CodegenConfig generator = null;
public JoinWithCommaLambda() {
}
public JoinWithCommaLambda generator(final CodegenConfig generator) {
this.generator = generator;
return this;
}
@Override
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
@@ -25,8 +25,6 @@ import org.openapitools.codegen.languages.AbstractCSharpCodegen;
import java.io.IOException;
import java.io.Writer;
import static org.openapitools.codegen.utils.StringUtils.camelize;
/**
* Appends trailing ? to a text fragment if not already present
*
@@ -42,7 +40,6 @@ import static org.openapitools.codegen.utils.StringUtils.camelize;
*/
public class OptionalParameterLambda implements Mustache.Lambda {
private CodegenConfig generator = null;
private Boolean escapeParam = false;
public OptionalParameterLambda() {}
@@ -19,13 +19,10 @@ package org.openapitools.codegen.templating.mustache;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;
import org.openapitools.codegen.CodegenConfig;
import java.io.IOException;
import java.io.Writer;
import static org.openapitools.codegen.utils.StringUtils.camelize;
/**
* Strips trailing ? from a text fragment
*
@@ -40,16 +37,9 @@ import static org.openapitools.codegen.utils.StringUtils.camelize;
* </pre>
*/
public class RequiredParameterLambda implements Mustache.Lambda {
private CodegenConfig generator = null;
private Boolean escapeParam = false;
public RequiredParameterLambda() {}
public RequiredParameterLambda generator(final CodegenConfig generator) {
this.generator = generator;
return this;
}
@Override
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
String text = fragment.execute();
@@ -21,7 +21,6 @@ import com.samskivert.mustache.Template;
import java.io.IOException;
import java.io.Writer;
import java.util.Locale;
import static org.openapitools.codegen.utils.StringUtils.underscore;
@@ -38,7 +38,7 @@ import java.util.Locale;
* </pre>
*/
public class TitlecaseLambda implements Mustache.Lambda {
private String delimiter;
private final String delimiter;
/**
* Constructs a new instance of {@link TitlecaseLambda}, which will convert all text
@@ -36,8 +36,18 @@ import com.samskivert.mustache.Template.Fragment;
* </pre>
*/
public class TrimTrailingWhiteSpaceLambda implements Mustache.Lambda {
private final boolean withNewLine;
public TrimTrailingWhiteSpaceLambda(boolean withNewLine) {
this.withNewLine = withNewLine;
}
@Override
public void execute(Fragment fragment, Writer writer) throws IOException {
writer.write(fragment.execute().stripTrailing() + "\n");
writer.write(fragment.execute().stripTrailing());
if (this.withNewLine) {
writer.write("\n");
}
}
}
@@ -28,7 +28,7 @@ namespace {{packageName}}.Test.{{apiPackage}}
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
.Configure{{apiName}}((context, services, options) =>
{
{{#lambda.trimTrailingWhiteSpace}}
{{#lambda.trimTrailingWithNewLine}}
{{#hasApiKeyMethods}}
string apiKeyTokenValue = context.Configuration["<token>"] ?? throw new Exception("Token not found.");
ApiKeyToken apiKeyToken = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}(apiKeyTokenValue, timeout: TimeSpan.FromSeconds(1));
@@ -59,7 +59,7 @@ namespace {{packageName}}.Test.{{apiPackage}}
OAuthToken oauthToken = new{{^net70OrLater}} OAuthToken{{/net70OrLater}}(oauthTokenValue, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(oauthToken);
{{/hasOAuthMethods}}
{{/lambda.trimTrailingWhiteSpace}}
{{/lambda.trimTrailingWithNewLine}}
});
}
}
@@ -351,7 +351,7 @@
/// <exception cref="NotImplementedException"></exception>
public void WriteProperties(ref Utf8JsonWriter writer, {{classname}} {{#lambda.camelcase_param}}{{classname}}{{/lambda.camelcase_param}}, JsonSerializerOptions jsonSerializerOptions)
{
{{#lambda.trimTrailingWhiteSpace}}
{{#lambda.trimTrailingWithNewLine}}
{{#lambda.trimLineBreaks}}
{{#allVars}}
{{#isString}}
@@ -513,6 +513,6 @@
{{/isUuid}}
{{/allVars}}
{{/lambda.trimLineBreaks}}
{{/lambda.trimTrailingWhiteSpace}}
{{/lambda.trimTrailingWithNewLine}}
}
}
@@ -71,7 +71,7 @@ namespace {{packageName}}.{{apiPackage}}
/// </summary>
{{>visibility}} class {{classname}}Events
{
{{#lambda.trimTrailingWhiteSpace}}
{{#lambda.trimTrailingWithNewLine}}
{{#operation}}
/// <summary>
/// The event raised after the server response
@@ -94,7 +94,7 @@ namespace {{packageName}}.{{apiPackage}}
}
{{/operation}}
{{/lambda.trimTrailingWhiteSpace}}
{{/lambda.trimTrailingWithNewLine}}
}
/// <summary>
@@ -183,7 +183,7 @@ namespace {{packageName}}.{{apiPackage}}
/// <returns></returns>
private void Validate{{operationId}}({{#vendorExtensions.x-not-nullable-reference-types}}{{^required}}Option<{{/required}}{{{dataType}}}{{>NullConditionalParameter}}{{^required}}>{{/required}} {{paramName}}{{^-last}}, {{/-last}}{{/vendorExtensions.x-not-nullable-reference-types}})
{
{{#lambda.trimTrailingWhiteSpace}}
{{#lambda.trimTrailingWithNewLine}}
{{#vendorExtensions.x-not-nullable-reference-types}}
{{#required}}
{{^vendorExtensions.x-is-value-type}}
@@ -200,7 +200,7 @@ namespace {{packageName}}.{{apiPackage}}
{{/vendorExtensions.x-is-value-type}}
{{/required}}
{{/vendorExtensions.x-not-nullable-reference-types}}
{{/lambda.trimTrailingWhiteSpace}}
{{/lambda.trimTrailingWithNewLine}}
}
{{/vendorExtensions.x-has-not-nullable-reference-types}}
@@ -40,7 +40,7 @@ namespace {{packageName}}.Test.Model
// Cleanup when everything is done.
}
{{#lambda.trimTrailingWhiteSpace}}
{{#lambda.trimTrailingWithNewLine}}
{{#lambda.trimLineBreaks}}
/// <summary>
/// Test an instance of {{classname}}
@@ -80,7 +80,7 @@ namespace {{packageName}}.Test.Model
}
{{/vars}}
{{/lambda.trimLineBreaks}}
{{/lambda.trimTrailingWhiteSpace}}
{{/lambda.trimTrailingWithNewLine}}
}
}
{{/model}}
@@ -97,32 +97,32 @@
{{#vendorExtensions.x-is-value-type}}
{{#isNullable}}
if (this.{{{name}}} != null){
{{#lambda.trimTrailingWhiteSpace}}
{{#lambda.trimTrailingWithNewLine}}
{{#lambda.indent4}}
{{>ValidateRegex}}
{{/lambda.indent4}}
{{/lambda.trimTrailingWhiteSpace}}
{{/lambda.trimTrailingWithNewLine}}
}
{{/isNullable}}
{{^isNullable}}
{{#lambda.trimTrailingWhiteSpace}}
{{#lambda.trimTrailingWithNewLine}}
{{#lambda.indent3}}
{{>ValidateRegex}}
{{/lambda.indent3}}
{{/lambda.trimTrailingWhiteSpace}}
{{/lambda.trimTrailingWithNewLine}}
{{/isNullable}}
{{/vendorExtensions.x-is-value-type}}
{{^vendorExtensions.x-is-value-type}}
if (this.{{{name}}} != null) {
{{#lambda.trimTrailingWhiteSpace}}
{{#lambda.trimTrailingWithNewLine}}
{{#lambda.indent4}}
{{>ValidateRegex}}
{{/lambda.indent4}}
{{/lambda.trimTrailingWhiteSpace}}
{{/lambda.trimTrailingWithNewLine}}
}
{{/vendorExtensions.x-is-value-type}}
@@ -23,7 +23,7 @@ public class IndentedLambdaTest extends LambdaTest {
@Test
public void indentedCountTest() {
// Given
Map<String, Object> ctx = context("indented", new IndentedLambda(8, " "));
Map<String, Object> ctx = context("indented", new IndentedLambda(8, " ", false));
// When & Then
// IndentedLambda applies indentation from second line on of a template.