Documentation ¶
Overview ¶
Package ctxdata provides a helper type for request-scoped metadata.
Index ¶
- Variables
- type Data
- func (d *Data) Get(key string) (value string, ok bool)
- func (d *Data) GetAll() map[string]string
- func (d *Data) GetAllSlice() []KeyValue
- func (d *Data) GetDefault(key, defaultValue string) (value string)
- func (d *Data) Set(key, value string) error
- func (d *Data) Walk(fn func(key, value string) error) error
- type KeyValue
Constants ¶
This section is empty.
Variables ¶
var ErrNilData = errors.New("nil data (no data in context?)")
ErrNilData is returned by certain methods on the Data type when they're called with a nil pointer receiver. This can happen when methods are chained with the From helper, and indicates a Data object wasn't found in the context.
Functions ¶
This section is empty.
Types ¶
type Data ¶
type Data struct {
// contains filtered or unexported fields
}
Data is an opaque type that can be injected into a context at the start of a request, updated with metadata over the course of the request, and queried at the end of the request for collected metadata.
When a new request arrives in your program, HTTP server, etc., use the New constructor with the incoming request's context to construct a new, empty Data. Use the returned context for all further operations on that request, and use the From helper to retrieve the request's Data and set or get metadata. At the end of the request, all metadata collected will be available from any point in the callstack.
func From ¶
From extracts a Data object from the provided context. If no Data object was injected to the context, the returned pointer will be nil, but all Data methods gracefully handle this situation, so it's safe to chain.
func New ¶
New constructs a Data object and injects it into the provided context. Use the returned context for all further operations in this request lifecycle. The returned Data object can be directly queried for metadata collected over the life of the request.
func (*Data) GetAll ¶
GetAll returns a copy of all of the keys and values currently stored as an unordered map.
func (*Data) GetAllSlice ¶
GetAllSlice returns a copy of all of the keys and values currently stored as a slice of KeyValues, in the order they were added (set).
func (*Data) GetDefault ¶
GetDefault returns the value of key, or defaultValue if no value is available.