Skip to content

Commit

Permalink
Refactor copy lambda (#19983)
Browse files Browse the repository at this point in the history
* comment out broken tests

* refactored copy lambda

* added tests
  • Loading branch information
devhl-labs authored Nov 6, 2024
1 parent b51b18e commit f98073d
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.openapitools.codegen.model.OperationMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.templating.mustache.*;
import org.openapitools.codegen.templating.mustache.CopyLambda.CopyContent;
import org.openapitools.codegen.templating.mustache.CopyLambda.WhiteSpaceStrategy;
import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -427,7 +429,7 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio

@Override
protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
CopyLambda copyLambda = new CopyLambda();
final CopyContent copyContent = new CopyContent();

return super.addMustacheLambdas()
.put("camelcase_sanitize_param", new CamelCaseAndSanitizeLambda().generator(this).escapeAsParamName(true))
Expand All @@ -443,12 +445,13 @@ protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
.put("first", new FirstLambda(" "))
.put("firstDot", new FirstLambda("\\."))
.put("indent1", new IndentedLambda(4, " ", false, true))
.put("indentAll1", new IndentedLambda(4, " ", true, true))
.put("indent3", new IndentedLambda(12, " ", false, true))
.put("indent4", new IndentedLambda(16, " ", false, true))
.put("copy", copyLambda)
.put("paste", new PasteLambda(copyLambda, true, true, true, false))
.put("pasteOnce", new PasteLambda(copyLambda, true, true, true, true))
.put("pasteLine", new PasteLambda(copyLambda, true, true, false, false))
.put("copy", new CopyLambda(copyContent, WhiteSpaceStrategy.None, WhiteSpaceStrategy.None))
.put("copyText", new CopyLambda(copyContent, WhiteSpaceStrategy.Strip, WhiteSpaceStrategy.StripLineBreakIfPresent))
.put("paste", new PasteLambda(copyContent, false))
.put("pasteOnce", new PasteLambda(copyContent, true))
.put("uniqueLines", new UniqueLambda("\n", false))
.put("unique", new UniqueLambda("\n", true))
.put("camel_case", new CamelCaseLambda())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* Register:
* <pre>
* additionalProperties.put("copy", new CopyLambda());
* additionalProperties.put("copy", new CopyLambda(new CopyContent()));
* </pre>
*
* Use:
Expand All @@ -36,13 +36,55 @@
* </pre>
*/
public class CopyLambda implements Mustache.Lambda {
public String savedContent;
public static class CopyContent {
public String content;
}

public CopyContent copyContent;
private final WhiteSpaceStrategy leadingWhiteSpaceStrategy;
private final WhiteSpaceStrategy trailingWhiteSpaceStrategy;

public enum WhiteSpaceStrategy {
None,
AppendLineBreakIfMissing,
Strip,
StripLineBreakIfPresent
}

public CopyLambda() {
public CopyLambda(CopyContent content, WhiteSpaceStrategy leadingWhiteSpaceStrategy, WhiteSpaceStrategy trailingWhiteSpaceStrategy) {
this.copyContent = content;
this.leadingWhiteSpaceStrategy = leadingWhiteSpaceStrategy;
this.trailingWhiteSpaceStrategy = trailingWhiteSpaceStrategy;
}

@Override
public void execute(Fragment fragment, Writer writer) throws IOException {
savedContent = fragment.execute().stripTrailing();
String content = fragment.execute();

if (this.leadingWhiteSpaceStrategy == WhiteSpaceStrategy.AppendLineBreakIfMissing && !content.startsWith("\n")){
content = "\n" + content;
}

if (this.leadingWhiteSpaceStrategy == WhiteSpaceStrategy.Strip) {
content = content.stripLeading();
}

if (this.leadingWhiteSpaceStrategy == WhiteSpaceStrategy.StripLineBreakIfPresent && content.startsWith("\n")) {
content = content.substring(1);
}

if (this.trailingWhiteSpaceStrategy == WhiteSpaceStrategy.AppendLineBreakIfMissing && !content.endsWith("\n")){
content = content + "\n";
}

if (this.trailingWhiteSpaceStrategy == WhiteSpaceStrategy.Strip){
content = content.stripTrailing();
}

if (this.trailingWhiteSpaceStrategy == WhiteSpaceStrategy.StripLineBreakIfPresent && content.endsWith("\n")) {
content = content.substring(0, content.length() - 1);
}

this.copyContent.content = content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.IOException;
import java.io.Writer;

import org.openapitools.codegen.templating.mustache.CopyLambda.CopyContent;

import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template.Fragment;

Expand All @@ -27,7 +29,7 @@
*
* Register:
* <pre>
* additionalProperties.put("paste", new PasteLambda(copyLambda, true, true, true, false));
* additionalProperties.put("paste", new PasteLambda(copyContent, false));
* </pre>
*
* Use:
Expand All @@ -36,41 +38,26 @@
* </pre>
*/
public class PasteLambda implements Mustache.Lambda {
private final CopyLambda copyLambda;
private final Boolean stripLeading;
private final Boolean stripTrailing;
private final Boolean endWithLineBreak;
private final CopyContent copyContent;
private final Boolean clear;

public PasteLambda(CopyLambda copyLambda, Boolean stripLeading, Boolean stripTrailing, Boolean endWithLineBreak, boolean clear) {
this.copyLambda = copyLambda;
this.stripLeading = stripLeading;
this.stripTrailing = stripTrailing;
this.endWithLineBreak = endWithLineBreak;
public PasteLambda(CopyContent copyContent, boolean clear) {
this.copyContent = copyContent;
this.clear = clear;
}

@Override
public void execute(Fragment fragment, Writer writer) throws IOException {
String content = this.copyLambda.savedContent;
String content = this.copyContent.content;

if (content == null) {
return;
}

if (this.stripTrailing){
content = content.stripTrailing();
}
if (this.stripLeading) {
content = content.stripLeading();
}
if (this.endWithLineBreak && !content.endsWith("\n")){
content = content + "\n";
}
writer.write(content);

if (this.clear) {
this.copyLambda.savedContent = null;
this.copyContent.content = null;
}

writer.write(content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,12 @@
public override void Write(Utf8JsonWriter writer, {{classname}} {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}, JsonSerializerOptions jsonSerializerOptions)
{
{{#lambda.trimLineBreaks}}
{{#lambda.copy}}
{{#lambda.copyText}}
{{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}
{{/lambda.copy}}
{{/lambda.copyText}}
{{#discriminator}}
{{#children}}
if ({{#lambda.pasteLine}}{{/lambda.pasteLine}} is {{classname}} {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}){
if ({{#lambda.paste}}{{/lambda.paste}} is {{classname}} {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}){
JsonSerializer.Serialize<{{{name}}}>(writer, {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}, jsonSerializerOptions);
return;
}
Expand Down Expand Up @@ -449,9 +449,9 @@
{{^isMap}}
{{^isEnum}}
{{^isUuid}}
{{#lambda.copy}}
{{#lambda.copyText}}
writer.WriteString("{{baseName}}", {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}});
{{/lambda.copy}}
{{/lambda.copyText}}
{{#lambda.indent3}}
{{>WriteProperty}}
{{/lambda.indent3}}
Expand All @@ -460,44 +460,44 @@
{{/isMap}}
{{/isString}}
{{#isBoolean}}
{{#lambda.copy}}
{{#lambda.copyText}}
writer.WriteBoolean("{{baseName}}", {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}{{^required}}Option.Value{{#vendorExtensions.x-is-value-type}}{{nrt!}}.Value{{/vendorExtensions.x-is-value-type}}{{/required}}{{#required}}{{#isNullable}}.Value{{/isNullable}}{{/required}});
{{/lambda.copy}}
{{/lambda.copyText}}
{{#lambda.indent3}}
{{>WriteProperty}}
{{/lambda.indent3}}
{{/isBoolean}}
{{^isEnum}}
{{#isNumeric}}
{{#lambda.copy}}
{{#lambda.copyText}}
writer.WriteNumber("{{baseName}}", {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}{{^required}}Option.Value{{#vendorExtensions.x-is-value-type}}{{nrt!}}.Value{{/vendorExtensions.x-is-value-type}}{{/required}}{{#required}}{{#isNullable}}.Value{{/isNullable}}{{/required}});
{{/lambda.copy}}
{{/lambda.copyText}}
{{#lambda.indent3}}
{{>WriteProperty}}
{{/lambda.indent3}}
{{/isNumeric}}
{{/isEnum}}
{{#isDate}}
{{#lambda.copy}}
{{#lambda.copyText}}
writer.WriteString("{{baseName}}", {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}{{^required}}Option.Value{{#vendorExtensions.x-is-value-type}}{{nrt!}}.Value{{/vendorExtensions.x-is-value-type}}{{/required}}{{#required}}{{#isNullable}}.Value{{/isNullable}}{{/required}}.ToString({{name}}Format));
{{/lambda.copy}}
{{/lambda.copyText}}
{{#lambda.indent3}}
{{>WriteProperty}}
{{/lambda.indent3}}
{{/isDate}}
{{#isDateTime}}
{{#lambda.copy}}
{{#lambda.copyText}}
writer.WriteString("{{baseName}}", {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}{{^required}}Option.Value{{#vendorExtensions.x-is-value-type}}{{nrt!}}.Value{{/vendorExtensions.x-is-value-type}}{{/required}}{{#required}}{{#isNullable}}.Value{{/isNullable}}{{/required}}.ToString({{name}}Format));
{{/lambda.copy}}
{{/lambda.copyText}}
{{#lambda.indent3}}
{{>WriteProperty}}
{{/lambda.indent3}}
{{/isDateTime}}
{{#isEnum}}
{{#isNumeric}}
{{#lambda.copy}}
{{#lambda.copyText}}
writer.WriteNumber("{{baseName}}", {{#isInnerEnum}}{{classname}}.{{/isInnerEnum}}{{{datatypeWithEnum}}}ToJsonValue({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}{{^required}}Option.Value{{#vendorExtensions.x-is-value-type}}{{nrt!}}.Value{{/vendorExtensions.x-is-value-type}}{{/required}}{{#required}}{{#isNullable}}.Value{{/isNullable}}{{/required}}));
{{/lambda.copy}}
{{/lambda.copyText}}
{{#lambda.indent3}}
{{>WriteProperty}}
{{/lambda.indent3}}
Expand All @@ -519,9 +519,9 @@
{{/isNullable}}
{{/isInnerEnum}}
{{^isInnerEnum}}
{{#lambda.copy}}
{{#lambda.copyText}}
{{#lambda.camelcase_sanitize_param}}{{name}}{{/lambda.camelcase_sanitize_param}}
{{/lambda.copy}}
{{/lambda.copyText}}
{{#required}}
{{#isNullable}}
if ({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}} == null)
Expand All @@ -533,7 +533,7 @@
{{#enumVars}}
{{#-first}}
{{#isString}}
if ({{#lambda.pasteLine}}{{/lambda.pasteLine}}RawValue != null){{! we cant use name here because enumVar also has a name property, so use the paste lambda instead }}
if ({{#lambda.paste}}{{/lambda.paste}}RawValue != null){{! we cant use name here because enumVar also has a name property, so use the paste lambda instead }}
writer.WriteString("{{baseName}}", {{#lambda.camelcase_sanitize_param}}{{nameInPascalCase}}{{/lambda.camelcase_sanitize_param}}RawValue);
else
writer.WriteNull("{{baseName}}");
Expand All @@ -552,10 +552,10 @@
{{#enumVars}}
{{#-first}}
{{^isNumeric}}
writer.WriteString("{{baseName}}", {{#lambda.pasteLine}}{{/lambda.pasteLine}}RawValue);
writer.WriteString("{{baseName}}", {{#lambda.paste}}{{/lambda.paste}}RawValue);
{{/isNumeric}}
{{#isNumeric}}
writer.WriteNumber("{{baseName}}", {{#lambda.camelcase_sanitize_param}}{{#lambda.pasteLine}}{{/lambda.pasteLine}}{{/lambda.camelcase_sanitize_param}}RawValue);
writer.WriteNumber("{{baseName}}", {{#lambda.camelcase_sanitize_param}}{{#lambda.paste}}{{/lambda.paste}}{{/lambda.camelcase_sanitize_param}}RawValue);
{{/isNumeric}}
{{/-first}}
{{/enumVars}}
Expand All @@ -568,16 +568,16 @@
{{#isNullable}}
if ({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}Option{{nrt!}}.Value != null)
{
var {{#lambda.pasteLine}}{{/lambda.pasteLine}}RawValue = {{{datatypeWithEnum}}}ValueConverter.ToJsonValue({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}Option.Value{{nrt!}}.Value);
writer.{{#lambda.first}}{{#allowableValues}}{{#enumVars}}{{^isNumeric}}WriteString {{/isNumeric}}{{#isNumeric}}WriteNumber {{/isNumeric}}{{/enumVars}}{{/allowableValues}}{{/lambda.first}}("{{baseName}}", {{#lambda.pasteLine}}{{/lambda.pasteLine}}RawValue);
var {{#lambda.paste}}{{/lambda.paste}}RawValue = {{{datatypeWithEnum}}}ValueConverter.ToJsonValue({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}Option.Value{{nrt!}}.Value);
writer.{{#lambda.first}}{{#allowableValues}}{{#enumVars}}{{^isNumeric}}WriteString {{/isNumeric}}{{#isNumeric}}WriteNumber {{/isNumeric}}{{/enumVars}}{{/allowableValues}}{{/lambda.first}}("{{baseName}}", {{#lambda.paste}}{{/lambda.paste}}RawValue);
}
else
writer.WriteNull("{{baseName}}");
{{/isNullable}}
{{^isNullable}}
{
var {{#lambda.pasteLine}}{{/lambda.pasteLine}}RawValue = {{{datatypeWithEnum}}}ValueConverter.ToJsonValue({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}{{nrt!}}.Value);
writer.{{#lambda.first}}{{#allowableValues}}{{#enumVars}}{{^isNumeric}}WriteString {{/isNumeric}}{{#isNumeric}}WriteNumber {{/isNumeric}}{{/enumVars}}{{/allowableValues}}{{/lambda.first}}("{{baseName}}", {{#lambda.pasteLine}}{{/lambda.pasteLine}}RawValue);
var {{#lambda.paste}}{{/lambda.paste}}RawValue = {{{datatypeWithEnum}}}ValueConverter.ToJsonValue({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}{{nrt!}}.Value);
writer.{{#lambda.first}}{{#allowableValues}}{{#enumVars}}{{^isNumeric}}WriteString {{/isNumeric}}{{#isNumeric}}WriteNumber {{/isNumeric}}{{/enumVars}}{{/allowableValues}}{{/lambda.first}}("{{baseName}}", {{#lambda.paste}}{{/lambda.paste}}RawValue);
}
{{/isNullable}}
{{/required}}
Expand All @@ -586,9 +586,9 @@
{{/isMap}}
{{/isEnum}}
{{#isUuid}}
{{#lambda.copy}}
{{#lambda.copyText}}
writer.WriteString("{{baseName}}", {{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}{{^required}}Option.Value{{#vendorExtensions.x-is-value-type}}{{nrt!}}.Value{{/vendorExtensions.x-is-value-type}}{{/required}}{{#required}}{{#isNullable}}.Value{{/isNullable}}{{/required}});
{{/lambda.copy}}
{{/lambda.copyText}}
{{#lambda.indent3}}
{{>WriteProperty}}
{{/lambda.indent3}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ namespace {{packageName}}.{{clientPackage}}
}
else
{
{{#lambda.indent1}}{{#lambda.pasteLine}}{{/lambda.pasteLine}}{{/lambda.indent1}}
{{#lambda.indentAll1}}
{{#lambda.paste}}
{{/lambda.paste}}
{{/lambda.indentAll1}}
}
{{/hasApiKeyMethods}}
{{^hasApiKeyMethods}}
{{#lambda.pasteLine}}{{/lambda.pasteLine}}
{{#lambda.paste}}
{{/lambda.paste}}
{{/hasApiKeyMethods}}

foreach(Channel<TTokenBase> tokens in AvailableTokens.Values)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// {{{name}}} ({{{dataType}}}) pattern
Regex regex{{{name}}} = new Regex(@"{{{vendorExtensions.x-regex}}}"{{#vendorExtensions.x-modifiers}}{{#-first}}, {{/-first}}RegexOptions.{{{.}}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}});
{{#lambda.copy}}this.{{{name}}}{{#useGenericHost}}{{^required}}Option.Value{{/required}}{{/useGenericHost}} != null && {{/lambda.copy}}
if ({{#lambda.first}}{{^required}}{{#lambda.pasteLine}}{{/lambda.pasteLine}} {{/required}}{{#isNullable}}{{#lambda.pasteLine}}{{/lambda.pasteLine}}{{/isNullable}}{{/lambda.first}}!regex{{{name}}}.Match(this.{{{name}}}{{#useGenericHost}}{{^required}}Option.Value{{/required}}{{/useGenericHost}}{{#isUuid}}.ToString(){{#lambda.first}}{{^required}}{{nrt!}} {{/required}}{{#isNullable}}! {{/isNullable}}{{/lambda.first}}{{/isUuid}}).Success)
if ({{#lambda.first}}{{^required}}{{#lambda.paste}}{{/lambda.paste}} {{/required}}{{#isNullable}}{{#lambda.paste}}{{/lambda.paste}}{{/isNullable}}{{/lambda.first}}!regex{{{name}}}.Match(this.{{{name}}}{{#useGenericHost}}{{^required}}Option.Value{{/required}}{{/useGenericHost}}{{#isUuid}}.ToString(){{#lambda.first}}{{^required}}{{nrt!}} {{/required}}{{#isNullable}}! {{/isNullable}}{{/lambda.first}}{{/isUuid}}).Success)
{
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for {{{name}}}, must match a pattern of " + regex{{{name}}}, new [] { "{{{name}}}" });
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{{#isNullable}}
if ({{#lambda.camelcase_sanitize_param}}{{classname}}{{/lambda.camelcase_sanitize_param}}.{{name}}{{^required}}Option.Value{{/required}} != null)
{{#lambda.pasteLine}}{{/lambda.pasteLine}}
{{#lambda.paste}}{{/lambda.paste}}
else
writer.WriteNull("{{baseName}}");
{{/isNullable}}
{{^isNullable}}
{{#lambda.pasteLine}}{{/lambda.pasteLine}}
{{#lambda.paste}}
{{/lambda.paste}}
{{/isNullable}}
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,17 @@
{{/readOnlyVars}}
{{#readOnlyVars}}
{{#lambda.copy}}

if ({{name}} != null)
hashCode = (hashCode * 59) + {{name}}.GetHashCode();

{{/lambda.copy}}
{{#isNullable}}
{{#lambda.pasteOnce}}{{/lambda.pasteOnce}}
{{#lambda.pasteOnce}}
{{/lambda.pasteOnce}}
{{/isNullable}}
{{^required}}
{{#lambda.pasteOnce}}{{/lambda.pasteOnce}}
{{#lambda.pasteOnce}}
{{/lambda.pasteOnce}}
{{/required}}
{{/readOnlyVars}}
{{#isAdditionalPropertiesTrue}}
Expand Down
Loading

0 comments on commit f98073d

Please sign in to comment.