Skip to content

Commit 322da9c

Browse files
feat(context): support chaining
1 parent e88fc89 commit 322da9c

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

context.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,15 @@ func (c *Context) Error(err error) *Error {
280280

281281
// Set is used to store a new key/value pair exclusively for this context.
282282
// It also lazy initializes c.Keys if it was not used previously.
283-
func (c *Context) Set(key any, value any) {
283+
func (c *Context) Set(key any, value any) *Context {
284284
c.mu.Lock()
285285
defer c.mu.Unlock()
286286
if c.Keys == nil {
287287
c.Keys = make(map[any]any)
288288
}
289289

290290
c.Keys[key] = value
291+
return c
291292
}
292293

293294
// Get returns the value for the given key, ie: (value, true).
@@ -506,8 +507,9 @@ func (c *Context) Param(key string) string {
506507
// Example Route: "/user/:id"
507508
// AddParam("id", 1)
508509
// Result: "/user/1"
509-
func (c *Context) AddParam(key, value string) {
510+
func (c *Context) AddParam(key, value string) *Context {
510511
c.Params = append(c.Params, Param{Key: key, Value: value})
512+
return c
511513
}
512514

513515
// Query returns the keyed url query value if it exists,
@@ -1052,19 +1054,21 @@ func bodyAllowedForStatus(status int) bool {
10521054
}
10531055

10541056
// Status sets the HTTP response code.
1055-
func (c *Context) Status(code int) {
1057+
func (c *Context) Status(code int) *Context {
10561058
c.Writer.WriteHeader(code)
1059+
return c
10571060
}
10581061

10591062
// Header is an intelligent shortcut for c.Writer.Header().Set(key, value).
10601063
// It writes a header in the response.
10611064
// If value == "", this method removes the header `c.Writer.Header().Del(key)`
1062-
func (c *Context) Header(key, value string) {
1065+
func (c *Context) Header(key, value string) *Context {
10631066
if value == "" {
10641067
c.Writer.Header().Del(key)
1065-
return
1068+
return c
10661069
}
10671070
c.Writer.Header().Set(key, value)
1071+
return c
10681072
}
10691073

10701074
// GetHeader returns value from request headers.
@@ -1081,14 +1085,15 @@ func (c *Context) GetRawData() ([]byte, error) {
10811085
}
10821086

10831087
// SetSameSite with cookie
1084-
func (c *Context) SetSameSite(samesite http.SameSite) {
1088+
func (c *Context) SetSameSite(samesite http.SameSite) *Context {
10851089
c.sameSite = samesite
1090+
return c
10861091
}
10871092

10881093
// SetCookie adds a Set-Cookie header to the ResponseWriter's headers.
10891094
// The provided cookie must have a valid Name. Invalid cookies may be
10901095
// silently dropped.
1091-
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) {
1096+
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) *Context {
10921097
if path == "" {
10931098
path = "/"
10941099
}
@@ -1102,19 +1107,21 @@ func (c *Context) SetCookie(name, value string, maxAge int, path, domain string,
11021107
Secure: secure,
11031108
HttpOnly: httpOnly,
11041109
})
1110+
return c
11051111
}
11061112

11071113
// SetCookieData adds a Set-Cookie header to the ResponseWriter's headers.
11081114
// It accepts a pointer to http.Cookie structure for more flexibility in setting cookie attributes.
11091115
// The provided cookie must have a valid Name. Invalid cookies may be silently dropped.
1110-
func (c *Context) SetCookieData(cookie *http.Cookie) {
1116+
func (c *Context) SetCookieData(cookie *http.Cookie) *Context {
11111117
if cookie.Path == "" {
11121118
cookie.Path = "/"
11131119
}
11141120
if cookie.SameSite == http.SameSiteDefaultMode {
11151121
cookie.SameSite = c.sameSite
11161122
}
11171123
http.SetCookie(c.Writer, cookie)
1124+
return c
11181125
}
11191126

11201127
// Cookie returns the named cookie provided in the request or
@@ -1394,8 +1401,9 @@ func (c *Context) NegotiateFormat(offered ...string) string {
13941401
}
13951402

13961403
// SetAccepted sets Accept header data.
1397-
func (c *Context) SetAccepted(formats ...string) {
1404+
func (c *Context) SetAccepted(formats ...string) *Context {
13981405
c.Accepted = formats
1406+
return c
13991407
}
14001408

14011409
/************************************/

context_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3677,3 +3677,46 @@ func BenchmarkGetMapFromFormData(b *testing.B) {
36773677
})
36783678
}
36793679
}
3680+
3681+
func TestContextChaining(t *testing.T) {
3682+
c, _ := CreateTestContext(httptest.NewRecorder())
3683+
// Basic cookie settings
3684+
cookie := &http.Cookie{
3685+
Name: "name",
3686+
Value: "gin",
3687+
MaxAge: 1,
3688+
Path: "/",
3689+
Domain: "localhost",
3690+
Secure: true,
3691+
HttpOnly: true,
3692+
}
3693+
3694+
c.Set("foo", "bar").
3695+
AddParam("id", "1").
3696+
SetSameSite(http.SameSiteLaxMode).
3697+
SetCookie("user", "gin", 1, "/", "localhost", true, true).
3698+
SetCookieData(cookie).
3699+
Header("Content-Type", "text/plain").
3700+
Header("X-Custom", "value").
3701+
SetAccepted(MIMEJSON, MIMEXML).
3702+
Status(200)
3703+
3704+
value, err := c.Get("foo")
3705+
assert.Equal(t, "bar", value)
3706+
assert.True(t, err)
3707+
3708+
v, ok := c.Params.Get("id")
3709+
assert.True(t, ok)
3710+
assert.Equal(t, "1", v)
3711+
3712+
assert.Equal(t, []string{"user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure; SameSite=Lax", "name=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure"}, c.Writer.Header().Values("Set-Cookie"))
3713+
3714+
assert.Equal(t, "text/plain", c.Writer.Header().Get("Content-Type"))
3715+
assert.Equal(t, "value", c.Writer.Header().Get("X-Custom"))
3716+
3717+
assert.Equal(t, MIMEJSON, c.NegotiateFormat(MIMEJSON, MIMEXML)) //nolint:testifylint
3718+
assert.Equal(t, MIMEXML, c.NegotiateFormat(MIMEXML, MIMEHTML))
3719+
assert.Equal(t, MIMEJSON, c.NegotiateFormat(MIMEJSON)) //nolint:testifylint
3720+
3721+
assert.Equal(t, 200, c.Writer.Status())
3722+
}

0 commit comments

Comments
 (0)