Skip to content

Commit

Permalink
feat: Ability to set context in middleware & capture responses (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gobd authored Jul 1, 2024
1 parent 693ccb1 commit bd3ee21
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ func (r *CommandContext) Logger() Logger {
return r.logger
}

// WithContext returns a shallow copy of r with its context changed
// to ctx. The provided ctx must be non-nil.
func (r *CommandContext) WithContext(ctx context.Context) *CommandContext {
if ctx == nil {
panic("nil context")
}
r2 := new(CommandContext)
*r2 = *r
r.ctx = ctx
return r
}

// newInteractionContext creates a new interaction context
func newInteractionContext(
ctx context.Context,
Expand Down Expand Up @@ -140,6 +152,18 @@ func (r *InteractionContext) Logger() Logger {
return r.logger
}

// WithContext returns a shallow copy of r with its context changed
// to ctx. The provided ctx must be non-nil.
func (r *InteractionContext) WithContext(ctx context.Context) *InteractionContext {
if ctx == nil {
panic("nil context")
}
r2 := new(InteractionContext)
*r2 = *r
r.ctx = ctx
return r
}

// newJobContext creates a new bot context
func newJobContext(ctx context.Context, logger Logger, slackClient *slack.Client, definition *JobDefinition) *JobContext {
writer := newWriter(ctx, logger, slackClient)
Expand Down Expand Up @@ -186,3 +210,15 @@ func (r *JobContext) SlackClient() *slack.Client {
func (r *JobContext) Logger() Logger {
return r.logger
}

// WithContext returns a shallow copy of r with its context changed
// to ctx. The provided ctx must be non-nil.
func (r *JobContext) WithContext(ctx context.Context) *JobContext {
if ctx == nil {
panic("nil context")
}
r2 := new(JobContext)
*r2 = *r
r.ctx = ctx
return r
}

0 comments on commit bd3ee21

Please sign in to comment.