Skip to content

Commit

Permalink
Lambda refactors (#16369)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
devhl-labs authored Aug 24, 2023
1 parent da411b3 commit 7e67e3a
Show file tree
Hide file tree
Showing 21 changed files with 61 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,9 @@ protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
.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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,15 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public void processOpts() {
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public void processOpts() {
@Override
protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
return super.addMustacheLambdas()
.put("indented_4", new IndentedLambda(4, " "));
.put("indented_4", new IndentedLambda(4, " ", false));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ protected String getEnumDefaultValue(String defaultValue, String dataType) {
@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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
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;
Expand All @@ -43,6 +43,9 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
if (generator != null && generator.reservedWords().contains(text)) {
text = generator.escapeReservedWord(text);
}
writer.write(text);

if (text != null) {
writer.write(text);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
* {@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ public FirstLambda(String delimiter) {

@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(""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,25 @@
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);
}

/**
* Constructs a new instance of {@link IndentedLambda}, with customized indent count and intention character
*
* @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);
}

/**
Expand All @@ -69,19 +71,10 @@ public IndentedLambda(int prefixSpaceCount, String indentionCharacter) {
* @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);
}

/**
Expand All @@ -90,8 +83,9 @@ private IndentedLambda(int prefixSpaceCount, int indentionCodePoint) {
* @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");
}
Expand All @@ -103,6 +97,7 @@ private IndentedLambda(int prefixSpaceCount, int indentionCodePoint, String pref
this.prefixSpaceCount = prefixSpaceCount;
this.spaceCode = indentionCodePoint;
this.prefix = prefix;
this.indentFirstLine = indentFirstLine;
}

@Override
Expand All @@ -119,7 +114,7 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;
import org.openapitools.codegen.CodegenConfig;

import java.io.IOException;
import java.io.Writer;
Expand All @@ -38,16 +37,11 @@
* </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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
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
*
Expand All @@ -42,7 +40,6 @@
*/
public class OptionalParameterLambda implements Mustache.Lambda {
private CodegenConfig generator = null;
private Boolean escapeParam = false;

public OptionalParameterLambda() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@

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
*
Expand All @@ -40,16 +37,9 @@
* </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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.io.IOException;
import java.io.Writer;
import java.util.Locale;

import static org.openapitools.codegen.utils.StringUtils.underscore;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* </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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,18 @@
* </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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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}}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -513,6 +513,6 @@
{{/isUuid}}
{{/allVars}}
{{/lambda.trimLineBreaks}}
{{/lambda.trimTrailingWhiteSpace}}
{{/lambda.trimTrailingWithNewLine}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -94,7 +94,7 @@ namespace {{packageName}}.{{apiPackage}}
}

{{/operation}}
{{/lambda.trimTrailingWhiteSpace}}
{{/lambda.trimTrailingWithNewLine}}
}

/// <summary>
Expand Down Expand Up @@ -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}}
Expand All @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -80,7 +80,7 @@ namespace {{packageName}}.Test.Model
}
{{/vars}}
{{/lambda.trimLineBreaks}}
{{/lambda.trimTrailingWhiteSpace}}
{{/lambda.trimTrailingWithNewLine}}
}
}
{{/model}}
Expand Down
Loading

0 comments on commit 7e67e3a

Please sign in to comment.