-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Rename ctx.Form() to ctx.FormString() and move code into own file #16571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
6543
merged 21 commits into
go-gitea:main
from
6543-forks:followup-from-16562-prepare-for-16567
Aug 11, 2021
Merged
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
de47d7a
Rename ctx.Form() to ctx.FormString()
6543 ba9a3df
move ctx.FormX to own file and delete unused code
6543 e8e335f
Update modules/context/form.go
6543 275780a
revert unrelated change
6543 a7e0c9f
Merge branch 'main' into followup-from-16562-prepare-for-16567
6543 bffe40d
enhance func desc
6543 e8cbded
Merge branch 'main' into followup-from-16562-prepare-for-16567
6543 d86ceee
Merge branch 'main' into followup-from-16562-prepare-for-16567
6543 9195706
Merge branch 'main' into followup-from-16562-prepare-for-16567
6543 a9f6723
Merge branch 'main' into followup-from-16562-prepare-for-16567
6543 a91a0b3
Merge branch 'main' into followup-from-16562-prepare-for-16567
6543 86d3ec0
Merge branch 'main' into followup-from-16562-prepare-for-16567
6543 1e1da67
Apply suggestions from code review
6543 a71e28e
format fix
zeripath 1e0eac2
Merge branch 'main' into followup-from-16562-prepare-for-16567
6543 e83e9ac
Merge branch 'main' into followup-from-16562-prepare-for-16567
6543 ab30a6f
Merge branch 'main' into followup-from-16562-prepare-for-16567
6543 8e3fb62
Merge branch 'main' into followup-from-16562-prepare-for-16567
6543 1e4f5e1
Merge branch 'main' into followup-from-16562-prepare-for-16567
6543 03b6dea
Merge branch 'master' into followup-from-16562-prepare-for-16567
6543 81cdb2a
Merge branch 'main' into followup-from-16562-prepare-for-16567
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,241 +1,64 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Copyright 2014 The Gogs Authors. All rights reserved. | ||
// Copyright 2020 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package context | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
"text/template" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/util" | ||
) | ||
|
||
// Forms a new enhancement of http.Request | ||
type Forms http.Request | ||
|
||
// Values returns http.Request values | ||
func (f *Forms) Values() url.Values { | ||
return (*http.Request)(f).Form | ||
} | ||
|
||
// String returns request form as string | ||
func (f *Forms) String(key string) (string, error) { | ||
return (*http.Request)(f).FormValue(key), nil | ||
} | ||
|
||
// Trimmed returns request form as string with trimed spaces left and right | ||
func (f *Forms) Trimmed(key string) (string, error) { | ||
return strings.TrimSpace((*http.Request)(f).FormValue(key)), nil | ||
// FormString returns fist value of a query based on key as string | ||
func (ctx *Context) FormString(key string) string { | ||
return ctx.Req.FormValue(key) | ||
} | ||
|
||
// Strings returns request form as strings | ||
func (f *Forms) Strings(key string) ([]string, error) { | ||
if (*http.Request)(f).Form == nil { | ||
if err := (*http.Request)(f).ParseMultipartForm(32 << 20); err != nil { | ||
return nil, err | ||
// FormStrings returns request form as strings | ||
func (ctx *Context) FormStrings(key string) []string { | ||
if ctx.Req.Form == nil { | ||
if err := ctx.Req.ParseMultipartForm(32 << 20); err != nil { | ||
return nil | ||
} | ||
} | ||
if v, ok := (*http.Request)(f).Form[key]; ok { | ||
return v, nil | ||
} | ||
return nil, errors.New("not exist") | ||
} | ||
|
||
// Escape returns request form as escaped string | ||
func (f *Forms) Escape(key string) (string, error) { | ||
return template.HTMLEscapeString((*http.Request)(f).FormValue(key)), nil | ||
} | ||
|
||
// Int returns request form as int | ||
func (f *Forms) Int(key string) (int, error) { | ||
return strconv.Atoi((*http.Request)(f).FormValue(key)) | ||
} | ||
|
||
// Int32 returns request form as int32 | ||
func (f *Forms) Int32(key string) (int32, error) { | ||
v, err := strconv.ParseInt((*http.Request)(f).FormValue(key), 10, 32) | ||
return int32(v), err | ||
} | ||
|
||
// Int64 returns request form as int64 | ||
func (f *Forms) Int64(key string) (int64, error) { | ||
return strconv.ParseInt((*http.Request)(f).FormValue(key), 10, 64) | ||
} | ||
|
||
// Uint returns request form as uint | ||
func (f *Forms) Uint(key string) (uint, error) { | ||
v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 64) | ||
return uint(v), err | ||
} | ||
|
||
// Uint32 returns request form as uint32 | ||
func (f *Forms) Uint32(key string) (uint32, error) { | ||
v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 32) | ||
return uint32(v), err | ||
} | ||
|
||
// Uint64 returns request form as uint64 | ||
func (f *Forms) Uint64(key string) (uint64, error) { | ||
return strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 64) | ||
} | ||
|
||
// Bool returns request form as bool | ||
func (f *Forms) Bool(key string) (bool, error) { | ||
return strconv.ParseBool((*http.Request)(f).FormValue(key)) | ||
} | ||
|
||
// Float32 returns request form as float32 | ||
func (f *Forms) Float32(key string) (float32, error) { | ||
v, err := strconv.ParseFloat((*http.Request)(f).FormValue(key), 64) | ||
return float32(v), err | ||
} | ||
|
||
// Float64 returns request form as float64 | ||
func (f *Forms) Float64(key string) (float64, error) { | ||
return strconv.ParseFloat((*http.Request)(f).FormValue(key), 64) | ||
} | ||
|
||
// MustString returns request form as string with default | ||
func (f *Forms) MustString(key string, defaults ...string) string { | ||
if v := (*http.Request)(f).FormValue(key); len(v) > 0 { | ||
if v, ok := ctx.Req.Form[key]; ok { | ||
return v | ||
} | ||
if len(defaults) > 0 { | ||
return defaults[0] | ||
} | ||
return "" | ||
return nil | ||
} | ||
|
||
// MustTrimmed returns request form as string with default | ||
func (f *Forms) MustTrimmed(key string, defaults ...string) string { | ||
return strings.TrimSpace(f.MustString(key, defaults...)) | ||
// FormTrim returns space trimmed value of a query based on key as string | ||
func (ctx *Context) FormTrim(key string) string { | ||
return strings.TrimSpace(ctx.Req.FormValue(key)) | ||
} | ||
|
||
// MustStrings returns request form as strings with default | ||
func (f *Forms) MustStrings(key string, defaults ...[]string) []string { | ||
if (*http.Request)(f).Form == nil { | ||
if err := (*http.Request)(f).ParseMultipartForm(32 << 20); err != nil { | ||
log.Error("ParseMultipartForm: %v", err) | ||
return []string{} | ||
} | ||
} | ||
|
||
if v, ok := (*http.Request)(f).Form[key]; ok { | ||
return v | ||
} | ||
if len(defaults) > 0 { | ||
return defaults[0] | ||
} | ||
return []string{} | ||
} | ||
|
||
// MustEscape returns request form as escaped string with default | ||
func (f *Forms) MustEscape(key string, defaults ...string) string { | ||
if v := (*http.Request)(f).FormValue(key); len(v) > 0 { | ||
return template.HTMLEscapeString(v) | ||
} | ||
if len(defaults) > 0 { | ||
return defaults[0] | ||
} | ||
return "" | ||
} | ||
|
||
// MustInt returns request form as int with default | ||
func (f *Forms) MustInt(key string, defaults ...int) int { | ||
v, err := strconv.Atoi((*http.Request)(f).FormValue(key)) | ||
if len(defaults) > 0 && err != nil { | ||
return defaults[0] | ||
} | ||
return v | ||
} | ||
|
||
// MustInt32 returns request form as int32 with default | ||
func (f *Forms) MustInt32(key string, defaults ...int32) int32 { | ||
v, err := strconv.ParseInt((*http.Request)(f).FormValue(key), 10, 32) | ||
if len(defaults) > 0 && err != nil { | ||
return defaults[0] | ||
} | ||
return int32(v) | ||
} | ||
|
||
// MustInt64 returns request form as int64 with default | ||
func (f *Forms) MustInt64(key string, defaults ...int64) int64 { | ||
v, err := strconv.ParseInt((*http.Request)(f).FormValue(key), 10, 64) | ||
if len(defaults) > 0 && err != nil { | ||
return defaults[0] | ||
} | ||
// FormInt returns request form as int | ||
func (ctx *Context) FormInt(key string) int { | ||
v, _ := strconv.Atoi(ctx.Req.FormValue(key)) | ||
return v | ||
} | ||
|
||
// MustUint returns request form as uint with default | ||
func (f *Forms) MustUint(key string, defaults ...uint) uint { | ||
v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 64) | ||
if len(defaults) > 0 && err != nil { | ||
return defaults[0] | ||
} | ||
return uint(v) | ||
} | ||
|
||
// MustUint32 returns request form as uint32 with default | ||
func (f *Forms) MustUint32(key string, defaults ...uint32) uint32 { | ||
v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 32) | ||
if len(defaults) > 0 && err != nil { | ||
return defaults[0] | ||
} | ||
return uint32(v) | ||
} | ||
|
||
// MustUint64 returns request form as uint64 with default | ||
func (f *Forms) MustUint64(key string, defaults ...uint64) uint64 { | ||
v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 64) | ||
if len(defaults) > 0 && err != nil { | ||
return defaults[0] | ||
} | ||
return v | ||
} | ||
|
||
// MustFloat32 returns request form as float32 with default | ||
func (f *Forms) MustFloat32(key string, defaults ...float32) float32 { | ||
v, err := strconv.ParseFloat((*http.Request)(f).FormValue(key), 32) | ||
if len(defaults) > 0 && err != nil { | ||
return defaults[0] | ||
} | ||
return float32(v) | ||
} | ||
|
||
// MustFloat64 returns request form as float64 with default | ||
func (f *Forms) MustFloat64(key string, defaults ...float64) float64 { | ||
v, err := strconv.ParseFloat((*http.Request)(f).FormValue(key), 64) | ||
if len(defaults) > 0 && err != nil { | ||
return defaults[0] | ||
} | ||
// FormInt64 returns request form as int64 | ||
func (ctx *Context) FormInt64(key string) int64 { | ||
v, _ := strconv.ParseInt(ctx.Req.FormValue(key), 10, 64) | ||
return v | ||
} | ||
|
||
// MustBool returns request form as bool with default | ||
func (f *Forms) MustBool(key string, defaults ...bool) bool { | ||
v, err := strconv.ParseBool((*http.Request)(f).FormValue(key)) | ||
if len(defaults) > 0 && err != nil { | ||
return defaults[0] | ||
} | ||
// FormBool returns value of a query based on key as bool | ||
func (ctx *Context) FormBool(key string) bool { | ||
v, _ := strconv.ParseBool(ctx.Req.FormValue(key)) | ||
return v | ||
} | ||
|
||
// MustOptionalBool returns request form as OptionalBool with default | ||
func (f *Forms) MustOptionalBool(key string, defaults ...util.OptionalBool) util.OptionalBool { | ||
value := (*http.Request)(f).FormValue(key) | ||
// FormOptionalBool returns value of a query based on key as OptionalBool | ||
func (ctx *Context) FormOptionalBool(key string) util.OptionalBool { | ||
value := ctx.Req.FormValue(key) | ||
if len(value) == 0 { | ||
return util.OptionalBoolNone | ||
} | ||
v, err := strconv.ParseBool((*http.Request)(f).FormValue(key)) | ||
if len(defaults) > 0 && err != nil { | ||
return defaults[0] | ||
} | ||
v, _ := strconv.ParseBool(ctx.Req.FormValue(key)) | ||
return util.OptionalBoolOf(v) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.