Преглед изворни кода

Add context methods for the acme linker.

Mariano Cano пре 4 година
родитељ
комит
55b0f72821
1 измењених фајлова са 23 додато и 0 уклоњено
  1. 23 0
      acme/api/linker.go

+ 23 - 0
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