Skip to content

Commit 0731959

Browse files
committed
Object method to get real object
Signed-off-by: Vishal Rana <[email protected]>
1 parent 3f48b92 commit 0731959

File tree

10 files changed

+55
-41
lines changed

10 files changed

+55
-41
lines changed

context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type (
4747
Redirect(int, string) error
4848
Error(err error)
4949
Logger() logger.Logger
50-
Context() *context
50+
Object() *context
5151
}
5252

5353
context struct {
@@ -307,8 +307,8 @@ func (c *context) Logger() logger.Logger {
307307
return c.echo.logger
308308
}
309309

310-
// Context returns the `context` instance.
311-
func (c *context) Context() *context {
310+
// Object returns the `context` object.
311+
func (c *context) Object() *context {
312312
return c
313313
}
314314

context_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func TestContext(t *testing.T) {
5151
assert.Nil(t, c.Socket())
5252

5353
// Param by id
54-
c.Context().pnames = []string{"id"}
55-
c.Context().pvalues = []string{"1"}
54+
c.Object().pnames = []string{"id"}
55+
c.Object().pvalues = []string{"1"}
5656
assert.Equal(t, "1", c.P(0))
5757

5858
// Param by name
@@ -68,13 +68,13 @@ func TestContext(t *testing.T) {
6868

6969
// JSON
7070
testBindOk(t, c, ApplicationJSON)
71-
c.Context().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
71+
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
7272
testBindError(t, c, ApplicationJSON)
7373

7474
// XML
75-
c.Context().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
75+
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
7676
testBindOk(t, c, ApplicationXML)
77-
c.Context().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
77+
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
7878
testBindError(t, c, ApplicationXML)
7979

8080
// Unsupported
@@ -87,14 +87,14 @@ func TestContext(t *testing.T) {
8787
tpl := &Template{
8888
templates: template.Must(template.New("hello").Parse("Hello, {{.}}!")),
8989
}
90-
c.Context().echo.SetRenderer(tpl)
90+
c.Object().echo.SetRenderer(tpl)
9191
err := c.Render(http.StatusOK, "hello", "Joe")
9292
if assert.NoError(t, err) {
9393
assert.Equal(t, http.StatusOK, rec.Status())
9494
assert.Equal(t, "Hello, Joe!", rec.Body.String())
9595
}
9696

97-
c.Context().echo.renderer = nil
97+
c.Object().echo.renderer = nil
9898
err = c.Render(http.StatusOK, "hello", "Joe")
9999
assert.Error(t, err)
100100

@@ -226,12 +226,12 @@ func TestContext(t *testing.T) {
226226

227227
// Error
228228
rec = test.NewResponseRecorder()
229-
c = NewContext(req, rec, e).Context()
229+
c = NewContext(req, rec, e).Object()
230230
c.Error(errors.New("error"))
231231
assert.Equal(t, http.StatusInternalServerError, c.Response().Status())
232232

233233
// reset
234-
c.Context().reset(req, test.NewResponseRecorder(), e)
234+
c.Object().reset(req, test.NewResponseRecorder(), e)
235235
}
236236

237237
func TestContextPath(t *testing.T) {

engine/engine.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@ type (
1515
}
1616

1717
Request interface {
18+
URI() string
19+
URL() URL
1820
Header() Header
1921
// Proto() string
2022
// ProtoMajor() int
2123
// ProtoMinor() int
2224
RemoteAddress() string
2325
Method() string
24-
URI() string
25-
URL() URL
2626
Body() io.ReadCloser
2727
FormValue(string) string
28+
Object() interface{}
2829
}
2930

3031
Response interface {
@@ -36,6 +37,7 @@ type (
3637
Committed() bool
3738
SetWriter(io.Writer)
3839
Writer() io.Writer
40+
Object() interface{}
3941
}
4042

4143
Header interface {

engine/fasthttp/request.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,34 @@ import (
99

1010
type (
1111
Request struct {
12-
context *fasthttp.RequestCtx
12+
request *fasthttp.RequestCtx
1313
url engine.URL
1414
header engine.Header
1515
}
1616
)
1717

18-
func (r *Request) Header() engine.Header {
19-
return r.header
18+
func (r *Request) Object() interface{} {
19+
return r.request
2020
}
2121

22-
func (r *Request) RemoteAddress() string {
23-
return r.context.RemoteAddr().String()
22+
func (r *Request) URI() string {
23+
return string(r.request.RequestURI())
2424
}
2525

26-
func (r *Request) Method() string {
27-
return string(r.context.Method())
26+
func (r *Request) URL() engine.URL {
27+
return r.url
2828
}
2929

30-
func (r *Request) URI() string {
31-
return string(r.context.RequestURI())
30+
func (r *Request) Header() engine.Header {
31+
return r.header
3232
}
3333

34-
func (r *Request) URL() engine.URL {
35-
return r.url
34+
func (r *Request) RemoteAddress() string {
35+
return r.request.RemoteAddr().String()
36+
}
37+
38+
func (r *Request) Method() string {
39+
return string(r.request.Method())
3640
}
3741

3842
func (r *Request) Body() io.ReadCloser {

engine/fasthttp/response.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
type (
1111
Response struct {
12-
context *fasthttp.RequestCtx
12+
response *fasthttp.RequestCtx
1313
header engine.Header
1414
status int
1515
size int64
@@ -18,16 +18,20 @@ type (
1818
}
1919
)
2020

21+
func (r *Response) Object() interface{} {
22+
return r.response
23+
}
24+
2125
func (r *Response) Header() engine.Header {
2226
return r.header
2327
}
2428

2529
func (r *Response) WriteHeader(code int) {
26-
r.context.SetStatusCode(code)
30+
r.response.SetStatusCode(code)
2731
}
2832

2933
func (r *Response) Write(b []byte) (int, error) {
30-
return r.context.Write(b)
34+
return r.response.Write(b)
3135
}
3236

3337
func (r *Response) Status() int {

engine/fasthttp/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ func NewServer(c *engine.Config, h engine.HandlerFunc, l logger.Logger) *Server
2929
func (s *Server) Start() {
3030
fasthttp.ListenAndServe(s.config.Address, func(ctx *fasthttp.RequestCtx) {
3131
req := &Request{
32-
context: ctx,
32+
request: ctx,
3333
url: &URL{ctx.URI()},
3434
header: &RequestHeader{ctx.Request.Header},
3535
}
3636
res := &Response{
37-
context: ctx,
38-
header: &ResponseHeader{ctx.Response.Header},
37+
response: ctx,
38+
header: &ResponseHeader{ctx.Response.Header},
3939
}
4040
s.handler(req, res)
4141
})

engine/standard/request.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ func NewRequest(r *http.Request) *Request {
2323
}
2424
}
2525

26-
func (r *Request) Request() *http.Request {
26+
func (r *Request) Object() interface{} {
2727
return r.request
2828
}
2929

30-
func (r *Request) Header() engine.Header {
31-
return r.header
32-
}
33-
3430
func (r *Request) URL() engine.URL {
3531
return r.url
3632
}
3733

34+
func (r *Request) Header() engine.Header {
35+
return r.header
36+
}
37+
3838
func (r *Request) RemoteAddress() string {
3939
return r.request.RemoteAddr
4040
}

engine/standard/response.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ func NewResponse(w http.ResponseWriter, l logger.Logger) *Response {
2929
}
3030
}
3131

32+
func (r *Response) Object() interface{} {
33+
return r.response
34+
}
35+
3236
func (r *Response) Header() engine.Header {
3337
return r.header
3438
}

router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func (n *node) check405() HandlerFunc {
274274
}
275275

276276
func (r *Router) Find(method, path string, context Context) (h HandlerFunc, e *Echo) {
277-
x := context.Context()
277+
x := context.Object()
278278
h = notFoundHandler
279279
e = r.echo
280280
cn := r.tree // Current node as root

router_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,16 +529,16 @@ func TestRouterParamNames(t *testing.T) {
529529
// Route > /users/:id
530530
h, _ = r.Find(GET, "/users/1", c)
531531
if assert.NotNil(t, h) {
532-
assert.Equal(t, "id", c.Context().pnames[0])
532+
assert.Equal(t, "id", c.Object().pnames[0])
533533
assert.Equal(t, "1", c.P(0))
534534
}
535535

536536
// Route > /users/:uid/files/:fid
537537
h, _ = r.Find(GET, "/users/1/files/1", c)
538538
if assert.NotNil(t, h) {
539-
assert.Equal(t, "uid", c.Context().pnames[0])
539+
assert.Equal(t, "uid", c.Object().pnames[0])
540540
assert.Equal(t, "1", c.P(0))
541-
assert.Equal(t, "fid", c.Context().pnames[1])
541+
assert.Equal(t, "fid", c.Object().pnames[1])
542542
assert.Equal(t, "1", c.P(1))
543543
}
544544
}
@@ -556,7 +556,7 @@ func TestRouterAPI(t *testing.T) {
556556
for _, route := range api {
557557
h, _ := r.Find(route.Method, route.Path, c)
558558
if assert.NotNil(t, h) {
559-
for i, n := range c.Context().pnames {
559+
for i, n := range c.Object().pnames {
560560
if assert.NotEmpty(t, n) {
561561
assert.Equal(t, ":"+n, c.P(i))
562562
}

0 commit comments

Comments
 (0)