Skip to content

Commit ac0e6a2

Browse files
feat(context): support chaining
1 parent c3d5a28 commit ac0e6a2

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
@@ -272,14 +272,15 @@ func (c *Context) Error(err error) *Error {
272272

273273
// Set is used to store a new key/value pair exclusively for this context.
274274
// It also lazy initializes c.Keys if it was not used previously.
275-
func (c *Context) Set(key any, value any) {
275+
func (c *Context) Set(key any, value any) *Context {
276276
c.mu.Lock()
277277
defer c.mu.Unlock()
278278
if c.Keys == nil {
279279
c.Keys = make(map[any]any)
280280
}
281281

282282
c.Keys[key] = value
283+
return c
283284
}
284285

285286
// Get returns the value for the given key, ie: (value, true).
@@ -498,8 +499,9 @@ func (c *Context) Param(key string) string {
498499
// Example Route: "/user/:id"
499500
// AddParam("id", 1)
500501
// Result: "/user/1"
501-
func (c *Context) AddParam(key, value string) {
502+
func (c *Context) AddParam(key, value string) *Context {
502503
c.Params = append(c.Params, Param{Key: key, Value: value})
504+
return c
503505
}
504506

505507
// Query returns the keyed url query value if it exists,
@@ -1014,19 +1016,21 @@ func bodyAllowedForStatus(status int) bool {
10141016
}
10151017

10161018
// Status sets the HTTP response code.
1017-
func (c *Context) Status(code int) {
1019+
func (c *Context) Status(code int) *Context {
10181020
c.Writer.WriteHeader(code)
1021+
return c
10191022
}
10201023

10211024
// Header is an intelligent shortcut for c.Writer.Header().Set(key, value).
10221025
// It writes a header in the response.
10231026
// If value == "", this method removes the header `c.Writer.Header().Del(key)`
1024-
func (c *Context) Header(key, value string) {
1027+
func (c *Context) Header(key, value string) *Context {
10251028
if value == "" {
10261029
c.Writer.Header().Del(key)
1027-
return
1030+
return c
10281031
}
10291032
c.Writer.Header().Set(key, value)
1033+
return c
10301034
}
10311035

10321036
// GetHeader returns value from request headers.
@@ -1043,14 +1047,15 @@ func (c *Context) GetRawData() ([]byte, error) {
10431047
}
10441048

10451049
// SetSameSite with cookie
1046-
func (c *Context) SetSameSite(samesite http.SameSite) {
1050+
func (c *Context) SetSameSite(samesite http.SameSite) *Context {
10471051
c.sameSite = samesite
1052+
return c
10481053
}
10491054

10501055
// SetCookie adds a Set-Cookie header to the ResponseWriter's headers.
10511056
// The provided cookie must have a valid Name. Invalid cookies may be
10521057
// silently dropped.
1053-
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) {
1058+
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) *Context {
10541059
if path == "" {
10551060
path = "/"
10561061
}
@@ -1064,19 +1069,21 @@ func (c *Context) SetCookie(name, value string, maxAge int, path, domain string,
10641069
Secure: secure,
10651070
HttpOnly: httpOnly,
10661071
})
1072+
return c
10671073
}
10681074

10691075
// SetCookieData adds a Set-Cookie header to the ResponseWriter's headers.
10701076
// It accepts a pointer to http.Cookie structure for more flexibility in setting cookie attributes.
10711077
// The provided cookie must have a valid Name. Invalid cookies may be silently dropped.
1072-
func (c *Context) SetCookieData(cookie *http.Cookie) {
1078+
func (c *Context) SetCookieData(cookie *http.Cookie) *Context {
10731079
if cookie.Path == "" {
10741080
cookie.Path = "/"
10751081
}
10761082
if cookie.SameSite == http.SameSiteDefaultMode {
10771083
cookie.SameSite = c.sameSite
10781084
}
10791085
http.SetCookie(c.Writer, cookie)
1086+
return c
10801087
}
10811088

10821089
// Cookie returns the named cookie provided in the request or
@@ -1356,8 +1363,9 @@ func (c *Context) NegotiateFormat(offered ...string) string {
13561363
}
13571364

13581365
// SetAccepted sets Accept header data.
1359-
func (c *Context) SetAccepted(formats ...string) {
1366+
func (c *Context) SetAccepted(formats ...string) *Context {
13601367
c.Accepted = formats
1368+
return c
13611369
}
13621370

13631371
/************************************/

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)