Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure Prefix/Suffix for generated files #39

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ Optionally if you need to attach generated code to your source code add this:
- __typeMapFile__ type mapping file to use. Default is /type-mappings/java-type-map.properties. There is available dbml-type-map.properties file on same location, or you can define your own.
- __includeGenerationInfo__ should info about generation be included? Defaults to false
- __doNotGenerateTables__ list of tables to be not generated. Can be regexp.
- __prefix__ prefix to be added to generated classes. For CLASS_PER_TABLE strategy.
- __suffix__ suffix to be added to generated classes. For CLASS_PER_TABLE strategy.


You can customize generation template, by providing __templates__ list:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ private void _execute() {
getTargetFolder(),
getBaseDir(),
getSingleResultName(),
getGeneratorStrategy()),
getGeneratorStrategy(),
getPrefix(),
getSuffix()),
getExt(),
getDateImpl(),
getTypeMapFile(),
Expand Down Expand Up @@ -146,4 +148,8 @@ private static boolean isWindows() {
String getSingleResultName();

String getTypeMapFile();

String getPrefix();

String getSuffix();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class MockedTool implements AbstractTool {
private GeneratorStrategy generatorStrategy;
private String singleResultName;
private String typeMapFile;
private String prefix;
private String suffix;

@Override
public List<Item> getExtractionParameters() {
Expand Down Expand Up @@ -102,6 +104,16 @@ public String getTypeMapFile() {
return typeMapFile;
}

@Override
public String getPrefix() {
return prefix;
}

@Override
public String getSuffix() {
return suffix;
}

public void setExtractionParameters(List<Item> extractionParameters) {
this.extractionParameters = extractionParameters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public class ToolWithSettings implements AbstractTool {
private GeneratorStrategy generatorStrategy;
private String singleResultName;
private String typeMapFile;
private String prefix;
private String suffix;
}
19 changes: 19 additions & 0 deletions java-pojo-generator-mojo-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@
<!-- Default pojo.mustache is invoked -->
</configuration>
</execution>
<execution>
<id>testPrefixSuffix</id>
<goals>
<goal>generatePojo</goal>
</goals>
<configuration>
<includeGenerationInfo>true</includeGenerationInfo>
<extractionParameters>
<item>
<importFile>${basedir}/target/classes/TEST_SCHEMA.yml</importFile>
</item>
</extractionParameters>
<baseDir>${project.basedir}</baseDir>
<targetFolder>target/generated-sources</targetFolder>
<targetPackage>from_imported_metadata.testpkg</targetPackage>
<prefix>TestPref</prefix>
<suffix>TestSuff</suffix>
</configuration>
</execution>
<execution>
<id>pojoExecution</id>
<goals>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package testpkg;

import from_imported_metadata.testpkg.TestPrefTestTable1TestSuff;
import from_imported_metadata.testpkg.TestPrefTestTable2TestSuff;
import from_imported_metadata.testpkg.TestPrefTestTable3TestSuff;
import org.junit.jupiter.api.Test;

public class PrefixSuffixTest {
@Test
public void testPrefixSuffix() {
new TestPrefTestTable1TestSuff();
new TestPrefTestTable2TestSuff();
new TestPrefTestTable3TestSuff();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class PojoMojo extends AbstractMojo implements AbstractTool {

@Parameter private String typeMapFile;

@Parameter private String prefix;
@Parameter private String suffix;

@Override
public void execute() {
AbstractTool.super.execute();
Expand Down Expand Up @@ -125,4 +128,14 @@ public String getSingleResultName() {
public String getTypeMapFile() {
return typeMapFile;
}

@Override
public String getPrefix() {
return prefix;
}

@Override
public String getSuffix() {
return suffix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class GeneratorTarget {
private final String baseDir;
private final String singleResultName;
private final GeneratorStrategy generatorStrategy;
private final String prefix;
private final String suffix;

public Path getPath() {
String pkg = getTargetPackage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ private List<ClassAdapter> makeTableClassList() {
params.getGeneratorTarget().getTargetPackage(),
params.getDateImpl(),
params.getTypeMapFile(),
params.isIncludeGenerationInfo()))
params.isIncludeGenerationInfo(),
params.getGeneratorTarget().getPrefix(),
params.getGeneratorTarget().getSuffix()))
.collect(Collectors.toList());
setIsLast(tableClassList);
return tableClassList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.db2code.convert.JavaPropertyConverter;
import org.db2code.rawmodel.RawTable;

Expand All @@ -25,17 +26,22 @@ public class DefaultClassAdapter implements ClassAdapter {
return true;
}
};
private final String prefix;
private final String suffix;

public DefaultClassAdapter(
RawTable rawTable,
String targetPackage,
DateImpl dateImpl,
String typeMapFile,
boolean includeGenerationInfo) {
boolean includeGenerationInfo,
String prefix,
String suffix) {
this.rawTable = rawTable;
this.targetPackage = targetPackage;
this.includeGenerationInfo = includeGenerationInfo;

this.prefix = prefix;
this.suffix = suffix;
properties = initProperties(rawTable, dateImpl, typeMapFile);
}

Expand All @@ -58,7 +64,15 @@ private Collection<PropertyAdapter> initProperties(

@Override
public String getClassName() {
return JavaPropertyConverter.camelCaseFromSnakeCaseInitCap(rawTable.getTableName());
String className =
JavaPropertyConverter.camelCaseFromSnakeCaseInitCap(rawTable.getTableName());
if (StringUtils.isNotBlank(prefix)) {
className = prefix + className;
}
if (StringUtils.isNotBlank(suffix)) {
className = className + suffix;
}
return className;
}

public RawTable getRawTable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ public ProcedureAdapter(RawProcedure rawProcedure, ExecutorParams params) {

@Override
public String getClassName() {
return JavaPropertyConverter.camelCaseFromSnakeCaseInitCap(rawProcedure.getProcedureName());
String classname =
JavaPropertyConverter.camelCaseFromSnakeCaseInitCap(
rawProcedure.getProcedureName());
if (params.getGeneratorTarget().getPrefix() != null) {
classname = params.getGeneratorTarget().getPrefix() + classname;
}
if (params.getGeneratorTarget().getSuffix() != null) {
classname = classname + params.getGeneratorTarget().getSuffix();
}
return classname;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void test() throws IOException {
false)),
Arrays.asList("pojo.mustache"),
null,
new GeneratorTarget(TESTPKG, TARGET_FOLDER, dir, null, null),
new GeneratorTarget(TESTPKG, TARGET_FOLDER, dir, null, null, null, null),
null,
DateImpl.UTIL_DATE,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void testUniqueProperties() {
col2.setColumnName("test_col_");
rawTable.setColumns(asList(col1, col2));
DefaultClassAdapter classAdapter =
new DefaultClassAdapter(rawTable, null, null, null, false);
new DefaultClassAdapter(rawTable, null, null, null, false, null, null);
Collection<PropertyAdapter> properties = classAdapter.getProperties();
Assertions.assertTrue(properties.size() == 2);
Iterator<PropertyAdapter> iterator = properties.iterator();
Expand Down
Loading