From 55b0f7282144f1c5a343d0c623248c9108acf706 Mon Sep 17 00:00:00 2001 From: Mariano Cano Date: Thu, 28 Apr 2022 15:14:15 -0700 Subject: [PATCH] Add context methods for the acme linker. --- acme/api/linker.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/acme/api/linker.go b/acme/api/linker.go index a605ffc3..114ba698 100644 --- a/acme/api/linker.go +++ b/acme/api/linker.go @@ -41,6 +41,29 @@ type Linker interface { LinkOrdersByAccountID(ctx context.Context, orders []string) } +type linkerKey struct{} + +// NewLinkerContext adds the given linker to the context. +func NewLinkerContext(ctx context.Context, v Linker) context.Context { + return context.WithValue(ctx, linkerKey{}, v) +} + +// LinkerFromContext returns the current linker from the given context. +func LinkerFromContext(ctx context.Context) (v Linker, ok bool) { + v, ok = ctx.Value(linkerKey{}).(Linker) + return +} + +// MustLinkerFromContext returns the current linker from the given context. It +// will panic if it's not in the context. +func MustLinkerFromContext(ctx context.Context) Linker { + if v, ok := LinkerFromContext(ctx); !ok { + panic("acme linker is not the context") + } else { + return v + } +} + // linker generates ACME links. type linker struct { prefix string