36 lines
720 B
Go
36 lines
720 B
Go
package serverevents
|
|
|
|
import (
|
|
"encoding/json"
|
|
di "git.apihub24.de/admin/generic-di"
|
|
)
|
|
|
|
func init() {
|
|
di.Injectable(newMessageParser)
|
|
}
|
|
|
|
type IMessageParser interface {
|
|
ToString(event Event) (string, error)
|
|
FromString(eventStr string) (Event, error)
|
|
}
|
|
|
|
type messageParser struct{}
|
|
|
|
func newMessageParser() IMessageParser {
|
|
return &messageParser{}
|
|
}
|
|
|
|
func (parser *messageParser) ToString(event Event) (string, error) {
|
|
stream, marshalErr := json.Marshal(event)
|
|
if marshalErr != nil {
|
|
return "", marshalErr
|
|
}
|
|
return string(stream), nil
|
|
}
|
|
|
|
func (parser *messageParser) FromString(eventStr string) (Event, error) {
|
|
var ev Event
|
|
unmarshalErr := json.Unmarshal([]byte(eventStr), &ev)
|
|
return ev, unmarshalErr
|
|
}
|