chibenwa commented on code in PR #2991:
URL: https://github.com/apache/james-project/pull/2991#discussion_r3044207581
##########
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:
Hmm we are actually doing unnecessary conversions because we are getting the
wrong typing model trying to fit bytes and string together.
Actually I am against those types conversions that defeat the intent to
avoid them which matters at scale.
Please spend the time to refine the data model to:
- use a generic? (I prefer)
- have 2 types of success, one for string, one for bytes
--
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]