This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
80 lines (67 loc) · 2.12 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package stripe
import (
"encoding/json"
"fmt"
)
// Event is the resource representing a Stripe event.
// For more details see https://stripe.com/docs/api#events.
type Event struct {
ID string `json:"id"`
Live bool `json:"livemode"`
Created int64 `json:"created"`
Data *EventData `json:"data"`
Webhooks uint64 `json:"pending_webhooks"`
Type string `json:"type"`
Req string `json:"request"`
UserID string `json:"user_id"`
}
// EventData is the unmarshalled object as a map.
type EventData struct {
Raw json.RawMessage `json:"object"`
Prev map[string]interface{} `json:"previous_attributes"`
Obj map[string]interface{}
}
// EventList is a list of events as retrieved from a list endpoint.
type EventList struct {
ListMeta
Values []*Event `json:"data"`
}
// EventListParams is the set of parameters that can be used when listing events.
// For more details see https://stripe.com/docs/api#list_events.
type EventListParams struct {
ListParams
Created int64
// Type is one of the values documented at https://stripe.com/docs/api#event_types.
Type string
}
// GetObjValue returns the value from the e.Data.Obj bag based on the keys hierarchy.
func (e *Event) GetObjValue(keys ...string) string {
return getValue(e.Data.Obj, keys)
}
// GetPrevValue returns the value from the e.Data.Prev bag based on the keys hierarchy.
func (e *Event) GetPrevValue(keys ...string) string {
return getValue(e.Data.Prev, keys)
}
// UnmarshalJSON handles deserialization of the EventData.
// This custom unmarshaling exists so that we can keep both the map and raw data.
func (e *EventData) UnmarshalJSON(data []byte) error {
type eventdata EventData
var ee eventdata
err := json.Unmarshal(data, &ee)
if err != nil {
return err
}
*e = EventData(ee)
return json.Unmarshal(e.Raw, &e.Obj)
}
// getValue returns the value from the m map based on the keys.
func getValue(m map[string]interface{}, keys []string) string {
node := m[keys[0]]
for i := 1; i < len(keys); i++ {
node = node.(map[string]interface{})[keys[i]]
}
if node == nil {
return ""
}
return fmt.Sprintf("%v", node)
}