-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommons.go
More file actions
executable file
·104 lines (91 loc) · 2.1 KB
/
Copy pathcommons.go
File metadata and controls
executable file
·104 lines (91 loc) · 2.1 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package rubik
import (
"errors"
"fmt"
"net/url"
"reflect"
"strconv"
"strings"
"unicode"
)
const (
// clientAgent is the user agent header
clientAgent = "rubik-http-client/1.1"
// HeaderUserAgent supplies user-agent key
headerUserAgent = "User-Agent"
)
func valuesToMap(values url.Values) map[string]interface{} {
var bodyMap = make(map[string]interface{})
for k, v := range values {
val, _ := strconv.Atoi(v[0])
bodyMap[k] = val
}
return bodyMap
}
func getCountOfDollar(path string) int {
count := 0
for _, s := range path {
if s == '$' {
count++
}
}
return count
}
func substituteParam(path string, reqParams []string) (string, error) {
pathWithParams := path
dollarCount := getCountOfDollar(pathWithParams)
if len(reqParams) != dollarCount {
message := fmt.Sprintf("RubikParamsSubstitutionError: ink was not able to substitute params because of params count mismatch - $ count: %d and params given: %d", dollarCount, len(reqParams))
return "", errors.New(message)
}
// we need a replaced path
if len(reqParams) > 0 {
for _, param := range reqParams {
pathWithParams = strings.Replace(pathWithParams, "$", param, -1)
}
}
return pathWithParams, nil
}
func checkIsEntity(entity interface{}) bool {
elem := reflect.TypeOf(entity)
if elem.Kind() == reflect.Ptr {
elem = elem.Elem()
}
_, ok := elem.FieldByName("Entity")
return ok
}
func isEmptyEntity(entity struct{}) bool {
refEn := reflect.TypeOf(entity).Elem()
newEn := reflect.New(refEn)
return reflect.DeepEqual(entity, newEn)
}
func safeRouterPath(path string) string {
if strings.HasSuffix(path, "/") {
return strings.TrimSuffix(path, "/")
}
return path
}
func safeRoutePath(path string) string {
if strings.HasPrefix(path, "/") {
return path
}
return "/" + path
}
func unCapitalize(target string) string {
r := []rune(target)
r[0] = unicode.ToLower(r[0])
return string(r)
}
func capitalize(target string) string {
r := []rune(target)
r[0] = unicode.ToUpper(r[0])
return string(r)
}
func isOneOf(t string, vals ...string) bool {
for _, s := range vals {
if t == s {
return true
}
}
return false
}