Skip to content

Commit

Permalink
Provide stash.Set
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Aug 21, 2024
1 parent bc6ff1a commit 059571f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 2 additions & 4 deletions httpactions/httpactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ func (h *httpAction) Execute(ctx context.Context) error {
return fmt.Errorf("actions.httpAction: error during request: %w", err)
}

st := stash.FromContext(ctx)

logger.DebugContext(ctx, "actions.httpAction", slog.Any("request", req))
logger.DebugContext(ctx, "actions.httpAction", slog.Any("response", res))
st.Set(prevRequestKey{}, req)
st.Set(prevResponseKey{}, res)
stash.Set(ctx, prevRequestKey{}, req)

Check failure on line 69 in httpactions/httpactions.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `stash.Set` is not checked (errcheck)
stash.Set(ctx, prevResponseKey{}, res)

Check failure on line 70 in httpactions/httpactions.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `stash.Set` is not checked (errcheck)
return nil
}
10 changes: 10 additions & 0 deletions stash/stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stash

import (
"context"
"errors"

"github.com/lestrrat-go/scriptor/ctxutil"
)
Expand Down Expand Up @@ -45,6 +46,15 @@ func (s *stash) Get(k any) (any, bool) {
return v, ok
}

func Set(ctx context.Context, key any, value any) error {
st := FromContext(ctx)
if st == nil {
return errors.New("stash.Set: no stash found in context")
}
st.Set(key, value)
return nil
}

func Fetch[T any](ctx context.Context, key any, dst *T) bool {
st := FromContext(ctx)
if st == nil {
Expand Down

0 comments on commit 059571f

Please sign in to comment.