Skip to content

Commit

Permalink
fix: format code accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Mar 18, 2017
1 parent 24f33ad commit ab59b02
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 64 deletions.
106 changes: 53 additions & 53 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
package context

import (
"time"
"context"
"context"
"net/http"
"net/url"
"time"

"gopkg.in/h2non/gentleman.v2/utils"
)
Expand Down Expand Up @@ -56,28 +56,28 @@ func New() *Context {

// getStore retrieves the current request context data store.
func (c *Context) getStore() Store {
store, ok := c.Request.Context().Value(Key).(Store)
if !ok {
panic("invalid request context")
}
store, ok := c.Request.Context().Value(Key).(Store)
if !ok {
panic("invalid request context")
}
return store
}

// Set sets a value on the current store
func (c *Context) Set(key interface{}, value interface{}) {
store := c.getStore()
store[key] = value
store[key] = value
}

// Get gets a value by key in the current or parent context
func (c *Context) Get(key interface{}) interface{} {
store := c.getStore()
if store == nil {
return store
}
if value, ok := store[key]; ok {
return value
}
return store
}
if value, ok := store[key]; ok {
return value
}
if c.Parent != nil {
return c.Parent.Get(key)
}
Expand All @@ -89,11 +89,11 @@ func (c *Context) Get(key interface{}) interface{} {
func (c *Context) GetOk(key interface{}) (interface{}, bool) {
store := c.getStore()
val, ok := store[key]
if !ok {
if c.Parent != nil {
return c.Parent.GetOk(key)
}
}
if !ok {
if c.Parent != nil {
return c.Parent.GetOk(key)
}
}
return val, ok
}

Expand All @@ -102,11 +102,11 @@ func (c *Context) GetOk(key interface{}) (interface{}, bool) {
// or the value does not evaluate to a string
func (c *Context) GetInt(key interface{}) (int, bool) {
value, ok := c.GetOk(key)
if !ok {
if c.Parent != nil {
return c.Parent.GetInt(key)
}
}
if !ok {
if c.Parent != nil {
return c.Parent.GetInt(key)
}
}
if num, ok := value.(int); ok {
return num, ok
}
Expand All @@ -123,25 +123,25 @@ func (c *Context) GetString(key interface{}) string {
return typed
}
}
if c.Parent != nil {
return c.Parent.GetString(key)
}
if c.Parent != nil {
return c.Parent.GetString(key)
}
return ""
}

// GetAll returns all stored context values for a request.
// Will always return a valid map. Returns an empty map for
// requests context data previously set
func (c *Context) GetAll() Store {
buf := Store{}
for key, value := range c.getStore() {
buf[key] = value
}
if c.Parent != nil {
for key, value := range c.Parent.GetAll() {
buf[key] = value
}
}
buf := Store{}
for key, value := range c.getStore() {
buf[key] = value
}
if c.Parent != nil {
for key, value := range c.Parent.GetAll() {
buf[key] = value
}
}
return buf
}

Expand All @@ -153,10 +153,10 @@ func (c *Context) Delete(key interface{}) {
// Clear clears all stored values in the current request’s context.
// Parent context store will not be cleaned.
func (c *Context) Clear() {
store := c.getStore()
for key := range store {
delete(store, key)
}
store := c.getStore()
for key := range store {
delete(store, key)
}
}

// UseParent uses a new parent Context
Expand Down Expand Up @@ -185,7 +185,7 @@ func (c *Context) Clone() *Context {

req := new(http.Request)
*req = *c.Request
ctx.Request = req
ctx.Request = req
c.CopyTo(ctx)

res := new(http.Response)
Expand All @@ -197,21 +197,21 @@ func (c *Context) Clone() *Context {

// CopyTo copies the current context store into a new Context.
func (c *Context) CopyTo(newCtx *Context) {
store := Store{}
store := Store{}

for key, value := range c.getStore() {
store[key] = value
}
for key, value := range c.getStore() {
store[key] = value
}

ctx := context.WithValue(context.Background(), Key, store)
newCtx.Request = newCtx.Request.WithContext(ctx)
ctx := context.WithValue(context.Background(), Key, store)
newCtx.Request = newCtx.Request.WithContext(ctx)
}

// Deadline returns the time when work done on behalf of this context
// should be canceled. Deadline returns ok==false when no deadline is
// set. Successive calls to Deadline return the same results.
func (c *Context) Deadline() (deadline time.Time, ok bool) {
return c.Request.Context().Deadline()
return c.Request.Context().Deadline()
}

// Done returns a channel that's closed when work done on behalf of this
Expand Down Expand Up @@ -244,15 +244,15 @@ func (c *Context) Deadline() (deadline time.Time, ok bool) {
// See https://blog.golang.org/pipelines for more examples of how to use
// a Done channel for cancelation.
func (c *Context) Done() <-chan struct{} {
return c.Request.Context().Done()
return c.Request.Context().Done()
}

// Err returns a non-nil error value after Done is closed. Err returns
// Canceled if the context was canceled or DeadlineExceeded if the
// context's deadline passed. No other values for Err are defined.
// After Done is closed, successive calls to Err return the same value.
func (c *Context) Err() error {
return c.Request.Context().Err()
return c.Request.Context().Err()
}

// Value returns the value associated with this context for key, or nil
Expand Down Expand Up @@ -301,17 +301,17 @@ func (c *Context) Err() error {
// return u, ok
// }
func (c *Context) Value(key interface{}) interface{} {
return c.Request.Context().Value(key)
return c.Request.Context().Value(key)
}

// emptyContext creates a new empty context.Context
func emptyContext() context.Context {
return context.WithValue(context.Background(), Key, Store{})
return context.WithValue(context.Background(), Key, Store{})
}

// createRequest creates a default http.Request instance.
func createRequest() *http.Request {
// Create HTTP request
// Create HTTP request
req := &http.Request{
Method: "GET",
URL: &url.URL{},
Expand All @@ -322,8 +322,8 @@ func createRequest() *http.Request {
Header: make(http.Header),
Body: utils.NopCloser(),
}
// Return shallow copy of Request with the new context
return req.WithContext(emptyContext())
// Return shallow copy of Request with the new context
return req.WithContext(emptyContext())
}

// createResponse creates a default http.Response instance.
Expand Down
22 changes: 11 additions & 11 deletions context/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestContext(t *testing.T) {
ctx.Set(key1, "1")
st.Expect(t, ctx.Get(key1), "1")
st.Expect(t, len(store), 1)
st.Expect(t, store[key1], "1")
st.Expect(t, store[key1], "1")

ctx.Set(key2, "2")
st.Expect(t, ctx.Get(key2), "2")
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestContextGetAll(t *testing.T) {
st.Expect(t, ctx.Get("bar"), "foo")

store := ctx.GetAll()
st.Expect(t, len(store), 2)
st.Expect(t, len(store), 2)
}

func TestContextRoot(t *testing.T) {
Expand All @@ -125,19 +125,19 @@ func TestContextGetters(t *testing.T) {
ctx.Set("bar", "foo")
st.Expect(t, ctx.GetString("foo"), "bar")
st.Expect(t, ctx.GetString("bar"), "foo")
ctx.Clear()
ctx.Clear()

parent.Set("foo", 1)
parent.Set("foo", 1)
ctx.Set("bar", 2)
foo, ok := ctx.GetInt("foo")
st.Expect(t, ok, true)
st.Expect(t, foo, 1)
bar, ok := ctx.GetInt("bar")
st.Expect(t, ok, true)
foo, ok := ctx.GetInt("foo")
st.Expect(t, ok, true)
st.Expect(t, foo, 1)
bar, ok := ctx.GetInt("bar")
st.Expect(t, ok, true)
st.Expect(t, bar, 2)

store := ctx.GetAll()
st.Expect(t, len(store), 2)
store := ctx.GetAll()
st.Expect(t, len(store), 2)
}

func TestContextClone(t *testing.T) {
Expand Down

0 comments on commit ab59b02

Please sign in to comment.