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

Add DataMapping annotation and new configuration #34

Merged
merged 7 commits into from
Jul 24, 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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.version>3.12.0</quarkus.version>
<assertj.version>3.26.0</assertj.version>
</properties>

<dependencyManagement>
Expand Down
6 changes: 6 additions & 0 deletions roq-data/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package io.quarkiverse.roq.data.deployment;

import java.io.IOException;
import java.util.List;

public interface DataConverter {

Object convert(String content);
Object convert(byte[] content) throws IOException;

<T> T convertToType(byte[] content, Class<T> clazz) throws IOException;

<T> List<T> convertToTypedList(byte[] content, Class<T> clazz) throws IOException;
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Objects;

import io.quarkiverse.roq.data.runtime.annotations.DataMapping;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
Expand All @@ -19,10 +20,17 @@ public interface RoqDataConfig {
@WithDefault(DEFAULT_LOCATION)
String dir();

/**
* Whether to enforce the use of a bean for each data file.
* <br>
* With this option enabled, when a record is annotated with {@link DataMapping}, a bean will be created and populated
* with the data from the file.
*/
@WithDefault("false")
boolean enforceBean();

static boolean isEqual(RoqDataConfig q1, RoqDataConfig q2) {
if (!Objects.equals(q1.dir(), q2.dir())) {
return false;
}
return true;
melloware marked this conversation as resolved.
Show resolved Hide resolved
return Objects.equals(q1.dir(), q2.dir()) && Objects.equals(q1.enforceBean(), q2.enforceBean());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.quarkiverse.roq.data.deployment;

import java.lang.reflect.Constructor;
import java.util.List;

import io.quarkiverse.roq.data.deployment.items.DataMappingBuildItem;
import io.quarkiverse.roq.data.deployment.items.RoqDataBeanBuildItem;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;

public class RoqDataConverterProcessor {

@BuildStep
void convertDataMapping(List<DataMappingBuildItem> mappings,
BuildProducer<RoqDataBeanBuildItem> beans) {

for (DataMappingBuildItem mapping : mappings) {
if (mapping.isParentType()) {
Class<?> parentClass;
try {
parentClass = Class.forName(mapping.getParentType().toString(), false,
Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
throw new RuntimeException("Class %s not found".formatted(mapping.getParentType().toString()), e);
}

final Constructor<?> constructor;
try {
constructor = parentClass.getConstructor(List.class);
} catch (NoSuchMethodException e) {
throw new RuntimeException(
"@DataMapping for list should declare a constructor with a List<T> as unique parameter.", e);
}

try {
final Class<?> itemClass = Class.forName(mapping.getClassName().toString(), false,
Thread.currentThread().getContextClassLoader());
final List<?> list = mapping.getConverter().convertToTypedList(mapping.getContent(), itemClass);
final Object data = constructor.newInstance(list);
beans.produce(new RoqDataBeanBuildItem(mapping.getName(), parentClass, data, mapping.isRecord()));
} catch (Exception e) {
throw new RuntimeException("Unable to convert data to a List<%s>.".formatted(mapping.getClassName()), e);
}
} else {
Class<?> beanClass = null;
try {
beanClass = Class.forName(mapping.getClassName().toString(), false,
Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
throw new RuntimeException("Class %s not found".formatted(mapping.getClassName().toString()), e);
}
try {
final Object data = mapping.getConverter().convertToType(mapping.getContent(), beanClass);
beans.produce(new RoqDataBeanBuildItem(mapping.getName(), beanClass, data, mapping.isRecord()));
} catch (Exception e) {
throw new RuntimeException("Unable to convert data to a %s.".formatted(mapping.getClassName()), e);
}
}
}

}
}
Loading