Skip to content

Commit 7cd74d3

Browse files
committed
feat: even simpler interface and structs
1 parent 96b0a5e commit 7cd74d3

12 files changed

+336
-333
lines changed

changes.go

+10-22
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@ var (
1515
ErrInstagramStoryInsightsHandlerNotDefined = errors.New("instagram story insights handler not defined")
1616
)
1717

18-
type Change struct {
19-
Field string `json:"field,omitempty"`
20-
Value interface{} `json:"value,omitempty"`
21-
}
22-
23-
type MentionsFieldValue struct {
18+
type Mention struct {
2419
MediaID string `json:"media_id"`
2520
CommentID string `json:"comment_id"`
2621
}
2722

28-
type StoryInsightsFieldValue struct {
23+
type StoryInsights struct {
2924
MediaID string `json:"media_id"`
3025
Exits int `json:"exits"`
3126
Replies int `json:"replies"`
@@ -35,8 +30,9 @@ type StoryInsightsFieldValue struct {
3530
Impressions int `json:"impressions"`
3631
}
3732

38-
type ChangesHandler interface {
39-
Changes(context.Context, Object, Entry, Change) error
33+
type Change struct {
34+
Field string `json:"field,omitempty"`
35+
Value interface{} `json:"value,omitempty"`
4036
}
4137

4238
func (c *Change) UnmarshalJSON(data []byte) error {
@@ -54,13 +50,13 @@ func (c *Change) UnmarshalJSON(data []byte) error {
5450
if valueRaw, ok := raw["value"]; ok {
5551
switch c.Field {
5652
case "mentions":
57-
var value MentionsFieldValue
53+
var value Mention
5854
if err := json.Unmarshal(valueRaw, &value); err != nil {
5955
return err
6056
}
6157
c.Value = value
6258
case "story_insights":
63-
var value StoryInsightsFieldValue
59+
var value StoryInsights
6460
if err := json.Unmarshal(valueRaw, &value); err != nil {
6561
return err
6662
}
@@ -82,29 +78,21 @@ func (hooks Webhooks) changes(ctx context.Context, object Object, entry Entry) e
8278
g.SetLimit(len(entry.Changes))
8379
for _, change := range entry.Changes {
8480
g.Go(func() error {
85-
return hooks.changesHandler.Changes(ctx, object, entry, change)
81+
return hooks.change(ctx, object, entry, change)
8682
})
8783
}
8884

8985
return g.Wait()
9086
}
9187

92-
func (h Webhooks) Changes(ctx context.Context, object Object, entry Entry, change Change) error {
93-
select {
94-
case <-ctx.Done():
95-
return context.Cause(ctx)
96-
default:
97-
return h.change(ctx, object, entry, change)
98-
}
99-
}
10088
func (h Webhooks) change(ctx context.Context, object Object, entry Entry, change Change) error {
10189
switch value := change.Value.(type) {
102-
case MentionsFieldValue:
90+
case Mention:
10391
if h.instagramMentionHandler == nil {
10492
return ErrInstagramMentionHandlerNotDefined
10593
}
10694
return h.instagramMentionHandler.InstagramMention(ctx, object, entry, value)
107-
case StoryInsightsFieldValue:
95+
case StoryInsights:
10896
if h.instagramStoryInsightsHandler == nil {
10997
return ErrInstagramStoryInsightsHandlerNotDefined
11098
}

entry.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ func (t *Entry) UnmarshalJSON(b []byte) error {
3939
return nil
4040
}
4141

42-
type EntryHandler interface {
43-
Entry(context.Context, Object, Entry) error
44-
}
45-
46-
func (h Webhooks) Entry(ctx context.Context, object Object, entry Entry) error {
42+
func (h Webhooks) entry(ctx context.Context, object Object, entry Entry) error {
4743
g, ctx := errgroup.WithContext(ctx)
4844

4945
g.SetLimit(2)

event.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (h Webhooks) Handle(ctx context.Context, event Event) error {
2424
case <-ctx.Done():
2525
return context.Cause(ctx)
2626
default:
27-
return h.Entry(ctx, event.Object, entry)
27+
return h.entry(ctx, event.Object, entry)
2828
}
2929
})
3030
}

handler/changes_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ func TestHandleChange(t *testing.T) {
7171
Time: 1569262486134,
7272
Changes: []handler.Change{{
7373
Field: "mentions",
74-
Value: handler.MentionsFieldValue{
74+
Value: handler.Mention{
7575
MediaID: "999",
7676
},
7777
}, {
7878
Field: "story_insights",
79-
Value: handler.StoryInsightsFieldValue{
79+
Value: handler.StoryInsights{
8080
MediaID: "999",
8181
Exits: 1,
8282
Replies: 2,
@@ -91,7 +91,7 @@ func TestHandleChange(t *testing.T) {
9191
options: func(scenario *hookScenario) []handler.Option {
9292
return []handler.Option{
9393
handler.Options.CompileSchema(),
94-
handler.Options.ChangesHandler(testHandler{func(ctx context.Context) error {
94+
handler.Options.InstagramChangesHandler(testHandler{func(ctx context.Context) error {
9595
scenario.trigger("change")
9696
return nil
9797
}}),
@@ -124,7 +124,7 @@ func TestHandleChange(t *testing.T) {
124124
Time: 1569262486134,
125125
Changes: []handler.Change{{
126126
Field: "mentions",
127-
Value: handler.MentionsFieldValue{
127+
Value: handler.Mention{
128128
MediaID: "999",
129129
},
130130
}},
@@ -167,7 +167,7 @@ func TestHandleChange(t *testing.T) {
167167
Time: 1569262486134,
168168
Changes: []handler.Change{{
169169
Field: "mentions",
170-
Value: handler.MentionsFieldValue{
170+
Value: handler.Mention{
171171
MediaID: "999",
172172
CommentID: "4444",
173173
},
@@ -216,7 +216,7 @@ func TestHandleChange(t *testing.T) {
216216
Time: 1569262486134,
217217
Changes: []handler.Change{{
218218
Field: "story_insights",
219-
Value: handler.StoryInsightsFieldValue{
219+
Value: handler.StoryInsights{
220220
MediaID: "999",
221221
Exits: 1,
222222
Replies: 2,
@@ -270,7 +270,7 @@ func TestHandleChange(t *testing.T) {
270270
Time: 1569262486134,
271271
Changes: []handler.Change{{
272272
Field: "story_insights",
273-
Value: handler.StoryInsightsFieldValue{
273+
Value: handler.StoryInsights{
274274
MediaID: "999",
275275
Exits: 1,
276276
Replies: 2,

handler/entry_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func TestHandleEvent(t *testing.T) {
118118
Time: 1569262486134,
119119
Changes: []handler.Change{{
120120
Field: "mentions",
121-
Value: handler.MentionsFieldValue{
121+
Value: handler.Mention{
122122
MediaID: "999",
123123
CommentID: "4444",
124124
},
@@ -185,7 +185,7 @@ func TestHandleEvent(t *testing.T) {
185185
Time: 1569262486134,
186186
Changes: []handler.Change{{
187187
Field: "mentions",
188-
Value: handler.MentionsFieldValue{
188+
Value: handler.Mention{
189189
MediaID: "999",
190190
CommentID: "4444",
191191
},
@@ -195,7 +195,7 @@ func TestHandleEvent(t *testing.T) {
195195
Time: 1569262486134,
196196
Changes: []handler.Change{{
197197
Field: "mentions",
198-
Value: handler.MentionsFieldValue{
198+
Value: handler.Mention{
199199
MediaID: "999",
200200
CommentID: "4444",
201201
},
@@ -205,7 +205,7 @@ func TestHandleEvent(t *testing.T) {
205205
Time: 1569262486134,
206206
Changes: []handler.Change{{
207207
Field: "mentions",
208-
Value: handler.MentionsFieldValue{
208+
Value: handler.Mention{
209209
MediaID: "999",
210210
CommentID: "4444",
211211
},

handler/handler_test.go

+5-23
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,31 @@ type testHandler struct {
2424
run func(ctx context.Context) error
2525
}
2626

27-
// Entry implements handler.EntryHandler.
28-
func (h testHandler) Entry(ctx context.Context, object handler.Object, entry handler.Entry) error {
29-
return h.run(ctx)
30-
}
31-
32-
// Changes implements handler.ChangesHandler.
33-
func (h testHandler) Changes(ctx context.Context, object handler.Object, entry handler.Entry, change handler.Change) error {
34-
return h.run(ctx)
35-
}
36-
37-
// Messaging implements handler.MessagingHandler.
38-
func (h testHandler) Messaging(ctx context.Context, object handler.Object, entry handler.Entry, messaging handler.Messaging) error {
39-
return h.run(ctx)
40-
}
41-
4227
// InstagramMention implements handler.InstagramMentionHandler.
43-
func (h testHandler) InstagramMention(ctx context.Context, object handler.Object, entry handler.Entry, mention handler.MentionsFieldValue) error {
28+
func (h testHandler) InstagramMention(ctx context.Context, object handler.Object, entry handler.Entry, mention handler.Mention) error {
4429
return h.run(ctx)
4530
}
4631

4732
// InstagramStoryInsights implements handler.InstagramStoryInsightsHandler.
48-
func (h testHandler) InstagramStoryInsights(ctx context.Context, object handler.Object, entry handler.Entry, storyInsights handler.StoryInsightsFieldValue) error {
33+
func (h testHandler) InstagramStoryInsights(ctx context.Context, object handler.Object, entry handler.Entry, storyInsights handler.StoryInsights) error {
4934
return h.run(ctx)
5035
}
5136

5237
// InstagramMessage implements handler.InstagramMessageHandler.
53-
func (h testHandler) InstagramMessage(ctx context.Context, object handler.Object, entry handler.Entry, sender string, recipient string, sent time.Time, message handler.Message) error {
38+
func (h testHandler) InstagramMessage(ctx context.Context, object handler.Object, entry handler.Entry, message handler.MessagingMessage) error {
5439
return h.run(ctx)
5540
}
5641

5742
// InstagramPostback implements handler.InstagramPostbackHandler.
58-
func (h testHandler) InstagramPostback(ctx context.Context, object handler.Object, entry handler.Entry, sender string, recipient string, sent time.Time, postback handler.Postback) error {
43+
func (h testHandler) InstagramPostback(ctx context.Context, object handler.Object, entry handler.Entry, postback handler.MessagingPostback) error {
5944
return h.run(ctx)
6045
}
6146

6247
// InstagramReferral implements handler.InstagramReferralHandler.
63-
func (h testHandler) InstagramReferral(ctx context.Context, object handler.Object, entry handler.Entry, sender string, recipient string, sent time.Time, referral handler.Referral) error {
48+
func (h testHandler) InstagramReferral(ctx context.Context, object handler.Object, entry handler.Entry, referral handler.MessagingReferral) error {
6449
return h.run(ctx)
6550
}
6651

67-
var _ handler.EntryHandler = (*testHandler)(nil)
68-
var _ handler.ChangesHandler = (*testHandler)(nil)
69-
var _ handler.MessagingHandler = (*testHandler)(nil)
7052
var _ handler.InstagramHandler = (*testHandler)(nil)
7153

7254
type hookScenario struct {

0 commit comments

Comments
 (0)