-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
43 lines (34 loc) · 882 Bytes
/
util.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
43
package wukong
import (
"fmt"
"net/url"
"path"
"reflect"
"regexp"
"strings"
"testing"
)
func AssertEqual(t *testing.T, received interface{}, expected interface{}) {
if received == expected {
return
}
t.Errorf("Received %v (type %v), expected %v (type %v)", received, reflect.TypeOf(received), expected, reflect.TypeOf(expected))
}
func UrlJoin(baseUrl string, segments ...string) (string, error) {
u, err := url.Parse(baseUrl)
if err != nil {
return "", err
}
u.Path = path.Join(append([]string{u.Path}, segments...)...)
return u.String(), nil
}
func UrlFormat(rawUrl string, pathParam map[string]interface{}) string {
re := regexp.MustCompile(`/:(\w+)`)
return re.ReplaceAllStringFunc(rawUrl, func(s string) string {
key := strings.Replace(s, "/:", "", -1)
if val, ok := pathParam[key]; ok {
return fmt.Sprintf("/%v", val)
}
return s
})
}