forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathutils.go
69 lines (61 loc) · 1.34 KB
/
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
package sls
import (
"encoding/base64"
"fmt"
"net/http"
"strconv"
"strings"
)
func BoolToInt64(b bool) int64 {
if b {
return 1
}
return 0
}
func BoolPtrToStringNum(b *bool) string {
if b == nil {
return ""
}
if *b {
return "1"
}
return "0"
}
func Int64PtrToString(i *int64) string {
if i == nil {
return ""
}
return strconv.FormatInt(*i, 10)
}
func ParseHeaderInt(r *http.Response, headerName string) (int, error) {
values := r.Header[headerName]
if len(values) > 0 {
value, err := strconv.Atoi(values[0])
if err != nil {
return -1, fmt.Errorf("can't parse '%s' header: %v", strings.ToLower(headerName), err)
}
return value, nil
}
return -1, fmt.Errorf("can't find '%s' header", strings.ToLower(headerName))
}
func parseHeaderString(header http.Header, headerName string) (string, error) {
v, ok := header[headerName]
if !ok || len(v) == 0 {
return "", fmt.Errorf("can't find '%s' header", strings.ToLower(headerName))
}
return v[0], nil
}
func decodeCursor(cursor string) (int64, error) {
c, err := base64.StdEncoding.DecodeString(cursor)
if err != nil {
return 0, err
}
cursorInt, err := strconv.ParseInt(string(c), 10, 64)
if err != nil {
return 0, err
}
return cursorInt, nil
}
func encodeCursor(cursor int64) string {
return base64.StdEncoding.EncodeToString([]byte(strconv.FormatInt(cursor, 10)))
}