I have multiple types like this:
type QueryMessage struct {
Header MessageHeader
Type MessageType
Query SqlQuery
}
type UpdateMessage struct {
Header MessageHeader
Type MessageType
OldData map[string]interface{}
NewData map[string]interface{}
}
type InsertMessage struct {
Header MessageHeader
Type MessageType
Data map[string]interface{}
}
They all have two properties in common, Header
and Type
. In the end I need to aggregate them into an array of generic messages.
At the moment my Message
interface looks like this:
type Message interface {}
So what I basically do is something like this (casting them all to Message interface):
q := QueryMessage{ ... }
u := UpdateMessage{ ... }
i := InsertMessage{ ... }
allMessages := [3]Message { Message(q), Message(u), Message(i), }
This works, but it loses all the type information and I would like to be able to still expose Header
and Type
from the Message
type (so that client code theoretically could cast the Message
based on the Type
back to it's original type.
How can this be done? I couldn't figure out a proper way, interfaces can't have properties and if I make Message
a struct, Go doesn't allow me to cast e. g. a QueryMessage
to a Message
anymore.