-
Notifications
You must be signed in to change notification settings - Fork 62
/
string_utils.go
185 lines (151 loc) · 3.97 KB
/
string_utils.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package utils
import (
"encoding/json"
//"errors"
"fmt"
"net/url"
"sort"
"strings"
uuid "github.com/google/uuid"
pnerr "github.com/pubnub/go/pnerr"
)
// JoinChannels encodes and joins channels
func JoinChannels(channels []string) []byte {
if len(channels) == 0 {
return []byte(",")
}
var encodedChannels []string
for _, value := range channels {
encodedChannels = append(encodedChannels, URLEncode(value))
}
return []byte(strings.Join(encodedChannels, ","))
}
// encodeJSONAsPathComponent properly encodes serialized JSON
// for placement within a URI path
func EncodeJSONAsPathComponent(jsonBytes string) string {
u := &url.URL{Path: jsonBytes}
encodedPath := u.String()
// Go 1.8 inserts a ./ per RFC 3986 §4.2. Previous versions
// will be unaffected by this under the assumption that jsonBytes
// represents valid JSON
return strings.TrimLeft(encodedPath, "./")
}
func Serialize(msg interface{}) ([]byte, error) {
jsonSerialized, errJSONMarshal := json.Marshal(msg)
if errJSONMarshal != nil {
return []byte{}, errJSONMarshal
}
return jsonSerialized, nil
}
func SerializeAndEncrypt(msg interface{}, cipherKey string, serialize bool) (string, error) {
var encrypted string
if serialize {
jsonSerialized, errJSONMarshal := json.Marshal(msg)
if errJSONMarshal != nil {
return "", errJSONMarshal
}
encrypted = EncryptString(cipherKey, string(jsonSerialized))
} else {
if serializedMsg, ok := msg.(string); ok {
encrypted = EncryptString(cipherKey, serializedMsg)
} else {
return "", pnerr.NewBuildRequestError("Message is not JSON serialized.")
}
}
return encrypted, nil
}
func SerializeEncryptAndSerialize(msg interface{}, cipherKey string, serialize bool) (string, error) {
var encrypted string
if serialize {
jsonSerialized, errJSONMarshal := json.Marshal(msg)
if errJSONMarshal != nil {
return "", errJSONMarshal
}
encrypted = EncryptString(cipherKey, string(jsonSerialized))
} else {
if serializedMsg, ok := msg.(string); ok {
encrypted = EncryptString(cipherKey, serializedMsg)
} else {
return "", pnerr.NewBuildRequestError("Message is not JSON serialized.")
}
}
jsonSerialized, errJSONMarshal := json.Marshal(encrypted)
if errJSONMarshal != nil {
return "", errJSONMarshal
}
return string(jsonSerialized), nil
}
// PubNub - specific serializer
func ValueAsString(value interface{}) ([]byte, error) {
switch t := value.(type) {
case string:
return []byte(fmt.Sprintf("\"%s\"", t)), nil
default:
val, err := json.Marshal(value)
return val, err
}
}
// Generate a random uuid string
func UUID() string {
return uuid.New().String()
}
func sortQueries(params *url.Values) []string {
sortedKeys := make([]string, len(*params))
i := 0
for k, _ := range *params {
sortedKeys[i] = k
i++
}
sort.Strings(sortedKeys)
return sortedKeys
}
func PreparePamParams(params *url.Values) string {
sortedKeys := sortQueries(params)
stringifiedQuery := ""
i := 0
for _, v := range sortedKeys {
if i == len(sortedKeys)-1 {
stringifiedQuery += fmt.Sprintf("%s=%s", v, PamEncode((*params)[v][0]))
} else {
stringifiedQuery += fmt.Sprintf("%s=%s&", v, PamEncode((*params)[v][0]))
}
i++
}
return stringifiedQuery
}
func PamEncode(value string) string {
// *!'()[]~
stringifiedParam := URLEncode(value)
var replacer = strings.NewReplacer(
"*", "%2A",
"!", "%21",
"'", "%27",
"(", "%28",
")", "%29",
"[", "%5B",
"]", "%5D",
"~", "%7E")
stringifiedParam = replacer.Replace(stringifiedParam)
return stringifiedParam
}
func QueryToString(query *url.Values) string {
stringifiedQuery := ""
i := 0
for k, v := range *query {
if i == len(*query)-1 {
stringifiedQuery += fmt.Sprintf("%s=%s", k, v[0])
} else {
stringifiedQuery += fmt.Sprintf("%s=%s&", k, v[0])
}
i++
}
return stringifiedQuery
}
// TODO: verify the helper is used where supposed to
func URLEncode(s string) string {
v := url.QueryEscape(s)
var replacer = strings.NewReplacer(
"+", "%20",
)
return replacer.Replace(v)
}