mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package context
|
|
|
|
import (
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/git/modules/setting"
|
|
|
|
"github.com/hanzoai/git/modules/web"
|
|
)
|
|
|
|
// PathParam returns the param in request path, eg: "/{var}" => "/a%2fb", then `var == "a/b"`
|
|
func (b *Base) PathParam(name string) string {
|
|
s, err := url.PathUnescape(b.PathParamRaw(name))
|
|
if err != nil && !setting.IsProd {
|
|
panic("Failed to unescape path param: " + err.Error() + ", there seems to be a double-unescaping bug")
|
|
}
|
|
return s
|
|
}
|
|
|
|
// PathParamRaw returns the raw param in request path, eg: "/{var}" => "/a%2fb", then `var == "a%2fb"`
|
|
func (b *Base) PathParamRaw(name string) string {
|
|
if strings.HasPrefix(name, ":") {
|
|
setting.PanicInDevOrTesting("path param should not start with ':'")
|
|
name = name[1:]
|
|
}
|
|
return web.GetRouteContext(b.Req.Context()).Param(name)
|
|
}
|
|
|
|
// PathParamInt64 returns the param in request path as int64
|
|
func (b *Base) PathParamInt64(p string) int64 {
|
|
v, _ := strconv.ParseInt(b.PathParam(p), 10, 64)
|
|
return v
|
|
}
|
|
|
|
func (b *Base) PathParamInt(p string) int {
|
|
v, _ := strconv.Atoi(b.PathParam(p))
|
|
return v
|
|
}
|
|
|
|
// SetPathParam set request path params into routes
|
|
func (b *Base) SetPathParam(name, value string) {
|
|
web.GetRouteContext(b).SetParam(name, url.PathEscape(value))
|
|
}
|
|
|
|
func (b *Base) SetPathParamRaw(name, value string) {
|
|
web.GetRouteContext(b).SetParam(name, value)
|
|
}
|