type BoardEventNotesUpdated struct {
author uuid.UUID
id uuid.UUID
position struct {
column uuid.UUID
rank int
stack bool
}
text string
}
type BoardEvent[T BoardEventNotesUpdated | EventTypeB | ...] struct {
eventType BoardEventType
data T
}
Or use the already existing dto's
realtime/boards.go
BroadcastToBoard needs to be a generic method. This leads to making the broker also generic since parameterized methods are not allowed.
If BroadcastToBoard should be a generic method -> structs that have a realtime.broker field must be generic too
type Broker[T DataType] struct {
con Client
}
A different solution:
We could also change the broadcast methods to functions. This way we can easily add generics.
Since our service layers have a broker connection it's also easy to pass on the connection as parameter instead of using an implicit call.
More boilerplate code (not all events have a dto format / functions to parse data to struct)
Conclusion:
Using generics for the realtime messages would be a nice addition, since it simplifies the error handling and parsing.
The problem here is that go doesn’t allow parameterized methods. To fix this issue we would have to make the Broker generic
But this also forces all other structures that have a realtime.broker field to be generic (all services)
Another solution would be to make the BroadcastToBoard method a function.
But do we really need generic realtime messages?
The broker only sends messages, but never receives any, which means that if implemented correctly there is no way to mess up a message and type safety isn't necessary
The only thing that would change is that the data is not passed as type interface{} when sending, but it forces to parse the data into a structure and then pass it on.
For most messages we already do that and in some cases like delete it would lead to more boilerplate code because instead of just passing the ID we would have to parse the ID into a structure first and then send it, which seems like an unnecessary step.
This leads me to the conclusion that we don’t need generics for our realtime messages. This only makes our code more complex leading to the same result.
Using generics instead of interfaces as message data
What will change:
realtime/boards.go:
Translates to this struct:
Or use the already existing dto's
realtime/boards.go
BroadcastToBoard needs to be a generic method. This leads to making the broker also generic since parameterized methods are not allowed.services/...:
realtime.broker
field must be generic tooA different solution: We could also change the broadcast methods to functions. This way we can easily add generics. Since our service layers have a broker connection it's also easy to pass on the connection as parameter instead of using an implicit call.
How would we benifit:
What problems would we encounter:
Conclusion:
Using generics for the realtime messages would be a nice addition, since it simplifies the error handling and parsing. The problem here is that go doesn’t allow parameterized methods. To fix this issue we would have to make the Broker generic
But this also forces all other structures that have a realtime.broker field to be generic (all services)
Another solution would be to make the BroadcastToBoard method a function.
But do we really need generic realtime messages? The broker only sends messages, but never receives any, which means that if implemented correctly there is no way to mess up a message and type safety isn't necessary
The only thing that would change is that the data is not passed as type interface{} when sending, but it forces to parse the data into a structure and then pass it on. For most messages we already do that and in some cases like delete it would lead to more boilerplate code because instead of just passing the ID we would have to parse the ID into a structure first and then send it, which seems like an unnecessary step.
This leads me to the conclusion that we don’t need generics for our realtime messages. This only makes our code more complex leading to the same result.