ctx

package
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2021 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

The package ctx defines how keys in a *http.Request.Context should behave and a way for storing and retrieving those keys for wider use in the application.

The main method for managing keys is through a KeyRing, or a custom implementation of KeyRingable.

Following https://medium.com/@matryer/context-keys-in-go-5312346a868d, context keys ought to be unexported by a package. This package cannot, in other words, provide default keys to be used in a context.Context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByCtxKeyable

type ByCtxKeyable []CtxKeyable

func (ByCtxKeyable) Len

func (k ByCtxKeyable) Len() int

func (ByCtxKeyable) Less

func (k ByCtxKeyable) Less(i, j int) bool

func (ByCtxKeyable) Swap

func (k ByCtxKeyable) Swap(i, j int)

type CtxKeyable

type CtxKeyable interface {
	// The key as in a key-value pair
	Key() string

	// A stringified version of the key, for logging
	String() string
}

type KeyRing

type KeyRing struct {
	// contains filtered or unexported fields
}

KeyRing stores CtxKeyables and cannot be mutated outside of a constructor.

func (*KeyRing) CurrentUserKey

func (kr *KeyRing) CurrentUserKey() CtxKeyable

CurrentUserKey returns the key set in the currentUserKey parameter of NewKeyRing or nil.

func (*KeyRing) Key

func (kr *KeyRing) Key(name string) CtxKeyable

Key returns the key by name (i.e., CtxKeyable.Key()) or nil.

func (*KeyRing) SessionKey

func (kr *KeyRing) SessionKey() CtxKeyable

SessionKey returns the key set in the sessionKey parameter of NewKeyRing or nil.

type KeyRingable

type KeyRingable interface {
	CurrentUserKey() CtxKeyable
	Key(name string) CtxKeyable
	SessionKey() CtxKeyable
}

Something KeyRingable because it stores arbitrary keys, accessible by a string name, and makes it convenient to grab a CurrentUserKey or SessionKey.

func NewKeyRing

func NewKeyRing(sessionKey, currentUserKey CtxKeyable, additional ...CtxKeyable) KeyRingable

NewKeyRing constructs a KeyRing from the given CtxKeyables. NewKeyRing requires keys to be retrieved through CurrentUserKey() and SessionKey(), respectively. NewKeyRing accepts an arbitrary number of other CtxKeyables, accessible through the Key method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL