Skip to content

Commit

Permalink
feat: add QueryIntWithDefault to web context (grafana#93130)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Godin authored Sep 10, 2024
1 parent 22c63ea commit 2c93120
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/web/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,22 @@ func (ctx *Context) QueryInt(name string) int {
return n
}

// QueryIntWithDefault returns query result in int type, including a default when the query param is not a valid int.
func (ctx *Context) QueryIntWithDefault(name string, d int) int {
n, err := strconv.Atoi(ctx.Query(name))
if err != nil {
return d
}
return n
}

// QueryInt64 returns query result in int64 type.
func (ctx *Context) QueryInt64(name string) int64 {
n, _ := strconv.ParseInt(ctx.Query(name), 10, 64)
return n
}

// QueryInt64WithDefault returns query result in int64 type, including a default when the query param is not a valid int64.
func (ctx *Context) QueryInt64WithDefault(name string, d int64) int64 {
n, err := strconv.ParseInt(ctx.Query(name), 10, 64)
if err != nil {
Expand Down

0 comments on commit 2c93120

Please sign in to comment.