sakila/internal/httpapi/util.go

54 lines
867 B
Go
Raw Permalink Normal View History

package httpapi
import (
"net/url"
"strconv"
"strings"
)
func parseIntDefault(s string, def int) int {
if s == "" {
return def
}
v, err := strconv.Atoi(s)
if err != nil {
return def
}
return v
}
func parseExpand(s string) map[string]bool {
m := map[string]bool{}
if strings.TrimSpace(s) == "" {
return m
}
for _, p := range strings.Split(s, ",") {
p = strings.TrimSpace(p)
if p != "" {
m[p] = true
}
}
return m
}
func buildURL(base string, limit, offset int, order, expand string) string {
v := url.Values{}
if limit > 0 {
v.Set("limit", strconv.Itoa(limit))
}
if offset > 0 {
v.Set("offset", strconv.Itoa(offset))
}
if strings.TrimSpace(order) != "" {
v.Set("order", order)
}
if strings.TrimSpace(expand) != "" {
v.Set("expand", expand)
}
qs := v.Encode()
if qs == "" {
return base
}
return base + "?" + qs
}