io.swagger.codegen.mustache.CamelCaseLambda Maven / Gradle / Ivy
package io.swagger.codegen.mustache;
import com.google.common.base.CaseFormat;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.DefaultCodegen;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import java.io.IOException;
import java.io.Writer;
/**
* Converts text in a fragment to camelCase.
*
* Register:
*
* additionalProperties.put("camelcase", new CamelCaseLambda());
*
*
* Use:
*
* {{#camelcase}}{{name}}{{/camelcase}}
*
*/
public class CamelCaseLambda implements Mustache.Lambda {
private CodegenConfig generator = null;
private Boolean escapeParam = false;
public CamelCaseLambda() {
}
public CamelCaseLambda generator(final CodegenConfig generator) {
this.generator = generator;
return this;
}
public CamelCaseLambda escapeAsParamName(final Boolean escape) {
this.escapeParam = escape;
return this;
}
@Override
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
String text = DefaultCodegen.camelize(fragment.execute(), true);
if (generator != null) {
text = generator.sanitizeName(text);
if (generator.reservedWords().contains(text)) {
// Escaping must be done *after* camelize, because generators may escape using characters removed by camelize function.
text = generator.escapeReservedWord(text);
}
if (escapeParam) {
// NOTE: many generators call escapeReservedWord in toParamName, but we can't assume that's always the case.
// Here, we'll have to accept that we may be duplicating some work.
text = generator.toParamName(text);
}
}
writer.write(text);
}
}