forked from russellcardullo/go-pingdom
-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.go
40 lines (35 loc) · 924 Bytes
/
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
package solarwinds
import (
"bytes"
"encoding/json"
"math/rand"
"time"
)
const (
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)
// Note there will be a new line character at the end of the output.
func ToJsonNoEscape(t interface{}) ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(t)
return buffer.Bytes(), err
}
// RandString returns random string at specified length.
func RandString(n int) string {
rand.Seed(time.Now().UnixNano())
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
// Convert is typically used to convert map[string]interface{} to a struct in the same json structure.
func Convert(from interface{}, to interface{}) error {
b, err := ToJsonNoEscape(from)
if err != nil {
return err
}
return json.Unmarshal(b, to)
}