Skip to content

Commit c4d70a0

Browse files
authored
Rename ctx.Form() to ctx.FormString() and move code into own file (#16571)
Followup from #16562 prepare for #16567 * Rename ctx.Form() to ctx.FormString() * Reimplement FormX func to need less code and cpu cycles * Move code into own file
1 parent 2eeae4e commit c4d70a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+236
-449
lines changed

modules/context/context.go

-36
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"code.gitea.io/gitea/modules/setting"
2929
"code.gitea.io/gitea/modules/templates"
3030
"code.gitea.io/gitea/modules/translation"
31-
"code.gitea.io/gitea/modules/util"
3231
"code.gitea.io/gitea/modules/web/middleware"
3332
"code.gitea.io/gitea/services/auth"
3433

@@ -287,41 +286,6 @@ func (ctx *Context) Header() http.Header {
287286
return ctx.Resp.Header()
288287
}
289288

290-
// Form returns request form as string with default
291-
func (ctx *Context) Form(key string, defaults ...string) string {
292-
return (*Forms)(ctx.Req).MustString(key, defaults...)
293-
}
294-
295-
// FormTrim returns request form as string with default and trimmed spaces
296-
func (ctx *Context) FormTrim(key string, defaults ...string) string {
297-
return (*Forms)(ctx.Req).MustTrimmed(key, defaults...)
298-
}
299-
300-
// FormStrings returns request form as strings with default
301-
func (ctx *Context) FormStrings(key string, defaults ...[]string) []string {
302-
return (*Forms)(ctx.Req).MustStrings(key, defaults...)
303-
}
304-
305-
// FormInt returns request form as int with default
306-
func (ctx *Context) FormInt(key string, defaults ...int) int {
307-
return (*Forms)(ctx.Req).MustInt(key, defaults...)
308-
}
309-
310-
// FormInt64 returns request form as int64 with default
311-
func (ctx *Context) FormInt64(key string, defaults ...int64) int64 {
312-
return (*Forms)(ctx.Req).MustInt64(key, defaults...)
313-
}
314-
315-
// FormBool returns request form as bool with default
316-
func (ctx *Context) FormBool(key string, defaults ...bool) bool {
317-
return (*Forms)(ctx.Req).MustBool(key, defaults...)
318-
}
319-
320-
// FormOptionalBool returns request form as OptionalBool with default
321-
func (ctx *Context) FormOptionalBool(key string, defaults ...util.OptionalBool) util.OptionalBool {
322-
return (*Forms)(ctx.Req).MustOptionalBool(key, defaults...)
323-
}
324-
325289
// HandleText handles HTTP status code
326290
func (ctx *Context) HandleText(status int, title string) {
327291
if (status/100 == 4) || (status/100 == 5) {

modules/context/form.go

+27-204
Original file line numberDiff line numberDiff line change
@@ -5,237 +5,60 @@
55
package context
66

77
import (
8-
"errors"
9-
"net/http"
10-
"net/url"
118
"strconv"
129
"strings"
13-
"text/template"
1410

15-
"code.gitea.io/gitea/modules/log"
1611
"code.gitea.io/gitea/modules/util"
1712
)
1813

19-
// Forms a new enhancement of http.Request
20-
type Forms http.Request
21-
22-
// Values returns http.Request values
23-
func (f *Forms) Values() url.Values {
24-
return (*http.Request)(f).Form
25-
}
26-
27-
// String returns request form as string
28-
func (f *Forms) String(key string) (string, error) {
29-
return (*http.Request)(f).FormValue(key), nil
30-
}
31-
32-
// Trimmed returns request form as string with trimed spaces left and right
33-
func (f *Forms) Trimmed(key string) (string, error) {
34-
return strings.TrimSpace((*http.Request)(f).FormValue(key)), nil
14+
// FormString returns the first value matching the provided key in the form as a string
15+
func (ctx *Context) FormString(key string) string {
16+
return ctx.Req.FormValue(key)
3517
}
3618

37-
// Strings returns request form as strings
38-
func (f *Forms) Strings(key string) ([]string, error) {
39-
if (*http.Request)(f).Form == nil {
40-
if err := (*http.Request)(f).ParseMultipartForm(32 << 20); err != nil {
41-
return nil, err
19+
// FormStrings returns a string slice for the provided key from the form
20+
func (ctx *Context) FormStrings(key string) []string {
21+
if ctx.Req.Form == nil {
22+
if err := ctx.Req.ParseMultipartForm(32 << 20); err != nil {
23+
return nil
4224
}
4325
}
44-
if v, ok := (*http.Request)(f).Form[key]; ok {
45-
return v, nil
46-
}
47-
return nil, errors.New("not exist")
48-
}
49-
50-
// Escape returns request form as escaped string
51-
func (f *Forms) Escape(key string) (string, error) {
52-
return template.HTMLEscapeString((*http.Request)(f).FormValue(key)), nil
53-
}
54-
55-
// Int returns request form as int
56-
func (f *Forms) Int(key string) (int, error) {
57-
return strconv.Atoi((*http.Request)(f).FormValue(key))
58-
}
59-
60-
// Int32 returns request form as int32
61-
func (f *Forms) Int32(key string) (int32, error) {
62-
v, err := strconv.ParseInt((*http.Request)(f).FormValue(key), 10, 32)
63-
return int32(v), err
64-
}
65-
66-
// Int64 returns request form as int64
67-
func (f *Forms) Int64(key string) (int64, error) {
68-
return strconv.ParseInt((*http.Request)(f).FormValue(key), 10, 64)
69-
}
70-
71-
// Uint returns request form as uint
72-
func (f *Forms) Uint(key string) (uint, error) {
73-
v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 64)
74-
return uint(v), err
75-
}
76-
77-
// Uint32 returns request form as uint32
78-
func (f *Forms) Uint32(key string) (uint32, error) {
79-
v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 32)
80-
return uint32(v), err
81-
}
82-
83-
// Uint64 returns request form as uint64
84-
func (f *Forms) Uint64(key string) (uint64, error) {
85-
return strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 64)
86-
}
87-
88-
// Bool returns request form as bool
89-
func (f *Forms) Bool(key string) (bool, error) {
90-
return strconv.ParseBool((*http.Request)(f).FormValue(key))
91-
}
92-
93-
// Float32 returns request form as float32
94-
func (f *Forms) Float32(key string) (float32, error) {
95-
v, err := strconv.ParseFloat((*http.Request)(f).FormValue(key), 64)
96-
return float32(v), err
97-
}
98-
99-
// Float64 returns request form as float64
100-
func (f *Forms) Float64(key string) (float64, error) {
101-
return strconv.ParseFloat((*http.Request)(f).FormValue(key), 64)
102-
}
103-
104-
// MustString returns request form as string with default
105-
func (f *Forms) MustString(key string, defaults ...string) string {
106-
if v := (*http.Request)(f).FormValue(key); len(v) > 0 {
26+
if v, ok := ctx.Req.Form[key]; ok {
10727
return v
10828
}
109-
if len(defaults) > 0 {
110-
return defaults[0]
111-
}
112-
return ""
29+
return nil
11330
}
11431

115-
// MustTrimmed returns request form as string with default
116-
func (f *Forms) MustTrimmed(key string, defaults ...string) string {
117-
return strings.TrimSpace(f.MustString(key, defaults...))
32+
// FormTrim returns the first value for the provided key in the form as a space trimmed string
33+
func (ctx *Context) FormTrim(key string) string {
34+
return strings.TrimSpace(ctx.Req.FormValue(key))
11835
}
11936

120-
// MustStrings returns request form as strings with default
121-
func (f *Forms) MustStrings(key string, defaults ...[]string) []string {
122-
if (*http.Request)(f).Form == nil {
123-
if err := (*http.Request)(f).ParseMultipartForm(32 << 20); err != nil {
124-
log.Error("ParseMultipartForm: %v", err)
125-
return []string{}
126-
}
127-
}
128-
129-
if v, ok := (*http.Request)(f).Form[key]; ok {
130-
return v
131-
}
132-
if len(defaults) > 0 {
133-
return defaults[0]
134-
}
135-
return []string{}
136-
}
137-
138-
// MustEscape returns request form as escaped string with default
139-
func (f *Forms) MustEscape(key string, defaults ...string) string {
140-
if v := (*http.Request)(f).FormValue(key); len(v) > 0 {
141-
return template.HTMLEscapeString(v)
142-
}
143-
if len(defaults) > 0 {
144-
return defaults[0]
145-
}
146-
return ""
147-
}
148-
149-
// MustInt returns request form as int with default
150-
func (f *Forms) MustInt(key string, defaults ...int) int {
151-
v, err := strconv.Atoi((*http.Request)(f).FormValue(key))
152-
if len(defaults) > 0 && err != nil {
153-
return defaults[0]
154-
}
155-
return v
156-
}
157-
158-
// MustInt32 returns request form as int32 with default
159-
func (f *Forms) MustInt32(key string, defaults ...int32) int32 {
160-
v, err := strconv.ParseInt((*http.Request)(f).FormValue(key), 10, 32)
161-
if len(defaults) > 0 && err != nil {
162-
return defaults[0]
163-
}
164-
return int32(v)
165-
}
166-
167-
// MustInt64 returns request form as int64 with default
168-
func (f *Forms) MustInt64(key string, defaults ...int64) int64 {
169-
v, err := strconv.ParseInt((*http.Request)(f).FormValue(key), 10, 64)
170-
if len(defaults) > 0 && err != nil {
171-
return defaults[0]
172-
}
37+
// FormInt returns the first value for the provided key in the form as an int
38+
func (ctx *Context) FormInt(key string) int {
39+
v, _ := strconv.Atoi(ctx.Req.FormValue(key))
17340
return v
17441
}
17542

176-
// MustUint returns request form as uint with default
177-
func (f *Forms) MustUint(key string, defaults ...uint) uint {
178-
v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 64)
179-
if len(defaults) > 0 && err != nil {
180-
return defaults[0]
181-
}
182-
return uint(v)
183-
}
184-
185-
// MustUint32 returns request form as uint32 with default
186-
func (f *Forms) MustUint32(key string, defaults ...uint32) uint32 {
187-
v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 32)
188-
if len(defaults) > 0 && err != nil {
189-
return defaults[0]
190-
}
191-
return uint32(v)
192-
}
193-
194-
// MustUint64 returns request form as uint64 with default
195-
func (f *Forms) MustUint64(key string, defaults ...uint64) uint64 {
196-
v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 64)
197-
if len(defaults) > 0 && err != nil {
198-
return defaults[0]
199-
}
200-
return v
201-
}
202-
203-
// MustFloat32 returns request form as float32 with default
204-
func (f *Forms) MustFloat32(key string, defaults ...float32) float32 {
205-
v, err := strconv.ParseFloat((*http.Request)(f).FormValue(key), 32)
206-
if len(defaults) > 0 && err != nil {
207-
return defaults[0]
208-
}
209-
return float32(v)
210-
}
211-
212-
// MustFloat64 returns request form as float64 with default
213-
func (f *Forms) MustFloat64(key string, defaults ...float64) float64 {
214-
v, err := strconv.ParseFloat((*http.Request)(f).FormValue(key), 64)
215-
if len(defaults) > 0 && err != nil {
216-
return defaults[0]
217-
}
43+
// FormInt64 returns the first value for the provided key in the form as an int64
44+
func (ctx *Context) FormInt64(key string) int64 {
45+
v, _ := strconv.ParseInt(ctx.Req.FormValue(key), 10, 64)
21846
return v
21947
}
22048

221-
// MustBool returns request form as bool with default
222-
func (f *Forms) MustBool(key string, defaults ...bool) bool {
223-
v, err := strconv.ParseBool((*http.Request)(f).FormValue(key))
224-
if len(defaults) > 0 && err != nil {
225-
return defaults[0]
226-
}
49+
// FormBool returns true if the value for the provided key in the form is "1" or "true"
50+
func (ctx *Context) FormBool(key string) bool {
51+
v, _ := strconv.ParseBool(ctx.Req.FormValue(key))
22752
return v
22853
}
22954

230-
// MustOptionalBool returns request form as OptionalBool with default
231-
func (f *Forms) MustOptionalBool(key string, defaults ...util.OptionalBool) util.OptionalBool {
232-
value := (*http.Request)(f).FormValue(key)
55+
// FormOptionalBool returns an OptionalBoolTrue or OptionalBoolFalse if the value
56+
// for the provided key exists in the form else it returns OptionalBoolNone
57+
func (ctx *Context) FormOptionalBool(key string) util.OptionalBool {
58+
value := ctx.Req.FormValue(key)
23359
if len(value) == 0 {
23460
return util.OptionalBoolNone
23561
}
236-
v, err := strconv.ParseBool((*http.Request)(f).FormValue(key))
237-
if len(defaults) > 0 && err != nil {
238-
return defaults[0]
239-
}
62+
v, _ := strconv.ParseBool(ctx.Req.FormValue(key))
24063
return util.OptionalBoolOf(v)
24164
}

modules/context/repo.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func repoAssignment(ctx *Context, repo *models.Repository) {
346346

347347
// Check access.
348348
if ctx.Repo.Permission.AccessMode == models.AccessModeNone {
349-
if ctx.Form("go-get") == "1" {
349+
if ctx.FormString("go-get") == "1" {
350350
EarlyResponseForGoGetMeta(ctx)
351351
return
352352
}
@@ -415,7 +415,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
415415
owner, err = models.GetUserByName(userName)
416416
if err != nil {
417417
if models.IsErrUserNotExist(err) {
418-
if ctx.Form("go-get") == "1" {
418+
if ctx.FormString("go-get") == "1" {
419419
EarlyResponseForGoGetMeta(ctx)
420420
return
421421
}
@@ -437,7 +437,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
437437
if err == nil {
438438
RedirectToRepo(ctx, redirectRepoID)
439439
} else if models.IsErrRepoRedirectNotExist(err) {
440-
if ctx.Form("go-get") == "1" {
440+
if ctx.FormString("go-get") == "1" {
441441
EarlyResponseForGoGetMeta(ctx)
442442
return
443443
}
@@ -618,7 +618,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
618618
}
619619
}
620620

621-
if ctx.Form("go-get") == "1" {
621+
if ctx.FormString("go-get") == "1" {
622622
ctx.Data["GoGetImport"] = ComposeGoGetImport(owner.Name, repo.Name)
623623
prefix := setting.AppURL + path.Join(owner.Name, repo.Name, "src", "branch", ctx.Repo.BranchName)
624624
ctx.Data["GoDocDirectory"] = prefix + "{/dir}"

routers/api/v1/admin/adopt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func ListUnadoptedRepositories(ctx *context.APIContext) {
4242
// "$ref": "#/responses/forbidden"
4343

4444
listOptions := utils.GetListOptions(ctx)
45-
repoNames, count, err := repository.ListUnadoptedRepositories(ctx.Form("query"), &listOptions)
45+
repoNames, count, err := repository.ListUnadoptedRepositories(ctx.FormString("query"), &listOptions)
4646
if err != nil {
4747
ctx.InternalServerError(err)
4848
}

routers/api/v1/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ import (
9393

9494
func sudo() func(ctx *context.APIContext) {
9595
return func(ctx *context.APIContext) {
96-
sudo := ctx.Form("sudo")
96+
sudo := ctx.FormString("sudo")
9797
if len(sudo) == 0 {
9898
sudo = ctx.Req.Header.Get("Sudo")
9999
}

routers/api/v1/notify/repo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func ReadRepoNotifications(ctx *context.APIContext) {
171171
// "$ref": "#/responses/empty"
172172

173173
lastRead := int64(0)
174-
qLastRead := strings.Trim(ctx.Form("last_read_at"), " ")
174+
qLastRead := strings.Trim(ctx.FormString("last_read_at"), " ")
175175
if len(qLastRead) > 0 {
176176
tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
177177
if err != nil {
@@ -200,7 +200,7 @@ func ReadRepoNotifications(ctx *context.APIContext) {
200200
return
201201
}
202202

203-
targetStatus := statusStringToNotificationStatus(ctx.Form("to-status"))
203+
targetStatus := statusStringToNotificationStatus(ctx.FormString("to-status"))
204204
if targetStatus == 0 {
205205
targetStatus = models.NotificationStatusRead
206206
}

routers/api/v1/notify/threads.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func ReadThread(ctx *context.APIContext) {
8282
return
8383
}
8484

85-
targetStatus := statusStringToNotificationStatus(ctx.Form("to-status"))
85+
targetStatus := statusStringToNotificationStatus(ctx.FormString("to-status"))
8686
if targetStatus == 0 {
8787
targetStatus = models.NotificationStatusRead
8888
}

0 commit comments

Comments
 (0)