Search Apps Documentation Source Content File Folder Download Copy Actions Download

query.gno

5.60 Kb ยท 190 lines
  1package query
  2
  3import (
  4	"net/url"
  5)
  6
  7// GetQueryValues returns all values for a given key in query parameters.
  8func GetQueryValues(params url.Values, key string) []string {
  9	return params[key]
 10}
 11
 12// GetQueryFirstValue returns the first value for a given key in query parameters.
 13func GetQueryFirstValue(params url.Values, key string) string {
 14	if vals, ok := params[key]; ok && len(vals) > 0 {
 15		return vals[0]
 16	}
 17	return ""
 18}
 19
 20// HasQueryKey checks if the given key exists in query parameters.
 21func HasQueryKey(params url.Values, key string) bool {
 22	_, exists := params[key]
 23	return exists
 24}
 25
 26// GetQueryValueFromURL retrieves a single query value from the raw URL.
 27// If the URL is invalid or the parameter does not exist, it returns an empty string.
 28//
 29//	 value := GetQueryValueFromURL("user", "https://example.com/?user=john")
 30//		// value == "john"
 31func GetQueryValueFromURL(key, rawPath string) (string, error) {
 32	u, err := url.Parse(rawPath)
 33	if err != nil {
 34		return "", err
 35	}
 36	return u.Query().Get(key), nil
 37}
 38
 39// GetQuery retrieves a single query parameter value from the raw URL.
 40// If the URL is invalid or the parameter does not exist, it returns an empty string.
 41func GetQuery(key, rawPath string) string {
 42	u, err := url.Parse(rawPath)
 43	if err != nil {
 44		return ""
 45	}
 46	return u.Query().Get(key)
 47}
 48
 49// ParseQueryFirstValuesMap returns a map of the first values for all query parameters.
 50//
 51//	m := ParseQueryFirstValuesMap("https://example.com/?foo=1&bar=2")
 52//	// m == map[string]string{"foo": "1", "bar": "2"}
 53func ParseQueryFirstValuesMap(rawPath string) (map[string]string, error) {
 54	u, err := url.Parse(rawPath)
 55	if err != nil {
 56		return nil, err
 57	}
 58
 59	result := make(map[string]string)
 60	for key, vals := range u.Query() {
 61		if len(vals) > 0 {
 62			result[key] = vals[0]
 63		}
 64	}
 65	return result, nil
 66}
 67
 68// UpdateQueryValue sets a single query key to one value, replacing existing values.
 69//
 70//	url := UpdateQueryValue("https://example.com/?foo=1", "foo", "2")
 71//	// url == "https://example.com/?foo=2"
 72func UpdateQueryValue(rawPath, key, value string) (string, error) {
 73	return UpdateQueryValues(rawPath, key, []string{value})
 74}
 75
 76// UpdateQueryValues sets a single query key to multiple values.
 77// Existing values are replaced, and new ones are added. Returns the modified URL as a string.
 78//
 79//	url := UpdateQueryValues("https://example.com/?foo=1", "foo", []string{"2", "3"})
 80//	// url == "https://example.com/?foo=2&foo=3"
 81func UpdateQueryValues(rawPath, key string, values []string) (string, error) {
 82	u, err := url.Parse(rawPath)
 83	if err != nil {
 84		return "", err
 85	}
 86	q := u.Query()
 87	q[key] = values
 88	u.RawQuery = q.Encode()
 89	return u.String(), nil
 90}
 91
 92// UpdateQueryFirstValues sets multiple query parameters to single values.
 93// Existing parameters are overwritten, and new ones are added. Returns the modified URL as a string.
 94//
 95//	url := UpdateQueryFirstValues("https://example.com/?foo=1", map[string]string{"foo": "2", "bar": "3"})
 96//	// url == "https://example.com/?bar=3&foo=2"
 97func UpdateQueryFirstValues(rawPath string, updates map[string]string) (string, error) {
 98	u, err := url.Parse(rawPath)
 99	if err != nil {
100		return "", err
101	}
102	q := u.Query()
103	for k, v := range updates {
104		q.Set(k, v)
105	}
106	u.RawQuery = q.Encode()
107	return u.String(), nil
108}
109
110// UpdateQueryAllValues sets multiple query parameters, each with multiple values.
111// Existing parameters are replaced, and new ones are added. Returns the modified URL as a string.
112//
113//	url := UpdateQueryAllValues("https://example.com/?foo=1", map[string][]string{"foo": {"2", "3"}, "bar": {"4"}})
114//	// url == "https://example.com/?bar=4&foo=2&foo=3"
115func UpdateQueryAllValues(rawPath string, updates map[string][]string) (string, error) {
116	u, err := url.Parse(rawPath)
117	if err != nil {
118		return "", err
119	}
120	q := u.Query()
121	for k, v := range updates {
122		q[k] = v
123	}
124	u.RawQuery = q.Encode()
125	return u.String(), nil
126}
127
128// SetQueries sets multiple query parameters in the URL.
129// It replaces existing parameters and adds new ones, returning the modified URL as a string.
130//
131// url := SetQueries("https://example.com/?fa=1", map[string]string{"foo": "2", "bar": "3"})
132// // url == "https://example.com/?bar=3&foo=2"
133func SetQueries(rawPath string, queries map[string]string) (string, error) {
134	u, err := url.Parse(rawPath)
135	if err != nil {
136		return "", err
137	}
138	q := u.Query()
139	for k, v := range queries {
140		q.Set(k, v)
141	}
142	u.RawQuery = q.Encode()
143	return u.String(), nil
144}
145
146// SetQueriesMulti sets multiple query parameters with multiple values.
147// It replaces existing parameters and adds new ones, returning the modified URL as a string.
148//
149// url := SetQueriesMulti("https://example.com/?fa=1", map[string][]string{"foo": {"2", "3"}, "bar": {"4"}})
150// // url == "https://example.com/?bar=4&foo=2&foo=3"
151func SetQueriesMulti(rawPath string, queries map[string][]string) (string, error) {
152	u, err := url.Parse(rawPath)
153	if err != nil {
154		return "", err
155	}
156	q := u.Query()
157	for k, v := range queries {
158		q[k] = v
159	}
160	u.RawQuery = q.Encode()
161	return u.String(), nil
162}
163
164// DeleteQuery removes a key from the query parameters.
165//
166//	url := DeleteQuery("https://example.com/?foo=1&bar=2", "foo")
167//	// url == "https://example.com/?bar=2"
168func DeleteQuery(rawPath, key string) (string, error) {
169	u, err := url.Parse(rawPath)
170	if err != nil {
171		return "", err
172	}
173	q := u.Query()
174	q.Del(key)
175	u.RawQuery = q.Encode()
176	return u.String(), nil
177}
178
179// ResetQuery clears all query parameters from the URL path.
180//
181//	url := ResetQuery("https://example.com/?foo=1&bar=2")
182//	// url == "https://example.com/"
183func ResetQuery(rawPath string) (string, error) {
184	u, err := url.Parse(rawPath)
185	if err != nil {
186		return "", err
187	}
188	u.RawQuery = ""
189	return u.String(), nil
190}