Skip to content

Commit

Permalink
feat(event): add VersionFromContext
Browse files Browse the repository at this point in the history
  • Loading branch information
SevereCloud committed Mar 29, 2022
1 parent e983680 commit 877a027
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions events/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ func GroupIDFromContext(ctx context.Context) int {
func EventIDFromContext(ctx context.Context) string {
return ctx.Value(internal.EventIDKey).(string)
}

// VersionFromContext returns the version from context.
func VersionFromContext(ctx context.Context) string {
return ctx.Value(internal.EventVersionKey).(string)
}
8 changes: 8 additions & 0 deletions events/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ func TestEventIDFromContext(t *testing.T) {
ctx := context.WithValue(context.Background(), internal.EventIDKey, eventID)
assert.Equal(t, eventID, events.EventIDFromContext(ctx))
}

func TestVersionFromContext(t *testing.T) {
t.Parallel()

const version = "5.131"
ctx := context.WithValue(context.Background(), internal.EventVersionKey, version)
assert.Equal(t, version, events.VersionFromContext(ctx))
}
2 changes: 2 additions & 0 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type GroupEvent struct {
Object json.RawMessage `json:"object"`
GroupID int `json:"group_id"`
EventID string `json:"event_id"`
V string `json:"v"`
Secret string `json:"secret"`
}

Expand Down Expand Up @@ -158,6 +159,7 @@ func NewFuncList() *FuncList {
func (fl FuncList) Handler(ctx context.Context, e GroupEvent) error { // nolint:gocyclo
ctx = context.WithValue(ctx, internal.GroupIDKey, e.GroupID)
ctx = context.WithValue(ctx, internal.EventIDKey, e.EventID)
ctx = context.WithValue(ctx, internal.EventVersionKey, e.V)

if sliceFunc, ok := fl.special[e.Type]; ok {
for _, f := range sliceFunc {
Expand Down
1 change: 1 addition & 0 deletions internal/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
CallbackRetryCounterKey
CallbackRetryAfterKey
CallbackRemove
EventVersionKey
)

// ContextClient return *http.Client.
Expand Down

0 comments on commit 877a027

Please sign in to comment.