Arsnael commented on code in PR #2991:
URL: https://github.com/apache/james-project/pull/2991#discussion_r3044218990
##########
event-bus/api/src/main/java/org/apache/james/events/EventSerializer.java:
##########
@@ -22,30 +22,37 @@
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
-import java.util.Optional;
public interface EventSerializer {
- Optional<String> toJson(Event event);
+ SerializationResult toJson(Event event);
- Optional<String> toJson(Collection<Event> event);
+ SerializationResult toJson(Collection<Event> event);
- default Optional<byte[]> toJsonBytes(Event event) {
- return toJson(event).map(json ->
json.getBytes(StandardCharsets.UTF_8));
+ default SerializationBytesResult toJsonBytes(Event event) {
+ return switch(toJson(event)) {
+ case SerializationResult.Success success -> new
SerializationBytesResult.Success(success.json().getBytes(StandardCharsets.UTF_8));
+ case SerializationResult.Failure failure -> new
SerializationBytesResult.Failure(failure.throwingMessage());
+ default -> new SerializationBytesResult.Failure("Could not
serialize event: " + event);
+ };
}
- default Optional<byte[]> toJsonBytes(Collection<Event> event) {
- return toJson(event).map(json ->
json.getBytes(StandardCharsets.UTF_8));
+ default SerializationBytesResult toJsonBytes(Collection<Event> event) {
+ return switch(toJson(event)) {
+ case SerializationResult.Success success -> new
SerializationBytesResult.Success(success.json().getBytes(StandardCharsets.UTF_8));
Review Comment:
I honestly hesitated generifying this. I'm more afraid of adding complexity
to the caller as a result. But fair point.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]