mirror of
https://github.com/goharbor/harbor.git
synced 2025-12-05 04:54:30 +01:00
refactor: replace HasPrefix+TrimPrefix with CutPrefix (#22520)
Signed-off-by: dulanting <dulanting@outlook.jp>
This commit is contained in:
parent
985d5c3f0c
commit
80daa2dfcd
@ -31,8 +31,8 @@ func FromRequest(req *http.Request) string {
|
||||
return ""
|
||||
}
|
||||
auth := req.Header.Get("Authorization")
|
||||
if strings.HasPrefix(auth, HeaderPrefix) {
|
||||
return strings.TrimPrefix(auth, HeaderPrefix)
|
||||
if after, ok := strings.CutPrefix(auth, HeaderPrefix); ok {
|
||||
return after
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@ -85,8 +85,8 @@ func ParseSorting(sort string) []*Sort {
|
||||
for sorting := range strings.SplitSeq(sort, ",") {
|
||||
key := sorting
|
||||
desc := false
|
||||
if strings.HasPrefix(sorting, "-") {
|
||||
key = strings.TrimPrefix(sorting, "-")
|
||||
if after, ok := strings.CutPrefix(sorting, "-"); ok {
|
||||
key = after
|
||||
desc = true
|
||||
}
|
||||
sorts = append(sorts, &Sort{
|
||||
|
||||
@ -111,8 +111,8 @@ func listOrderBy(query *q.Query) string {
|
||||
}
|
||||
prefixes := []string{"hard.", "used."}
|
||||
for _, prefix := range prefixes {
|
||||
if strings.HasPrefix(sortByItem.Key, prefix) {
|
||||
resource := strings.TrimPrefix(sortByItem.Key, prefix)
|
||||
if after, ok := strings.CutPrefix(sortByItem.Key, prefix); ok {
|
||||
resource := after
|
||||
if types.IsValidResource(types.ResourceName(resource)) {
|
||||
field := fmt.Sprintf("%s->>%s", strings.TrimSuffix(prefix, "."), orm.QuoteLiteral(resource))
|
||||
orderBy = fmt.Sprintf("(%s) %s", castQuantity(field), order)
|
||||
|
||||
@ -41,8 +41,8 @@ func ParseProjectName(r *http.Request) string {
|
||||
}
|
||||
|
||||
for _, prefix := range prefixes {
|
||||
if strings.HasPrefix(path, prefix) {
|
||||
parts := strings.Split(strings.TrimPrefix(path, prefix), "/")
|
||||
if after, ok := strings.CutPrefix(path, prefix); ok {
|
||||
parts := strings.Split(after, "/")
|
||||
if len(parts) > 0 {
|
||||
projectName = parts[0]
|
||||
break
|
||||
|
||||
Loading…
Reference in New Issue
Block a user