Skip to content

Commit

Permalink
type judgment in context
Browse files Browse the repository at this point in the history
Signed-off-by: xiekeyang <[email protected]>
  • Loading branch information
xiekeyang committed Mar 30, 2016
1 parent 0eefe17 commit e388517
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions context/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,17 @@ import (
// 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.
func Since(ctx Context, key interface{}) time.Duration {
startedAtI := ctx.Value(key)
if startedAtI != nil {
if startedAt, ok := startedAtI.(time.Time); ok {
return time.Since(startedAt)
}
if startedAt, ok := ctx.Value(key).(time.Time); ok {
return time.Since(startedAt)
}

return 0
}

// GetStringValue returns a string value from the context. The empty string
// will be returned if not found.
func GetStringValue(ctx Context, key interface{}) (value string) {
stringi := ctx.Value(key)
if stringi != nil {
if valuev, ok := stringi.(string); ok {
value = valuev
}
if valuev, ok := ctx.Value(key).(string); ok {
value = valuev
}

return value
}

0 comments on commit e388517

Please sign in to comment.