-
-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8610 from jhipster/protobuf
New module: protobuf support
- Loading branch information
Showing
21 changed files
with
528 additions
and
14 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
...pster/lite/generator/server/javatool/protobuf/application/ProtobufApplicationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package tech.jhipster.lite.generator.server.javatool.protobuf.application; | ||
|
||
import org.springframework.stereotype.Service; | ||
import tech.jhipster.lite.generator.server.javatool.protobuf.domain.ProtobufModuleFactory; | ||
import tech.jhipster.lite.module.domain.JHipsterModule; | ||
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; | ||
|
||
@Service | ||
public class ProtobufApplicationService { | ||
|
||
private final ProtobufModuleFactory factory; | ||
|
||
public ProtobufApplicationService() { | ||
factory = new ProtobufModuleFactory(); | ||
} | ||
|
||
public JHipsterModule buildProtobufModule(JHipsterModuleProperties properties) { | ||
return factory.buildProtobufModule(properties); | ||
} | ||
|
||
public JHipsterModule buildProtobufBackwardsCompatibilityCheckModule(JHipsterModuleProperties properties) { | ||
return factory.buildProtobufBackwardsCompatibilityCheckModule(properties); | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
...a/tech/jhipster/lite/generator/server/javatool/protobuf/domain/ProtobufModuleFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package tech.jhipster.lite.generator.server.javatool.protobuf.domain; | ||
|
||
import static tech.jhipster.lite.module.domain.JHipsterModule.*; | ||
|
||
import tech.jhipster.lite.module.domain.JHipsterModule; | ||
import tech.jhipster.lite.module.domain.file.JHipsterDestination; | ||
import tech.jhipster.lite.module.domain.file.JHipsterSource; | ||
import tech.jhipster.lite.module.domain.gradleplugin.GradleCommunityPlugin; | ||
import tech.jhipster.lite.module.domain.javabuild.GroupId; | ||
import tech.jhipster.lite.module.domain.javabuild.VersionSlug; | ||
import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin; | ||
import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin.MavenPluginOptionalBuilder; | ||
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; | ||
import tech.jhipster.lite.shared.error.domain.Assert; | ||
|
||
public class ProtobufModuleFactory { | ||
|
||
private static final JHipsterSource SOURCE = from("server/javatool/protobuf"); | ||
private static final JHipsterSource MAIN_SOURCE = SOURCE.append("main"); | ||
private static final JHipsterSource TEST_SOURCE = SOURCE.append("test"); | ||
|
||
private static final String PROTOBUF_PACKAGE = "shared/protobuf"; | ||
private static final VersionSlug PROTOBUF_VERSION_SLUG = versionSlug("protobuf"); | ||
private static final GroupId PROTOBUF_GROUPID = groupId("com.google.protobuf"); | ||
|
||
public JHipsterModule buildProtobufModule(JHipsterModuleProperties properties) { | ||
Assert.notNull("properties", properties); | ||
|
||
JHipsterDestination mainDestination = toSrcMainJava().append(properties.packagePath()).append(PROTOBUF_PACKAGE); | ||
JHipsterDestination testDestination = toSrcTestJava().append(properties.packagePath()).append(PROTOBUF_PACKAGE); | ||
|
||
//@formatter:off | ||
return moduleBuilder(properties) | ||
.files() | ||
.add(MAIN_SOURCE.template("package-info.java"), mainDestination.append("package-info.java")) | ||
.add(MAIN_SOURCE.template("ProtobufDatesReader.java"), mainDestination.append("infrastructure/primary/ProtobufDatesReader.java")) | ||
.add(MAIN_SOURCE.template("ProtobufDatesWriter.java"), mainDestination.append("infrastructure/secondary/ProtobufDatesWriter.java")) | ||
.add(MAIN_SOURCE.append(".gitkeep"), to("src/main/proto/.gitkeep")) | ||
.add( | ||
TEST_SOURCE.template("ProtobufDatesReaderTest.java"), | ||
testDestination.append("infrastructure/primary/ProtobufDatesReaderTest.java") | ||
) | ||
.add( | ||
TEST_SOURCE.template("ProtobufDatesWriterTest.java"), | ||
testDestination.append("infrastructure/secondary/ProtobufDatesWriterTest.java") | ||
) | ||
.and() | ||
.javaDependencies() | ||
.addDependency(PROTOBUF_GROUPID, artifactId("protobuf-java"), PROTOBUF_VERSION_SLUG) | ||
.addTestDependency(PROTOBUF_GROUPID, artifactId("protobuf-java-util"), PROTOBUF_VERSION_SLUG) | ||
.and() | ||
.mavenPlugins() | ||
.pluginManagement(protobufMavenPluginManagement()) | ||
.plugin(protobufMavenPluginBuilder().build()) | ||
.and() | ||
.mavenBuildExtensions() | ||
.addExtension(groupId("kr.motd.maven"), artifactId("os-maven-plugin"), versionSlug("os-maven-plugin")) | ||
.and() | ||
.gradlePlugins() | ||
.plugin(protobufGradlePlugin()) | ||
.and() | ||
.build(); | ||
//@formatter:on | ||
} | ||
|
||
private static GradleCommunityPlugin protobufGradlePlugin() { | ||
return gradleCommunityPlugin() | ||
.id("com.google.protobuf") | ||
.pluginSlug("protobuf") | ||
.versionSlug("protobuf-plugin") | ||
.configuration( | ||
""" | ||
protobuf { | ||
protoc { | ||
artifact = "com.google.protobuf:protoc:${libs.versions.protobuf.asProvider().get()}" | ||
} | ||
} | ||
""" | ||
) | ||
.build(); | ||
} | ||
|
||
private MavenPlugin protobufMavenPluginManagement() { | ||
return protobufMavenPluginBuilder() | ||
.versionSlug("protobuf-maven-plugin") | ||
.configuration( | ||
""" | ||
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact> | ||
""" | ||
) | ||
.addExecution(pluginExecution().goals("compile")) | ||
.build(); | ||
} | ||
|
||
private MavenPluginOptionalBuilder protobufMavenPluginBuilder() { | ||
return mavenPlugin().groupId("org.xolstice.maven.plugins").artifactId("protobuf-maven-plugin"); | ||
} | ||
|
||
public JHipsterModule buildProtobufBackwardsCompatibilityCheckModule(JHipsterModuleProperties properties) { | ||
//@formatter:off | ||
return moduleBuilder(properties) | ||
.mavenPlugins() | ||
.pluginManagement(protoBackwardsCompatibilityMavenPluginManagement()) | ||
.plugin(protoBackwardsCompatibilityMavenPluginBuilder().build()) | ||
.and() | ||
.build(); | ||
//@formatter:on | ||
} | ||
|
||
private MavenPlugin protoBackwardsCompatibilityMavenPluginManagement() { | ||
return protoBackwardsCompatibilityMavenPluginBuilder() | ||
.versionSlug("proto-backwards-compatibility") | ||
.addExecution(pluginExecution().goals("backwards-compatibility-check")) | ||
.build(); | ||
} | ||
|
||
private MavenPluginOptionalBuilder protoBackwardsCompatibilityMavenPluginBuilder() { | ||
return mavenPlugin().groupId("com.salesforce.servicelibs").artifactId("proto-backwards-compatibility"); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...enerator/server/javatool/protobuf/infrastructure/primary/ProtobufModuleConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package tech.jhipster.lite.generator.server.javatool.protobuf.infrastructure.primary; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import tech.jhipster.lite.generator.server.javatool.protobuf.application.ProtobufApplicationService; | ||
import tech.jhipster.lite.generator.slug.domain.JHLiteFeatureSlug; | ||
import tech.jhipster.lite.generator.slug.domain.JHLiteModuleSlug; | ||
import tech.jhipster.lite.module.domain.resource.JHipsterModuleOrganization; | ||
import tech.jhipster.lite.module.domain.resource.JHipsterModulePropertiesDefinition; | ||
import tech.jhipster.lite.module.domain.resource.JHipsterModuleResource; | ||
|
||
@Configuration | ||
class ProtobufModuleConfiguration { | ||
|
||
@Bean | ||
JHipsterModuleResource protobufModule(ProtobufApplicationService protobuf) { | ||
return JHipsterModuleResource | ||
.builder() | ||
.slug(JHLiteModuleSlug.PROTOBUF) | ||
.propertiesDefinition(JHipsterModulePropertiesDefinition.builder().addBasePackage().addIndentation().build()) | ||
.apiDoc("Java", "Add protobuf support") | ||
.organization(JHipsterModuleOrganization.builder().addDependency(JHLiteFeatureSlug.JAVA_BUILD_TOOL).build()) | ||
.tags("server", "protobuf") | ||
.factory(protobuf::buildProtobufModule); | ||
} | ||
|
||
@Bean | ||
JHipsterModuleResource protobufBackwardsCompatibilityCheckModule(ProtobufApplicationService protobuf) { | ||
return JHipsterModuleResource | ||
.builder() | ||
.slug(JHLiteModuleSlug.PROTOBUF_BACKWARDS_COMPATIBILITY_CHECK) | ||
.propertiesDefinition(JHipsterModulePropertiesDefinition.builder().addBasePackage().addIndentation().build()) | ||
.apiDoc("Java", "Add protobuf backwards compatibility check") | ||
.organization( | ||
JHipsterModuleOrganization.builder().addDependency(JHLiteModuleSlug.PROTOBUF).addDependency(JHLiteModuleSlug.MAVEN_JAVA).build() | ||
) | ||
.tags("server", "protobuf") | ||
.factory(protobuf::buildProtobufBackwardsCompatibilityCheckModule); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
17 changes: 17 additions & 0 deletions
17
src/main/resources/generator/server/javatool/protobuf/main/ProtobufDatesReader.java.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package {{packageName}}.shared.protobuf.infrastructure.primary; | ||
|
||
import com.google.protobuf.Timestamp; | ||
import java.time.Instant; | ||
|
||
public final class ProtobufDatesReader { | ||
private ProtobufDatesReader() {} | ||
|
||
public static Instant readInstant(Timestamp timestamp) { | ||
if (timestamp == null || timestamp.getSeconds() == 0) { | ||
return null; | ||
} | ||
|
||
return Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/resources/generator/server/javatool/protobuf/main/ProtobufDatesWriter.java.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package {{packageName}}.shared.protobuf.infrastructure.secondary; | ||
|
||
import com.google.protobuf.Timestamp; | ||
import java.time.Instant; | ||
|
||
public final class ProtobufDatesWriter { | ||
private ProtobufDatesWriter() {} | ||
|
||
public static Timestamp buildTimestamp(Instant instant) { | ||
if (instant == null) { | ||
return null; | ||
} | ||
|
||
return Timestamp.newBuilder().setSeconds(instant.getEpochSecond()).setNanos(instant.getNano()).build(); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/resources/generator/server/javatool/protobuf/main/package-info.java.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@{{packageName}}.SharedKernel | ||
package {{packageName}}.shared.protobuf; |
29 changes: 29 additions & 0 deletions
29
...n/resources/generator/server/javatool/protobuf/test/ProtobufDatesReaderTest.java.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package {{packageName}}.shared.protobuf.infrastructure.primary; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import com.google.protobuf.util.Timestamps; | ||
import {{packageName}}.UnitTest; | ||
import java.util.Date; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@UnitTest | ||
class ProtobufDatesReaderTest { | ||
@Test | ||
void shouldConvertNullTimestampToNull() { | ||
assertThat(ProtobufDatesReader.readInstant(null)).isNull(); | ||
} | ||
|
||
@Test | ||
void shouldConvertDefaultDateToNull() { | ||
assertThat(ProtobufDatesReader.readInstant(Timestamps.fromDate(new Date(0)))).isNull(); | ||
} | ||
|
||
@Test | ||
void shouldGetInstantFromTimestampFromDate() { | ||
Date date = new Date(); | ||
assertThat(ProtobufDatesReader.readInstant(Timestamps.fromDate(date))).isEqualTo(date.toInstant()); | ||
} | ||
} |
Oops, something went wrong.