From 78c58689e5745f69b9bb88e7b943a0337683aa26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=C6=B0=E1=BB=9Bc=20Trung?= Date: Mon, 29 Aug 2022 17:12:36 +0700 Subject: [PATCH] url param bool default --- context/context.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/context/context.go b/context/context.go index 2882ae3b..f758d323 100644 --- a/context/context.go +++ b/context/context.go @@ -1679,6 +1679,17 @@ func (ctx *Context) URLParamBool(name string) (bool, error) { return strconv.ParseBool(ctx.URLParam(name)) } +// URLParamBoolDefault returns the url query parameter as boolean value from a request, +// if not found or parse failed then "def" is returned. +func (ctx *Context) URLParamBoolDefault(name string, def bool) bool { + v, err := ctx.URLParamBool(name) + if err != nil { + return def + } + + return v +} + // URLParams returns a map of URL Query parameters. // If the value of a URL parameter is a slice, // then it is joined as one separated by comma.