forked from distribution/distribution
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
You shouldn't have to import both: github.com/docker/distribution/context golang.org/x/net/context just to use the distribution tools and implement the distribution interfaces. By pulling the Context interface from golang.org/x/net/context into the context package within the distribution project, you no longer have to import both packages. Note: You do not have to change anything anywhere else yet! All current uses of both packages together will still work correctly because the Context interface from either package is identical. I've also made some other minor changes: - Added a RemoteIP function. It's like RemoteAddr but discards the port suffix - Added `.String()` to the response duration context value so that JSON log formatting shows human-parseable duration and not just number of nano-seconds - Added WithMapContext(...) to the context package. This is a useful function so I pulled it out of the main.go in cmd/registry so that it can be used elsewhere. Docker-DCO-1.1-Signed-off-by: Josh Hawn <[email protected]> (github: jlhawn)
- Loading branch information
Josh Hawn
committed
Apr 1, 2015
1 parent
5052dc6
commit 731e0b0
Showing
9 changed files
with
97 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,7 @@ bin/* | |
|
||
# Cover profiles | ||
*.out | ||
|
||
# Editor/IDE specific files. | ||
*.sublime-project | ||
*.sublime-workspace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package context | ||
|
||
import ( | ||
"golang.org/x/net/context" | ||
) | ||
|
||
// Context is a copy of Context from the golang.org/x/net/context package. | ||
type Context interface { | ||
context.Context | ||
} | ||
|
||
// Background returns a non-nil, empty Context. | ||
func Background() Context { | ||
return context.Background() | ||
} | ||
|
||
// WithValue returns a copy of parent in which the value associated with key is | ||
// val. Use context Values only for request-scoped data that transits processes | ||
// and APIs, not for passing optional parameters to functions. | ||
func WithValue(parent Context, key, val interface{}) Context { | ||
return context.WithValue(parent, key, val) | ||
} | ||
|
||
// stringMapContext is a simple context implementation that checks a map for a | ||
// key, falling back to a parent if not present. | ||
type stringMapContext struct { | ||
context.Context | ||
m map[string]interface{} | ||
} | ||
|
||
// WithValues returns a context that proxies lookups through a map. Only | ||
// supports string keys. | ||
func WithValues(ctx context.Context, m map[string]interface{}) context.Context { | ||
mo := make(map[string]interface{}, len(m)) // make our own copy. | ||
for k, v := range m { | ||
mo[k] = v | ||
} | ||
|
||
return stringMapContext{ | ||
Context: ctx, | ||
m: mo, | ||
} | ||
} | ||
|
||
func (smc stringMapContext) Value(key interface{}) interface{} { | ||
if ks, ok := key.(string); ok { | ||
if v, ok := smc.m[ks]; ok { | ||
return v | ||
} | ||
} | ||
|
||
return smc.Context.Value(key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters