mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-22 02:47:48 +08:00
- Modified Godeps/Godeps.json by hand - [TEST] Updated welcome docs hash to sharness - [TEST] Updated contact doc - [TEST] disabled breaking test (t0080-repo refs local)
39 lines
984 B
Go
39 lines
984 B
Go
package eventlog
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
|
)
|
|
|
|
type key int
|
|
|
|
const metadataKey key = 0
|
|
|
|
// ContextWithLoggable returns a derived context which contains the provided
|
|
// Loggable. Any Events logged with the derived context will include the
|
|
// provided Loggable.
|
|
func ContextWithLoggable(ctx context.Context, l Loggable) context.Context {
|
|
existing, err := MetadataFromContext(ctx)
|
|
if err != nil {
|
|
// context does not contain meta. just set the new metadata
|
|
child := context.WithValue(ctx, metadataKey, Metadata(l.Loggable()))
|
|
return child
|
|
}
|
|
|
|
merged := DeepMerge(existing, l.Loggable())
|
|
child := context.WithValue(ctx, metadataKey, merged)
|
|
return child
|
|
}
|
|
|
|
func MetadataFromContext(ctx context.Context) (Metadata, error) {
|
|
value := ctx.Value(metadataKey)
|
|
if value != nil {
|
|
metadata, ok := value.(Metadata)
|
|
if ok {
|
|
return metadata, nil
|
|
}
|
|
}
|
|
return nil, errors.New("context contains no metadata")
|
|
}
|