-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
64 lines (52 loc) · 1.09 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package goswyftx
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"math"
"strconv"
"strings"
"time"
)
func buildString(strs ...string) string {
var builtStr strings.Builder
for _, str := range strs {
builtStr.WriteString(str)
}
return builtStr.String()
}
func isEmptyStr(str string) bool {
return str == ""
}
func copyReadCloser(rc io.ReadCloser) (*bytes.Buffer, error) {
var body bytes.Buffer
if _, err := io.Copy(&body, rc); err != nil {
return nil, err
}
rc = ioutil.NopCloser(bytes.NewReader(body.Bytes()))
return &body, nil
}
func decodeJSON(r io.Reader, v interface{}) error {
if err := json.NewDecoder(r).Decode(v); err != nil {
return err
}
return nil
}
type SwyftxTime struct {
time.Time
}
func (s *SwyftxTime) UnmarshalJSON(b []byte) (err error) {
if bytes.ContainsAny(b, "\"") {
b = bytes.Trim(b, "\"")
}
var floatTime float64
floatTime, err = strconv.ParseFloat(string(b), 32)
if err != nil {
return err
}
// https://stackoverflow.com/questions/37628254
sec, dec := math.Modf(floatTime)
s.Time = time.Unix(int64(sec), int64(dec*(1e9)))
return nil
}