Skip to content

Commit

Permalink
Spring integration example
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel committed Oct 22, 2024
1 parent 6508a09 commit 81fa835
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 50 deletions.
18 changes: 9 additions & 9 deletions examples/maven-spring-integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>community.flock.wirespec.integration</groupId>
<artifactId>spring-jvm</artifactId>
Expand All @@ -43,6 +42,13 @@
<groupId>community.flock.wirespec.plugin.maven</groupId>
<artifactId>wirespec-maven-plugin</artifactId>
<version>0.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>community.flock.wirespec.integration</groupId>
<artifactId>spring-jvm</artifactId>
<version>0.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>java</id>
Expand All @@ -58,20 +64,14 @@
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>community.flock.wirespec.integration</groupId>
<artifactId>spring-jvm</artifactId>
<version>0.0.0-SNAPSHOT</version>
</dependency>
</dependencies>

</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>17</release>
<release>21</release>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package community.flock.wirespec.examples.spring_boot_integration;

import community.flock.wirespec.integration.spring.configuration.WirespecConfiguration;
import community.flock.wirespec.integration.spring.java.configuration.WirespecConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.concurrent.CompletableFuture;

@RestController
class TodoController implements GetTodosEndpoint, GetTodoByIdEndpoint, CreateTodoEndpoint, UpdateTodoEndpoint, DeleteTodoEndpoint {
class TodoController implements GetTodosEndpoint.Handler, GetTodoByIdEndpoint.Handler, CreateTodoEndpoint.Handler, UpdateTodoEndpoint.Handler, DeleteTodoEndpoint.Handler {

private final TodoService service;

Expand All @@ -16,43 +16,43 @@ public TodoController(TodoService service) {
}

@Override
public CompletableFuture<CreateTodoEndpoint.Response<?>> createTodo(CreateTodoEndpoint.Request<?> request) {
public CompletableFuture<CreateTodoEndpoint.Response<?>> createTodo(CreateTodoEndpoint.Request request) {
var todoInput = switch (request){
case CreateTodoEndpoint.RequestApplicationJson req -> req.getContent().body();
case CreateTodoEndpoint.Request req -> req.getBody();
};
var todo = new Todo(
UUID.randomUUID().toString(),
todoInput.name(),
todoInput.done()
);
service.create(todo);
var res = new CreateTodoEndpoint.Response200ApplicationJson(todo);
var res = new CreateTodoEndpoint.Response200(todo);
return CompletableFuture.completedFuture(res);
}

@Override
public CompletableFuture<DeleteTodoEndpoint.Response<?>> deleteTodo(DeleteTodoEndpoint.Request<?> request) {
public CompletableFuture<DeleteTodoEndpoint.Response<?>> deleteTodo(DeleteTodoEndpoint.Request request) {
return null;
}

@Override
public CompletableFuture<GetTodoByIdEndpoint.Response<?>> getTodoById(GetTodoByIdEndpoint.Request<?> request) {
public CompletableFuture<GetTodoByIdEndpoint.Response<?>> getTodoById(GetTodoByIdEndpoint.Request request) {
var id = switch (request){
case GetTodoByIdEndpoint.RequestUnit req -> req.getPath();
case GetTodoByIdEndpoint.Request req -> req.getPath();
};
System.out.println(id);
var res = new GetTodoByIdEndpoint.Response200ApplicationJson(service.store.get(0));
var res = new GetTodoByIdEndpoint.Response200(service.store.get(0));
return CompletableFuture.completedFuture(res);
}

@Override
public CompletableFuture<UpdateTodoEndpoint.Response<?>> updateTodo(UpdateTodoEndpoint.Request<?> request) {
public CompletableFuture<UpdateTodoEndpoint.Response<?>> updateTodo(UpdateTodoEndpoint.Request request) {
return null;
}

@Override
public CompletableFuture<GetTodosEndpoint.Response<?>> getTodos(GetTodosEndpoint.Request<?> request) {
var res = new GetTodosEndpoint.Response200ApplicationJson(service.store);
public CompletableFuture<GetTodosEndpoint.Response<?>> getTodos(GetTodosEndpoint.Request request) {
var res = new GetTodosEndpoint.Response200(service.store);
return CompletableFuture.completedFuture(res);
}
}
1 change: 0 additions & 1 deletion fidle/bol/run.sh

This file was deleted.

1 change: 1 addition & 0 deletions scripts/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
dir="$(dirname -- "$0")"

./gradlew jvmTest &&
./gradlew src:integration:spring:publishToMavenLocal &&
./gradlew src:plugin:gradle:publishToMavenLocal &&
./gradlew src:plugin:maven:publishToMavenLocal &&
(cd "$dir"/../src/ide/vscode && npm i && npm run build) &&
Expand Down
2 changes: 1 addition & 1 deletion src/integration/spring/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ kotlin {
}
val jvmMain by getting {
dependencies {
compileOnly(project(":src:compiler:core"))
compileOnly(project(":src:integration:wirespec"))
compileOnly(project(":src:compiler:core"))
implementation(project(":src:integration:jackson"))
implementation("org.springframework.boot:spring-boot-starter-web:3.2.3")
implementation("org.jetbrains.kotlin:kotlin-reflect")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package community.flock.wirespec.integration.spring.java.emit

import community.flock.wirespec.compiler.core.emit.JavaEmitter
import community.flock.wirespec.compiler.core.emit.transformer.EndpointClass
import community.flock.wirespec.compiler.core.parse.Endpoint
import community.flock.wirespec.compiler.utils.Logger

class SpringJavaEmitter(packageName: String, logger: Logger) : JavaEmitter(packageName, logger) {
override fun emitHandleFunction(endpoint: Endpoint): String {
val path = "/${endpoint.path.joinToString("/") { it.emit() }}"
return when (endpoint.method) {
val annotation = when (endpoint.method) {
Endpoint.Method.GET -> "@org.springframework.web.bind.annotation.GetMapping(\"${path}\")\n"
Endpoint.Method.POST -> "@org.springframework.web.bind.annotation.PostMapping(\"${path}\")\n"
Endpoint.Method.PUT -> "@org.springframework.web.bind.annotation.PutMapping(\"${path}\")\n"
Endpoint.Method.DELETE -> "@org.springframework.web.bind.annotation.DeleteMapping(\"${path}\")\n"
else -> error("Method not implemented: ${endpoint.method}")
}
return """
|${annotation}
|${super.emitHandleFunction(endpoint)}
""".trimMargin()
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
package community.flock.wirespec.integration.spring.kotlin.configuration

import com.fasterxml.jackson.databind.ObjectMapper
import community.flock.wirespec.integration.jackson.java.WirespecModuleJava
import community.flock.wirespec.integration.jackson.kotlin.WirespecModuleKotlin
import community.flock.wirespec.integration.spring.kotlin.web.WirespecResponseBodyAdvice
import community.flock.wirespec.java.Wirespec
import community.flock.wirespec.kotlin.Wirespec
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import java.lang.reflect.Type
import kotlin.reflect.KType
import kotlin.reflect.javaType

@Configuration
@Import(WirespecResponseBodyAdvice::class, WirespecWebMvcConfiguration::class)
@OptIn(ExperimentalStdlibApi::class)
open class WirespecConfiguration {

@Bean
open fun wirespecSerialization(objectMapper: ObjectMapper) = object : Wirespec.Serialization<String> {

private val wirespecObjectMapper = objectMapper.copy().registerModule(WirespecModuleJava())
private val wirespecObjectMapper = objectMapper.copy().registerModule(WirespecModuleKotlin())

override fun <T : Any?> serialize(body: T, type: Type?): String {
override fun <T> serialize(body: T, kType: KType): String {
return wirespecObjectMapper.writeValueAsString(body)
}

override fun <T : Any?> deserialize(raw: String?, valueType: Type?): T {
val type = wirespecObjectMapper.constructType(valueType)
override fun <T> deserialize(raw: String, kType: KType): T {
val type = wirespecObjectMapper.constructType(kType.javaType)
val obj: T = wirespecObjectMapper.readValue(raw, type)
return obj
}


}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package community.flock.wirespec.integration.spring.kotlin.configuration

import community.flock.wirespec.integration.spring.java.web.WirespecMethodArgumentResolver
import community.flock.wirespec.java.Wirespec
import community.flock.wirespec.integration.spring.kotlin.web.WirespecMethodArgumentResolver
import community.flock.wirespec.kotlin.Wirespec
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Configuration
import org.springframework.web.method.support.HandlerMethodArgumentResolver
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package community.flock.wirespec.integration.spring.kotlin.web

import community.flock.wirespec.integration.spring.java.ExtensionFunctions.getStaticMethode
import community.flock.wirespec.integration.spring.java.ExtensionFunctions.invoke
import community.flock.wirespec.java.Wirespec
import jakarta.servlet.http.HttpServletRequest
import community.flock.wirespec.kotlin.Wirespec
import org.springframework.core.MethodParameter
import org.springframework.http.server.PathContainer
import org.springframework.web.bind.support.WebDataBinderFactory
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.method.support.ModelAndViewContainer
import org.springframework.web.util.pattern.PathPatternParser
import java.io.BufferedReader
import kotlin.reflect.full.companionObjectInstance

class WirespecMethodArgumentResolver(private val contentMapper: Wirespec.Serialization<String>) :
class WirespecMethodArgumentResolver(private val wirespecSerialization: Wirespec.Serialization<String>) :
HandlerMethodArgumentResolver {

override fun supportsParameter(parameter: MethodParameter): Boolean {
Expand Down
23 changes: 16 additions & 7 deletions src/plugin/maven/src/main/kotlin/CustomMojo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import community.flock.wirespec.plugin.Language
import community.flock.wirespec.plugin.PackageName
import community.flock.wirespec.plugin.compile
import community.flock.wirespec.plugin.toDirectory
import java.io.File
import org.apache.maven.plugins.annotations.LifecyclePhase
import org.apache.maven.plugins.annotations.Mojo
import org.apache.maven.plugins.annotations.Parameter
import org.apache.maven.plugins.annotations.ResolutionScope
import org.apache.maven.project.MavenProject
import java.io.File

@Mojo(
name = "custom",
Expand All @@ -43,13 +43,20 @@ class CustomMojo : BaseMojo() {
val ext = extension
val emitterPkg = PackageName(packageName)


val emitter = try {
getClassLoader(project)
.loadClass(emitterClass)
.getConstructor(String::class.java, Logger::class.java)
.newInstance(emitterPkg.value, logger) as Emitter
// .getConstructor(Logger::class.java, Boolean::class.java)
// .newInstance(logger, split) as Emitter
val clazz = getClassLoader(project).loadClass(emitterClass)
val constructor = clazz.constructors.first()
val args = constructor.parameters
.map {
when (it.type) {
String::class.java -> packageName
Boolean::class.java -> split
Logger::class.java -> logger
else -> error("Cannot map constructor parameter")
}
}
constructor.newInstance(*args.toTypedArray()) as Emitter
} catch (e: Exception) {
throw e.also { it.printStackTrace() }
}
Expand Down Expand Up @@ -78,13 +85,15 @@ class CustomMojo : BaseMojo() {

private fun getClassLoader(project: MavenProject): ClassLoader {
try {

val classpathElements = project.compileClasspathElements.apply {
add(project.build.outputDirectory)
add(project.build.testOutputDirectory)
}
val urls = classpathElements.indices
.map { File(classpathElements[it] as String).toURI().toURL() }
.toTypedArray()

return java.net.URLClassLoader(urls, javaClass.getClassLoader())
} catch (e: Exception) {
log.debug("Couldn't get the classloader.")
Expand Down

0 comments on commit 81fa835

Please sign in to comment.