Files
matchbox/api/context.go
Dalton Hubble bf72dde027 api: Convert handlers to ContextHandlers to isolate responsibility
* Add SpecMatcherHandler to match machine requests to Specs
2016-01-15 18:05:07 -08:00

33 lines
615 B
Go

package api
import (
"errors"
"golang.org/x/net/context"
)
// unexported key prevents collisions
type key int
const (
specKey key = iota
)
var (
errNoSpecFromContext = errors.New("api: Context missing a Spec")
)
// withSpec returns a copy of ctx that stores the given Spec.
func withSpec(ctx context.Context, spec *Spec) context.Context {
return context.WithValue(ctx, specKey, spec)
}
// specFromContext returns the Spec from the ctx.
func specFromContext(ctx context.Context) (*Spec, error) {
spec, ok := ctx.Value(specKey).(*Spec)
if !ok {
return nil, errNoSpecFromContext
}
return spec, nil
}