Skip to content

Commit

Permalink
feat: enable constructor injection of NamedDynamoDbTable
Browse files Browse the repository at this point in the history
  • Loading branch information
scrocquesel committed Oct 27, 2024
1 parent 2a38846 commit 243bd83
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.ClassType;
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.MethodParameterInfo;
import org.jboss.jandex.ParameterizedType;
import org.jboss.jandex.Type;

import io.quarkiverse.amazon.common.deployment.RequireAmazonClientInjectionBuildItem;
import io.quarkiverse.amazon.common.runtime.ClientUtil;
Expand Down Expand Up @@ -72,31 +73,50 @@ void discoverDynamoDbTable(CombinedIndexBuildItem combinedIndexBuildItem,
IndexView index = combinedIndexBuildItem.getIndex();
Collection<AnnotationInstance> ais = index.getAnnotations(DotNames.DYNAMODB_NAMED_TABLE);
for (AnnotationInstance ano : ais) {
String tableName = ano.value().asString();
Type targetType = null;

if (ano.target().kind().equals(Kind.FIELD)) {
FieldInfo field = ano.target().asField();
String tableName = ano.value().asString();
DotName beanClassName = field.type().asParameterizedType().arguments().get(0).name();

ClassInfo beanClass = index.getClassByName(beanClassName);
if (beanClass.annotation(DotNames.DYNAMODB_ENHANCED_BEAN) == null
&& beanClass.annotation(DotNames.DYNAMODB_ENHANCED_IMMUTABLE) == null) {
throw new DeploymentException(String
.format("'%s' must be bean annotated with @DynamoDbBean or @DynamoDbImmutable", beanClassName));
targetType = ano.target().asField().type();
} else if (ano.target().kind().equals(Kind.METHOD_PARAMETER)) {
MethodParameterInfo mpi = ano.target().asMethodParameter();
if (mpi.method().isConstructor()) {
targetType = mpi.type();
}
}

if (targetType == null) {
continue;
}

DotName beanClassName = targetType.asParameterizedType().arguments().get(0).name();
DotName dbTableClassName = targetType.name();

ClassInfo beanClass = index.getClassByName(beanClassName);

if (DotNames.DYNAMODB_TABLE.equals(field.type().name())) {
if (syncSeen.add(Map.entry(tableName, beanClassName))) {
tables.produce(new DynamodbEnhancedTableBuildItem(tableName, beanClassName,
DotNames.DYNAMODB_ENHANCED_CLIENT, DYNAMODB_ENHANCED_CLIENT_TABLE_METHOD,
DotNames.DYNAMODB_TABLE));
}
if (beanClass == null) {
throw new DeploymentException(String
.format("'%s' is not in the Jandex index", beanClassName));
}

if (beanClass.annotation(DotNames.DYNAMODB_ENHANCED_BEAN) == null
&& beanClass.annotation(DotNames.DYNAMODB_ENHANCED_IMMUTABLE) == null) {
throw new DeploymentException(String
.format("'%s' must be bean annotated with @DynamoDbBean or @DynamoDbImmutable", beanClassName));
}

if (DotNames.DYNAMODB_TABLE.equals(dbTableClassName)) {
if (syncSeen.add(Map.entry(tableName, beanClassName))) {
tables.produce(new DynamodbEnhancedTableBuildItem(tableName, beanClassName,
DotNames.DYNAMODB_ENHANCED_CLIENT, DYNAMODB_ENHANCED_CLIENT_TABLE_METHOD,
DotNames.DYNAMODB_TABLE));
}
if (DotNames.DYNAMODB_ASYNC_TABLE.equals(field.type().name())) {
if (asyncSeen.add(Map.entry(tableName, beanClassName))) {
tables.produce(new DynamodbEnhancedTableBuildItem(tableName, beanClassName,
DotNames.DYNAMODB_ENHANCED_ASYNC_CLIENT, DYNAMODB_ENHANCED_ASYNC_CLIENT_TABLE_METHOD,
DotNames.DYNAMODB_ASYNC_TABLE));
}
}
if (DotNames.DYNAMODB_ASYNC_TABLE.equals(dbTableClassName)) {
if (asyncSeen.add(Map.entry(tableName, beanClassName))) {
tables.produce(new DynamodbEnhancedTableBuildItem(tableName, beanClassName,
DotNames.DYNAMODB_ENHANCED_ASYNC_CLIENT, DYNAMODB_ENHANCED_ASYNC_CLIENT_TABLE_METHOD,
DotNames.DYNAMODB_ASYNC_TABLE));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkiverse.amazon.dynamodb.enhanced.deployment;

import jakarta.enterprise.context.ApplicationScoped;

import io.quarkiverse.amazon.dynamodb.enhanced.runtime.NamedDynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;

@ApplicationScoped
public class DynamoDBEnhancedDbTableConstructorInjection {

private DynamoDbTable<DynamoDBExampleTableEntry> syncTable;

DynamoDBEnhancedDbTableConstructorInjection(
@NamedDynamoDbTable("sync") DynamoDbTable<DynamoDBExampleTableEntry> syncTable) {
this.syncTable = syncTable;
}

public DynamoDbTable<DynamoDBExampleTableEntry> getSyncTable() {
return syncTable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ public class DynamoDbEnhancedDbTableTest {
@Inject
DynamoDbAsyncTable<DynamoDBExampleTableEntry> asyncTableOther;

@Inject
DynamoDBEnhancedDbTableConstructorInjection constructorInjection;

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClass(DynamoDBExampleTableEntry.class)
.addClass(DynamoDBEnhancedDbTableConstructorInjection.class)
.addAsResource("full-config.properties", "application.properties"));

@Test
Expand All @@ -53,5 +57,6 @@ public void test() {
assertEquals("async", asyncTable.tableName());
assertEquals("async", asyncTableDuplicate.tableName());
assertEquals("async-other", asyncTableOther.tableName());
assertEquals("sync", constructorInjection.getSyncTable().tableName());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkiverse.amazon.dynamodb.enhanced.runtime;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
Expand All @@ -12,7 +13,7 @@
/**
* Specification of DynamoDb table to be injected.
*/
@Target({ FIELD })
@Target({ FIELD, PARAMETER })
@Retention(RUNTIME)
@Qualifier
@Documented
Expand Down

0 comments on commit 243bd83

Please sign in to comment.