1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-18 08:26:45 +02:00
woodpecker/server/datastore/context.go

32 lines
680 B
Go
Raw Normal View History

2014-09-25 11:07:40 +03:00
package datastore
import (
"code.google.com/p/go.net/context"
)
const reqkey = "datastore"
// NewContext returns a Context whose Value method returns the
// application's data storage objects.
func NewContext(parent context.Context, ds Datastore) context.Context {
return &wrapper{parent, ds}
}
type wrapper struct {
context.Context
ds Datastore
}
// Value returns the named key from the context.
func (c *wrapper) Value(key interface{}) interface{} {
if key == reqkey {
return c.ds
}
return c.Context.Value(key)
}
// FromContext returns the sql.DB associated with this context.
func FromContext(c context.Context) Datastore {
return c.Value(reqkey).(Datastore)
}