-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77e33b9
commit e115a1c
Showing
3 changed files
with
98 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
package events | ||
|
||
func init() { | ||
register[AccountCreated]("account.created") | ||
register[AccountProfileUpdated]("account.profile_updated") | ||
register[AccountSessionOpened]("account.session_opened") | ||
} | ||
|
||
type ( | ||
AccountCreated struct { | ||
ID int64 | ||
Email string | ||
Email string `json:"email"` | ||
ID int64 `json:"id"` | ||
} | ||
|
||
AccountProfileUpdated struct { | ||
ID int64 | ||
Name string | ||
About string | ||
Callsign string | ||
Name string `json:"name"` | ||
About string `json:"about"` | ||
Callsign string `json:"callsign"` | ||
ID int64 `json:"id"` | ||
} | ||
|
||
AccountSessionOpened struct { | ||
ID int64 | ||
UserAgent string | ||
IP string | ||
UserAgent string `json:"user_agent"` | ||
IP string `json:"ip"` | ||
ID int64 `json:"id"` | ||
} | ||
) | ||
|
||
func (AccountCreated) Event() string { return "account.created" } | ||
func (AccountProfileUpdated) Event() string { return "account.profile_updated" } | ||
func (AccountSessionOpened) Event() string { return "account.session_opened" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,56 @@ | ||
package events | ||
|
||
type Event interface { | ||
Event() string | ||
import ( | ||
"encoding/gob" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// A registrant is a type registered with the event system | ||
// and available for decoding. | ||
type registrant struct { | ||
new func() any | ||
friendly string // a friendly name for the type, e.g. "user.session_created" | ||
name string // the name of the type, e.g. "events.UserCreated" | ||
} | ||
|
||
// Registry holds the registered types. | ||
var registry = make(map[string]registrant) | ||
|
||
// Register a type with the event system. We collect the name of the event, | ||
// a friendly type for user/consumer reference, and a function that returns a pointer | ||
// to a new instance of the type. We also register the type with gob, should | ||
// a consumer wish to encode/decode the event to/from a binary format. | ||
// | ||
// The friendly name shouldn't be used for anything other than display. Internal | ||
// references should use the name field. | ||
func register[K any](friendly string) { | ||
gob.Register(*new(K)) | ||
|
||
name := fmt.Sprintf("%T", *new(K)) | ||
registry[name] = registrant{ | ||
name: name, | ||
new: func() any { | ||
return new(K) | ||
}, | ||
friendly: friendly, | ||
} | ||
} | ||
|
||
// Decode an event from JSON. The kind is the name of the event, and the data is the | ||
// JSON payload. This is useful for decoding events from the database or queue. | ||
func Decode(kind string, data []byte) (any, error) { | ||
k := registry[kind].new() | ||
return k, json.Unmarshal(data, k) | ||
} | ||
|
||
// FriendlyNameFor returns the friendly name for the event type, as provided | ||
// during registration. | ||
func FriendlyNameFor(kind string) string { | ||
return registry[kind].friendly | ||
} | ||
|
||
// NameFor returns the name of the event type, e.g. "events.UserCreated". | ||
func NameFor(kind string) string { | ||
return registry[kind].name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters