diff options
author | Luke Granger-Brown <git@lukegb.com> | 2020-06-18 18:53:08 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-06-27 22:58:22 +0200 |
commit | 8a38897f79876b9372715150795b9da570af5a86 (patch) | |
tree | 1debbc91fda1c70adfb24a24072b8ca7cb651167 | |
parent | cf67c78823bd1e7591c2199d51aa3f3fffed73c4 (diff) | |
download | git-bug-8a38897f79876b9372715150795b9da570af5a86.tar.gz |
Add context.go to identity, used for attaching identities to and retrieving them from a context.Context
-rw-r--r-- | identity/context.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/identity/context.go b/identity/context.go new file mode 100644 index 00000000..619e2c29 --- /dev/null +++ b/identity/context.go @@ -0,0 +1,26 @@ +package identity + +import ( + "context" + + "github.com/MichaelMure/git-bug/repository" +) + +// identityCtxKey is a unique context key, accessible only in this struct. +type identityCtxKey struct { + repo string +} + +// AttachToContext attaches an Identity to a context. +func AttachToContext(ctx context.Context, r repository.RepoCommon, u *Identity) context.Context { + return context.WithValue(ctx, identityCtxKey{r.GetPath()}, u) +} + +// ForContext retrieves an Identity from the context, or nil if no Identity is present. +func ForContext(ctx context.Context, r repository.RepoCommon) *Identity { + u, ok := ctx.Value(identityCtxKey{r.GetPath()}).(*Identity) + if !ok { + return nil + } + return u +} |