-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.go
66 lines (53 loc) · 1.14 KB
/
entry.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
package gometawebhooks
import (
"context"
"encoding/json"
"errors"
"fmt"
"golang.org/x/sync/errgroup"
)
var (
ErrParsingEntry = errors.New("parsing entry")
)
type Entry struct {
Id string `json:"id"`
Time int64 `json:"time"`
Messaging []Messaging `json:"messaging,omitempty"`
Changes []Change `json:"changes,omitempty"`
}
func (t *Entry) UnmarshalJSON(b []byte) error {
type Alias Entry
var entry Alias
if err := json.Unmarshal(b, &entry); err != nil {
return err
}
if entry.Id == "" {
return fmt.Errorf("missing 'id' field: %w", ErrParsingEntry)
}
if entry.Time == 0 {
return fmt.Errorf("missing 'time' field: %w", ErrParsingEntry)
}
*t = Entry(entry)
return nil
}
func (h Webhooks) entry(ctx context.Context, object Object, entry Entry) error {
g, ctx := errgroup.WithContext(ctx)
g.SetLimit(2)
g.Go(func() error {
select {
case <-ctx.Done():
return context.Cause(ctx)
default:
return h.changes(ctx, object, entry)
}
})
g.Go(func() error {
select {
case <-ctx.Done():
return context.Cause(ctx)
default:
return h.messaging(ctx, object, entry)
}
})
return g.Wait()
}