-
Notifications
You must be signed in to change notification settings - Fork 448
/
event.go
127 lines (108 loc) · 3.72 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package stripe
import (
"encoding/json"
"fmt"
"strconv"
)
// Event is the resource representing a Stripe event.
// For more details see https://stripe.com/docs/api#events.
type Event struct {
APIResource
Account string `json:"account"`
Created int64 `json:"created"`
Data *EventData `json:"data"`
ID string `json:"id"`
Livemode bool `json:"livemode"`
PendingWebhooks int64 `json:"pending_webhooks"`
Request *EventRequest `json:"request"`
Type string `json:"type"`
}
// EventRequest contains information on a request that created an event.
type EventRequest struct {
// ID is the request ID of the request that created an event, if the event
// was created by a request.
ID string `json:"id"`
// IdempotencyKey is the idempotency key of the request that created an
// event, if the event was created by a request and if an idempotency key
// was specified for that request.
IdempotencyKey string `json:"idempotency_key"`
}
// EventData is the unmarshalled object as a map.
type EventData struct {
// Object is a raw mapping of the API resource contained in the event.
// Although marked with json:"-", it's still populated independently by
// a custom UnmarshalJSON implementation.
Object map[string]interface{} `json:"-"`
PreviousAttributes map[string]interface{} `json:"previous_attributes"`
Raw json.RawMessage `json:"object"`
}
// EventParams is the set of parameters that can be used when retrieving events.
// For more details see https://stripe.com/docs/api#retrieve_events.
type EventParams struct {
Params `form:"*"`
}
// EventList is a list of events as retrieved from a list endpoint.
type EventList struct {
APIResource
ListMeta
Data []*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 `form:"*"`
Created *int64 `form:"created"`
CreatedRange *RangeQueryParams `form:"created"`
DeliverySuccess *bool `form:"delivery_success"`
Type *string `form:"type"`
Types []*string `form:"types"`
}
// GetObjectValue returns the value from the e.Data.Object bag based on the keys hierarchy.
func (e *Event) GetObjectValue(keys ...string) string {
return getValue(e.Data.Object, keys)
}
// GetPreviousValue returns the value from the e.Data.Prev bag based on the keys hierarchy.
func (e *Event) GetPreviousValue(keys ...string) string {
return getValue(e.Data.PreviousAttributes, 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.Object)
}
// 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++ {
key := keys[i]
sliceNode, ok := node.([]interface{})
if ok {
intKey, err := strconv.Atoi(key)
if err != nil {
panic(fmt.Sprintf(
"Cannot access nested slice element with non-integer key: %s",
key))
}
node = sliceNode[intKey]
continue
}
mapNode, ok := node.(map[string]interface{})
if ok {
node = mapNode[key]
continue
}
panic(fmt.Sprintf(
"Cannot descend into non-map non-slice object with key: %s", key))
}
if node == nil {
return ""
}
return fmt.Sprintf("%v", node)
}