Merge pull request #1577 from xiekeyang/context

type judgment in context
pull/1560/head
Richard Scothern 2016-03-30 14:29:48 -07:00
commit e5aa9c8e56
1 changed files with 4 additions and 12 deletions

View File

@ -8,25 +8,17 @@ import (
// since that time. If the key is not found, the value returned will be zero. // since that time. If the key is not found, the value returned will be zero.
// This is helpful when inferring metrics related to context execution times. // This is helpful when inferring metrics related to context execution times.
func Since(ctx Context, key interface{}) time.Duration { func Since(ctx Context, key interface{}) time.Duration {
startedAtI := ctx.Value(key) if startedAt, ok := ctx.Value(key).(time.Time); ok {
if startedAtI != nil {
if startedAt, ok := startedAtI.(time.Time); ok {
return time.Since(startedAt) return time.Since(startedAt)
} }
}
return 0 return 0
} }
// GetStringValue returns a string value from the context. The empty string // GetStringValue returns a string value from the context. The empty string
// will be returned if not found. // will be returned if not found.
func GetStringValue(ctx Context, key interface{}) (value string) { func GetStringValue(ctx Context, key interface{}) (value string) {
stringi := ctx.Value(key) if valuev, ok := ctx.Value(key).(string); ok {
if stringi != nil {
if valuev, ok := stringi.(string); ok {
value = valuev value = valuev
} }
}
return value return value
} }