ctx

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: May 7, 2022 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 Key added in v0.3.14

type Key string

func (Key) Key added in v0.3.14

func (k Key) Key() string

Key returns key so it can be used as a key a map[string].

func (Key) String added in v0.3.14

func (k Key) String() string

String formats the stringified key with additional contextual information

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
	// contains filtered or unexported methods
}

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.

func WithKeyRing added in v0.3.14

func WithKeyRing(parent KeyRingable, additional ...CtxKeyable) KeyRingable

WithKeyRing constructs a new KeyRingable from the parent and adds additional CtxKeyables to the new KeyRingable.

Jump to

Keyboard shortcuts

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