context.go

 1package identity
 2
 3import (
 4	"context"
 5
 6	"github.com/MichaelMure/git-bug/repository"
 7)
 8
 9// identityCtxKey is a unique context key, accessible only in this struct.
10type identityCtxKey struct {
11	repo string
12}
13
14// AttachToContext attaches an Identity to a context.
15func AttachToContext(ctx context.Context, r repository.RepoCommon, u *Identity) context.Context {
16	return context.WithValue(ctx, identityCtxKey{r.GetPath()}, u)
17}
18
19// ForContext retrieves an Identity from the context, or nil if no Identity is present.
20func ForContext(ctx context.Context, r repository.RepoCommon) *Identity {
21	u, ok := ctx.Value(identityCtxKey{r.GetPath()}).(*Identity)
22	if !ok {
23		return nil
24	}
25	return u
26}