Skip to content

Commit

Permalink
Do not try to convert null results from DynamoDB async
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Oct 22, 2024
1 parent c88ba54 commit da4008f
Showing 1 changed file with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ public AsyncDynamoDbServiceIntroduction(
this.conversionService = conversionService;
}

@Override
public <T> Object doIntercept(MethodInvocationContext<Object, Object> context, Class<T> type, String tableName) {
AsyncDynamoDbService<T> service = provider.findOrCreate(tableName, type);

try {
return doIntercept(context, service);
} catch (ResourceNotFoundException ignored) {
return unwrapIfRequired(Flux.from(service.createTable()).map(t -> doIntercept(context, service)), context.getReturnType().getType());
}
}

private static void logTypeConversionFailure(Class<?> type, Object result) {
String message = "Cannot convert value %s to type %s".formatted(result, type);
LOGGER.warn(message, result, type, new IllegalArgumentException(message));
}

@SuppressWarnings("unchecked")
private <T> Publisher<T> toPublisher(Class<T> type, Argument<?> itemArgument, Map<String, MutableArgumentValue<?>> params) {
Object item = params.get(itemArgument.getName()).getValue();
Expand All @@ -92,17 +108,6 @@ private <T> Publisher<T> toPublisher(Class<T> type, Argument<?> itemArgument, Ma
return Flux.just((T) item);
}

@Override
public <T> Object doIntercept(MethodInvocationContext<Object, Object> context, Class<T> type, String tableName) {
AsyncDynamoDbService<T> service = provider.findOrCreate(tableName, type);

try {
return doIntercept(context, service);
} catch (ResourceNotFoundException ignored) {
return unwrapIfRequired(Flux.from(service.createTable()).map(t -> doIntercept(context, service)), context.getReturnType().getType());
}
}

private <T> Object doIntercept(MethodInvocationContext<Object, Object> context, AsyncDynamoDbService<T> service) {
String methodName = context.getMethodName();
if (methodName.startsWith("save")) {
Expand Down Expand Up @@ -205,14 +210,24 @@ private Object unwrapIfRequired(Publisher<?> publisher, Class<Object> type) {
if (Number.class.isAssignableFrom(type) || type.isPrimitive() && !boolean.class.isAssignableFrom(type)) {
if (Publishers.isSingle(publisher.getClass())) {
Object result = Mono.from(publisher).block();

if (result == null) {
return 0;
}

return conversionService.convert(result, type).orElseGet(() -> {
LOGGER.warn("Cannot convert value {} to type {}", result, type);
logTypeConversionFailure(type, result);
return 0;
});
}
Long count = Flux.from(publisher).count().block();

if (count == null) {
return 0;
}

return conversionService.convert(count, type).orElseGet(() -> {
LOGGER.warn("Cannot convert value {} to type {}", count, type);
logTypeConversionFailure(type, count);
return 0;
});
}
Expand All @@ -222,8 +237,13 @@ private Object unwrapIfRequired(Publisher<?> publisher, Class<Object> type) {
}

Object value = Mono.from(publisher).block();

if (value == null) {
return null;
}

return conversionService.convert(value, type).orElseGet(() -> {
LOGGER.warn("Cannot convert value {} to type {}", value, type);
logTypeConversionFailure(type, value);
return null;
});
}
Expand Down

0 comments on commit da4008f

Please sign in to comment.