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

[java-generator] Overridable package name #4694

Merged
merged 1 commit into from
Dec 24, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Fix #4637: all pod operations that require a ready / succeeded pod may use withReadyWaitTimeout, which supersedes withLogWaitTimeout.
* Fix #4633: provided inline access to all RunConfig builder methods via run().withNewRunConfig()
* Fix #4670: the initial informer listing will use a resourceVersion of 0 to utilize the watch cache if possible. This means that the initial cache state when the informer is returned, or the start future is completed, may not be as fresh as the previous behavior which forced the latest version. It will of course become more consistent as the watch will already have been established.
* Fix #4694: [java-generator] Option to override the package name of the generated code.

#### Dependency Upgrade

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import picocli.CommandLine.Option;

import java.io.File;
import java.util.Map;

@Command(name = "java-gen", mixinStandardHelpOptions = true, helpCommand = true, versionProvider = KubernetesClientVersionProvider.class)
public class GenerateJavaSources implements Runnable {
Expand Down Expand Up @@ -60,6 +61,10 @@ public class GenerateJavaSources implements Runnable {
"--skip-generated-annotations" }, description = "Add extra lombok and sundrio annotation to the generated classes", required = false, hidden = true)
Boolean skipGeneratedAnnotations = null;

@Option(names = { "-package-overrides",
"--package-overrides" }, description = "Apply the overrides to the package names", required = false)
Map<String, String> packageOverrides = null;

@Override
public void run() {
final Config.Prefix pSt = (prefixStrategy != null) ? Config.Prefix.valueOf(prefixStrategy) : null;
Expand All @@ -73,7 +78,8 @@ public void run() {
alwaysPreserveUnkownFields,
addExtraAnnotations,
structure,
generatedAnnotations);
generatedAnnotations,
packageOverrides);
final JavaGenerator runner = new FileJavaGenerator(config, source);
runner.run(target);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public List<WritableCRCompilationUnit> generate(CustomResourceDefinition crd, St
.map(p -> p + "." + version)
.orElse(version);

if (config.getPackageOverrides().containsKey(pkg)) {
pkg = config.getPackageOverrides().get(pkg);
}

AbstractJSONSchema2Pojo specGenerator = null;

String prefix = crName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import lombok.Builder;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

@Builder(toBuilder = true)
@NoArgsConstructor
public class Config {
Expand Down Expand Up @@ -45,6 +48,7 @@ public enum Suffix {
private static final boolean DEFAULT_ADD_EXTRA_ANNOTATIONS = false;
private static final CodeStructure DEFAULT_CODE_STRUCTURE = CodeStructure.PACKAGE_NESTED;
private static final boolean DEFAULT_ADD_GENERATED_ANNOTATIONS = true;
private static final Map<String, String> DEFAULT_PACKAGE_OVERRIDES = new HashMap<>();

private Boolean uppercaseEnums = DEFAULT_UPPERCASE_ENUM;
private Prefix prefixStrategy = DEFAULT_PREFIX_STRATEGY;
Expand All @@ -53,6 +57,7 @@ public enum Suffix {
private Boolean objectExtraAnnotations = DEFAULT_ADD_EXTRA_ANNOTATIONS;
private CodeStructure structure = DEFAULT_CODE_STRUCTURE;
private Boolean generatedAnnotations = DEFAULT_ADD_GENERATED_ANNOTATIONS;
private Map<String, String> packageOverrides = DEFAULT_PACKAGE_OVERRIDES;

public Config(
Boolean uppercaseEnums,
Expand All @@ -61,7 +66,8 @@ public Config(
Boolean alwaysPreserveUnknownFields,
Boolean objectExtraAnnotations,
CodeStructure structure,
Boolean generatedAnnotations) {
Boolean generatedAnnotations,
Map<String, String> packageOverrides) {
if (uppercaseEnums != null) {
this.uppercaseEnums = uppercaseEnums;
}
Expand All @@ -83,6 +89,9 @@ public Config(
if (generatedAnnotations != null) {
this.generatedAnnotations = generatedAnnotations;
}
if (packageOverrides != null) {
this.packageOverrides = packageOverrides;
}
}

public boolean isUppercaseEnums() {
Expand Down Expand Up @@ -118,4 +127,10 @@ public boolean isGeneratedAnnotations() {
? DEFAULT_ADD_GENERATED_ANNOTATIONS
: generatedAnnotations;
}

public Map<String, String> getPackageOverrides() {
return (packageOverrides == null)
? DEFAULT_PACKAGE_OVERRIDES
: packageOverrides;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.params.provider.MethodSource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Stream;

Expand All @@ -38,7 +39,7 @@ private static Stream<Arguments> getCRDGenerationInputData() {
return Stream.of(
Arguments.of("testCrontabCrd", "crontab-crd.yml", "CronTab", "CrontabJavaCr", new Config()),
Arguments.of("testCrontabExtraAnnotationsCrd", "crontab-crd.yml", "CronTab", "CrontabJavaExtraAnnotationsCr",
new Config(null, null, null, null, Boolean.TRUE, null, true)),
new Config(null, null, null, null, Boolean.TRUE, null, true, new HashMap<>())),
Arguments.of("testKeycloakCrd", "keycloak-crd.yml", "Keycloak", "KeycloakJavaCr", new Config()),
Arguments.of("testJokeCrd", "jokerequests-crd.yml", "JokeRequest", "JokeRequestJavaCr", new Config()),
Arguments.of("testAkkaMicroservicesCrd", "akka-microservices-crd.yml", "AkkaMicroservice", "AkkaMicroserviceJavaCr",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@

import org.junit.jupiter.api.Test;

import java.util.HashMap;

import static org.assertj.core.api.Assertions.assertThat;

class ConfigTest {

@Test
void defaultValuesWithAllArgsConstructor() {
final Config result = new Config(null, null, null, null, null, null, null);
final Config result = new Config(null, null, null, null, null, null, null, null);
assertThat(result)
.returns(true, Config::isUppercaseEnums)
.returns(Config.Prefix.TOP_LEVEL, Config::getPrefixStrategy)
.returns(Config.Suffix.TOP_LEVEL, Config::getSuffixStrategy)
.returns(false, Config::isAlwaysPreserveUnknownFields)
.returns(false, Config::isObjectExtraAnnotations)
.returns(Config.CodeStructure.PACKAGE_NESTED, Config::getCodeStructure)
.returns(true, Config::isGeneratedAnnotations);
.returns(true, Config::isGeneratedAnnotations)
.returns(new HashMap<>(), Config::getPackageOverrides);
}

@Test
Expand All @@ -44,7 +47,8 @@ void defaultValuesWithNoArgsConstructor() {
.returns(false, Config::isAlwaysPreserveUnknownFields)
.returns(false, Config::isObjectExtraAnnotations)
.returns(Config.CodeStructure.PACKAGE_NESTED, Config::getCodeStructure)
.returns(true, Config::isGeneratedAnnotations);
.returns(true, Config::isGeneratedAnnotations)
.returns(new HashMap<>(), Config::getPackageOverrides);
}

@Test
Expand All @@ -57,6 +61,7 @@ void defaultValuesWithBuilder() {
.returns(false, Config::isAlwaysPreserveUnknownFields)
.returns(false, Config::isObjectExtraAnnotations)
.returns(Config.CodeStructure.PACKAGE_NESTED, Config::getCodeStructure)
.returns(true, Config::isGeneratedAnnotations);
.returns(true, Config::isGeneratedAnnotations)
.returns(new HashMap<>(), Config::getPackageOverrides);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ void testCR() {
// Assert
assertEquals(1, res.getTopLevelClasses().size());
assertEquals("t", res.getTopLevelClasses().get(0).getName());
assertEquals("v1alpha1",
res.getTopLevelClasses().get(0).getCompilationUnit().getPackageDeclaration().get().getNameAsString());
}

@Test
Expand Down Expand Up @@ -255,7 +257,7 @@ void testEmptyObject() {
@Test
void testEmptyObjectWithSuffix() {
// Arrange
Config config = new Config(null, null, Config.Suffix.ALWAYS, null, null, null, true);
Config config = new Config(null, null, Config.Suffix.ALWAYS, null, null, null, true, new HashMap<>());
JObject obj = new JObject(
"v1alpha1",
"t",
Expand Down Expand Up @@ -382,7 +384,7 @@ void testObjectWithAndWithoutGeneratedAnnotation() {
null,
Boolean.FALSE,
null);
Config config = new Config(null, null, Config.Suffix.ALWAYS, null, null, null, false);
Config config = new Config(null, null, Config.Suffix.ALWAYS, null, null, null, false, new HashMap<>());
JObject obj2 = new JObject(
"v1alpha1",
"t",
Expand Down Expand Up @@ -458,7 +460,7 @@ void testNotUppercaseEnum() {
JEnum enu = new JEnum(
"t",
enumValues,
new Config(false, null, null, null, null, null, true),
new Config(false, null, null, null, null, null, true, new HashMap<>()),
null,
Boolean.FALSE,
null);
Expand Down Expand Up @@ -578,7 +580,7 @@ void testObjectOfObjects() {
@Test
void testObjectOfObjectsWithTopLevelPrefix() {
// Arrange
Config config = new Config(null, Config.Prefix.TOP_LEVEL, null, null, null, null, true);
Config config = new Config(null, Config.Prefix.TOP_LEVEL, null, null, null, null, true, new HashMap<>());
Map<String, JSONSchemaProps> props = new HashMap<>();
JSONSchemaProps newObj = new JSONSchemaProps();
newObj.setType("object");
Expand Down Expand Up @@ -608,7 +610,7 @@ void testObjectOfObjectsWithTopLevelPrefix() {
@Test
void testObjectOfObjectsWithAlwaysPrefix() {
// Arrange
Config config = new Config(null, Config.Prefix.ALWAYS, null, null, null, null, true);
Config config = new Config(null, Config.Prefix.ALWAYS, null, null, null, null, true, new HashMap<>());
Map<String, JSONSchemaProps> props = new HashMap<>();
JSONSchemaProps newObj = new JSONSchemaProps();
newObj.setType("object");
Expand Down
3 changes: 3 additions & 0 deletions java-generator/it/src/it/crd-names/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
<configuration>
<source>src/test/resources/artemis-crd.yml</source>
<generatedAnnotations>false</generatedAnnotations>
<packageOverrides>
<io.amq.broker.v1beta1>com.fabric8.test</io.amq.broker.v1beta1>
</packageOverrides>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package io.fabric8.it.names;

import io.amq.broker.v1beta1.ActiveMQArtemis;
import com.fabric8.test.ActiveMQArtemis;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class JavaGeneratorMojo extends AbstractMojo {
Expand Down Expand Up @@ -114,6 +115,13 @@ public class JavaGeneratorMojo extends AbstractMojo {
@Parameter(property = "fabric8.java-generator.generated-annotations", required = false)
Boolean generatedAnnotations = null;

/**
* Package names to be substituted
*
*/
@Parameter(property = "fabric8.java-generator.package-overrides", required = false)
Map<String, String> packageOverrides = null;

@Override
public void execute() throws MojoExecutionException {
final Config config = Config.builder()
Expand All @@ -124,6 +132,7 @@ public void execute() throws MojoExecutionException {
.objectExtraAnnotations(extraAnnotations)
.structure(codeStructure)
.generatedAnnotations(generatedAnnotations)
.packageOverrides(packageOverrides)
.build();

boolean executed = false;
Expand Down