Initial cloud-services repo - gateway service + pkg modules
This commit is contained in:
69
pkg/utils/urlhelper/urlhelper.go
Normal file
69
pkg/utils/urlhelper/urlhelper.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package urlhelper
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// BuildQuery builds querystring from map
|
||||
func BuildQuery(queries map[string]string) string {
|
||||
var qs []string
|
||||
|
||||
for key, value := range queries {
|
||||
qs = append(qs, key+"="+url.QueryEscape(value))
|
||||
}
|
||||
|
||||
return strings.Join(qs, "&")
|
||||
}
|
||||
|
||||
// BuildURL builds url with base url and querystring
|
||||
func BuildURL(base string, queries map[string]string) string {
|
||||
return strings.Join([]string{base, BuildQuery(queries)}, "?")
|
||||
}
|
||||
|
||||
func GetQueryInt(query url.Values, key string) int {
|
||||
i, _ := strconv.Atoi(query.Get(key))
|
||||
return i
|
||||
}
|
||||
|
||||
// Parse a epoch unix time number from the URL
|
||||
func GetQueryUnix(query url.Values, key string) (t time.Time) {
|
||||
return time.UnixMilli(GetQueryInt64(query, key))
|
||||
}
|
||||
|
||||
func GetQueryTimeStamp(query url.Values, key string) (t time.Time) {
|
||||
t, _ = time.Parse(time.RFC3339Nano, query.Get(key))
|
||||
return t
|
||||
}
|
||||
|
||||
// GetQueryBool returns val, ok.
|
||||
func GetQueryBool(query url.Values, key string) (bool, bool) {
|
||||
b, err := strconv.ParseBool(query.Get(key))
|
||||
return b, err == nil
|
||||
}
|
||||
|
||||
func GetQueryInt64(query url.Values, key string) int64 {
|
||||
i, _ := strconv.ParseInt(query.Get(key), 0, 64)
|
||||
return i
|
||||
}
|
||||
|
||||
func GetQueryFloat32(query url.Values, key string) float32 {
|
||||
i, _ := strconv.ParseFloat(query.Get(key), 32)
|
||||
return float32(i)
|
||||
}
|
||||
|
||||
func GetQueryUUID(query url.Values, key string) uuid.UUID {
|
||||
val := query.Get(key)
|
||||
if val != "" {
|
||||
id, err := uuid.Parse(val)
|
||||
if err == nil {
|
||||
return id
|
||||
}
|
||||
}
|
||||
|
||||
return uuid.Nil
|
||||
}
|
||||
Reference in New Issue
Block a user