-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
42 lines (36 loc) · 982 Bytes
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package vrm
import (
"bytes"
"reflect"
"text/template"
"github.com/google/go-querystring/query"
)
type URLParams map[string]string
// MapTemplate formats the given string by applying the replacement map
func MapTemplate(templateText string, replacements map[string]string) (string, error) {
tpl, err := template.New("").Parse(templateText)
if err != nil {
return "", err
}
var buf bytes.Buffer
err = tpl.Execute(&buf, replacements)
if err != nil {
return "", err
}
return buf.String(), nil
}
func formatURL(urlTemplate string, params URLParams, queryParams interface{}) (string, error) {
params["baseURL"] = baseURL
url, err := MapTemplate(urlTemplate, params)
if err != nil {
return "", err
}
if queryParams != nil && !reflect.DeepEqual(queryParams, reflect.Zero(reflect.TypeOf(queryParams)).Interface()) {
queryString, err := query.Values(queryParams)
if err != nil {
return "", err
}
url += "?" + queryString.Encode()
}
return url, nil
}