Skip to content

Commit 6de92fb

Browse files
committed
Change up how we marshal events and unmarshal them
1 parent a9d3faa commit 6de92fb

3 files changed

Lines changed: 48 additions & 48 deletions

File tree

events/event.go

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,13 @@
11
package events
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"time"
67

78
"github.com/rs/xid"
89
)
910

10-
//#region EventDataType
11-
12-
// EventObjectType holds the event type we are working.
13-
type EventObjectType string
14-
15-
const (
16-
// SubscriptionDataType describes the type of the data we are holding.
17-
// It allow us easily map data from the database to a struct
18-
SubscriptionDataType EventObjectType = "subscription"
19-
20-
// PurchaseDataType describes the type of the data we are holding.
21-
// It allow us easily map data from the database to a struct
22-
PurchaseDataType EventObjectType = "purchase"
23-
)
24-
25-
//#endregion EventDataType
26-
27-
//#region event types
28-
29-
// EventType is a unique key defined by event
30-
type EventType string
31-
32-
const (
33-
// AppSubscribedEventType defines the string constant name for the event that happens when
34-
// a user subscribes to a plan
35-
AppSubscribedEventType EventType = "app.subscription.created"
36-
37-
// AppSubscriptionCancelledEventType defines the string constant name for the event that happens when
38-
// a user cancels a subscription
39-
AppSubscriptionCancelledEventType EventType = "app.subscription.cancelled"
40-
41-
// AppSubscriptionModifiedEventType defines the string constant name for the event that happens when
42-
// a subscription is modified
43-
AppSubscriptionModifiedEventType EventType = "app.subscription.modified"
44-
45-
// AppPurchasedEventType indicates which type the struct is. Could make it easier for us data map this event from the database
46-
AppPurchasedEventType EventType = "app.purchase.created"
47-
)
48-
49-
func (et EventType) Valid() bool {
50-
return et == AppSubscribedEventType || et == AppPurchasedEventType ||
51-
et == AppSubscriptionCancelledEventType || et == AppSubscriptionModifiedEventType
52-
}
53-
54-
//#endregion event types
55-
5611
// App holds information about the application which the event was triggered
5712
type App struct {
5813
// ID defines the application id that is provided by the publisher
@@ -116,17 +71,22 @@ type Event struct {
11671
When time.Time `bson:"when" json:"when"`
11772

11873
// Data has the event specific payload
119-
Data interface{} `bson:"data" json:"data"`
74+
Data json.RawMessage `bson:"data" json:"data"`
12075
}
12176

12277
// New creates a new event with the status of created
12378
func New(name EventType, data interface{}) Event {
79+
marshalledJson, err := json.Marshal(data)
80+
if err != nil {
81+
panic(fmt.Sprintf("error creating a new event: %s", err.Error()))
82+
}
83+
12484
return Event{
12585
ID: fmt.Sprintf("evt_%s", xid.New().String()),
12686
Type: name,
12787
When: time.Now(),
12888
APIVersion: CurrentAPIVersion,
129-
Data: data,
89+
Data: marshalledJson,
13090
}
13191
}
13292

events/objectTypes.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package events
2+
3+
// EventObjectType holds the event type we are working.
4+
type EventObjectType string
5+
6+
const (
7+
// SubscriptionDataType describes the type of the data we are holding.
8+
// It allow us easily map data from the database to a struct
9+
SubscriptionDataType EventObjectType = "subscription"
10+
11+
// PurchaseDataType describes the type of the data we are holding.
12+
// It allow us easily map data from the database to a struct
13+
PurchaseDataType EventObjectType = "purchase"
14+
)

events/types.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package events
2+
3+
// EventType is a unique key defined by event
4+
type EventType string
5+
6+
const (
7+
// AppSubscribedEventType defines the string constant name for the event that happens when
8+
// a user subscribes to a plan
9+
AppSubscribedEventType EventType = "app.subscription.created"
10+
11+
// AppSubscriptionCancelledEventType defines the string constant name for the event that happens when
12+
// a user cancels a subscription
13+
AppSubscriptionCancelledEventType EventType = "app.subscription.cancelled"
14+
15+
// AppSubscriptionModifiedEventType defines the string constant name for the event that happens when
16+
// a subscription is modified
17+
AppSubscriptionModifiedEventType EventType = "app.subscription.modified"
18+
19+
// AppPurchasedEventType indicates which type the struct is. Could make it easier for us data map this event from the database
20+
AppPurchasedEventType EventType = "app.purchase.created"
21+
)
22+
23+
func (et EventType) Valid() bool {
24+
return et == AppSubscribedEventType || et == AppPurchasedEventType ||
25+
et == AppSubscriptionCancelledEventType || et == AppSubscriptionModifiedEventType
26+
}

0 commit comments

Comments
 (0)