Skip to content

Commit

Permalink
feat(api): Add support for HeaderInformation in the WithEventBus
Browse files Browse the repository at this point in the history
…verticle trait with many new overloaded methods for a better coding experience.

Co-authored-by: cb0s <cedric.boes@online.de>
  • Loading branch information
fussel178 and cb0s committed Jan 29, 2022
1 parent efc3886 commit 9bc61d3
Show file tree
Hide file tree
Showing 2 changed files with 440 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,59 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import de.wuespace.telestion.api.message.JsonMessage;
import io.vertx.core.Future;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;

/**
* A wrapper for a decoded message from the {@link Message Vert.x message} to the {@link JsonMessage} type
* and basis {@link Message Vert.x message}
*
* @param message the basic {@link Message Vert.x message}
* @param body the decoded body of the {@link Message Vert.x message} to the {@link JsonMessage} type
* @param <V> the type of the {@link JsonMessage} to map to
* @param <T> the type of body of the {@link Message Vert.x message}
* @see JsonMessage#on(Class, Message)
*
* @author Ludwig Richter (@fussel178)
*/
public record DecodedMessage<V extends JsonMessage, T extends JsonObject>(
@JsonProperty V body,
@JsonProperty Message<T> message
) implements JsonMessage {

/**
* Composes the given future, which eventually returns the received message, with the returned future
* to map from the {@link Message Vert.x message} to the {@link JsonMessage} type.
*
* @param clazz the class type of the {@link JsonMessage} to map to
* @param messageFuture the given future which eventually returns the received message
* @param <V> the type of the {@link JsonMessage} to map to
* @param <T> the type of body of the {@link Message Vert.x message}
* @return a new future composed with the given future that maps from the {@link Message Vert.x message}
* to the {@link JsonMessage} type
* @see JsonMessage#on(Class, Message)
*/
public static <V extends JsonMessage, T extends JsonObject> Future<DecodedMessage<V, T>> compose(
Class<V> clazz,
Future<Message<T>> messageFuture) {
return messageFuture.compose(message -> on(clazz, message));
}

/**
* Decodes the given {@link Message Vert.x message} to the {@link JsonMessage} type and returns a {@link Future}
* which eventually returns a {@link DecodedMessage} with the mapped contents.
*
* @param clazz the class type of the {@link JsonMessage} to map to
* @param message the received {@link Message Vert.x message}
* @param <V> the type of the {@link JsonMessage} to map to
* @param <T> the type of body of the {@link Message Vert.x message}
* @return a new future which eventually returns a {@link DecodedMessage} with the mapped contents
* @see JsonMessage#on(Class, Message)
*/
public static <V extends JsonMessage, T extends JsonObject> Future<DecodedMessage<V, T>> on(
Class<V> clazz,
Message<T> message) {
return JsonMessage.on(clazz, message).map(decoded -> new DecodedMessage<>(decoded, message));
}
}
Loading

0 comments on commit 9bc61d3

Please sign in to comment.