htmgo/framework/h/qs.go

98 lines
2.2 KiB
Go
Raw Permalink Normal View History

2024-09-21 16:52:56 +00:00
package h
import (
"net/url"
"strings"
)
type Qs struct {
m map[string]string
}
func NewQs(pairs ...string) *Qs {
q := &Qs{
m: make(map[string]string),
}
if len(pairs)%2 != 0 {
2024-10-23 15:50:22 +00:00
pairs = append(pairs, "")
2024-09-21 16:52:56 +00:00
}
for i := 0; i < len(pairs); i++ {
q.m[pairs[i]] = pairs[i+1]
i++
}
return q
}
func (q *Qs) Add(key string, value string) *Qs {
q.m[key] = value
return q
}
func (q *Qs) Remove(key string) *Qs {
delete(q.m, key)
return q
}
func (q *Qs) ToString() string {
builder := strings.Builder{}
index := 0
for k, v := range q.m {
builder.WriteString(k)
2024-10-23 15:50:22 +00:00
if v != "" {
builder.WriteString("=")
builder.WriteString(v)
}
2024-09-21 16:52:56 +00:00
if index < len(q.m)-1 {
builder.WriteString("&")
}
index++
}
return builder.String()
}
2024-10-26 02:59:17 +00:00
// GetQueryParam returns the value of the given query parameter from the request URL.
// There are two layers of priority:
// 1. The query parameter in the URL
// 2. The current browser URL
// If the query parameter is not found in the URL from the *RequestContext, it will fall back to the current browser URL if set.
// The URL from the *RequestContext would normally be the url from an XHR request through htmx,
// which is not the current browser url a visitor may be on.
2024-09-21 16:52:56 +00:00
func GetQueryParam(ctx *RequestContext, key string) string {
2024-10-01 03:08:52 +00:00
value, ok := ctx.Request.URL.Query()[key]
if value == nil || !ok {
2024-09-21 16:52:56 +00:00
current := ctx.currentBrowserUrl
if current != "" {
u, err := url.Parse(current)
if err == nil {
return u.Query().Get(key)
}
}
}
if len(value) == 0 {
return ""
}
return value[0]
2024-09-21 16:52:56 +00:00
}
2024-10-26 02:59:17 +00:00
// SetQueryParams sets the query parameters of the given URL.
// Given the *Qs passed in, it will set the query parameters of the URL to the given values.
// If the value does not exist in *QS, it will remain untouched.
// If the value is an empty string, it will be removed from the query parameters.
// If the value is not an empty string, it will be set to the given value.
2024-09-21 16:52:56 +00:00
func SetQueryParams(href string, qs *Qs) string {
u, err := url.Parse(href)
if err != nil {
return href
}
q := u.Query()
for key, value := range qs.m {
if value == "" {
q.Del(key)
} else {
q.Set(key, value)
}
}
u.RawQuery = q.Encode()
return u.String()
}