Skip to content

Commit 9995475

Browse files
committed
invent ctx.QueryOptionalBool
1 parent f4d3bf7 commit 9995475

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

modules/context/context.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"code.gitea.io/gitea/modules/setting"
2828
"code.gitea.io/gitea/modules/templates"
2929
"code.gitea.io/gitea/modules/translation"
30+
"code.gitea.io/gitea/modules/util"
3031
"code.gitea.io/gitea/modules/web/middleware"
3132
"code.gitea.io/gitea/services/auth"
3233

@@ -319,6 +320,11 @@ func (ctx *Context) QueryBool(key string, defaults ...bool) bool {
319320
return (*Forms)(ctx.Req).MustBool(key, defaults...)
320321
}
321322

323+
// QueryOptionalBool returns request form as OptionalBool with default
324+
func (ctx *Context) QueryOptionalBool(key string, defaults ...util.OptionalBool) util.OptionalBool {
325+
return (*Forms)(ctx.Req).MustOptionalBool(key, defaults...)
326+
}
327+
322328
// HandleText handles HTTP status code
323329
func (ctx *Context) HandleText(status int, title string) {
324330
if (status/100 == 4) || (status/100 == 5) {

modules/context/form.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"text/template"
1414

1515
"code.gitea.io/gitea/modules/log"
16+
"code.gitea.io/gitea/modules/util"
1617
)
1718

1819
// Forms a new enhancement of http.Request
@@ -225,3 +226,16 @@ func (f *Forms) MustBool(key string, defaults ...bool) bool {
225226
}
226227
return v
227228
}
229+
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)
233+
if len(value) == 0 {
234+
return util.OptionalBoolNone
235+
}
236+
v, err := strconv.ParseBool((*http.Request)(f).FormValue(key))
237+
if len(defaults) > 0 && err != nil {
238+
return defaults[0]
239+
}
240+
return util.OptionalBoolOf(v)
241+
}

0 commit comments

Comments
 (0)