-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
93 lines (81 loc) · 1.47 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package common
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strconv"
)
func NewBool(t bool) *bool {
return &t
}
func NewString(t string) *string {
if t == "" {
return nil
}
return &t
}
func NewInt(t int) *int {
return &t
}
func NewBoolFromInterface(val interface{}) bool {
switch val.(type) {
case bool:
return val.(bool)
default:
return false
}
}
func NewInt64pFromInterface(val interface{}) *int64 {
switch val.(type) {
case int64:
v := val.(int64)
return &v
case float64:
v := int64(val.(float64))
return &v
default:
return nil
}
}
func NewStringpFromInterface(val interface{}) *string {
switch val.(type) {
case string:
v := val.(string)
return &v
default:
return nil
}
}
func NewStringFromInterface(val interface{}) string {
switch val.(type) {
case string:
return val.(string)
case float64:
return fmt.Sprintf("%d", int64(val.(float64)))
default:
return ""
}
}
func NewReadCloserFromBytes(t []byte) io.ReadCloser {
return ioutil.NopCloser(bytes.NewReader(t))
}
func NewReadCloserFromReader(r io.Reader) io.ReadCloser {
return ioutil.NopCloser(r)
}
func PrettyPrint(json_dirty []byte) []byte {
var json_pretty bytes.Buffer
error := json.Indent(&json_pretty, json_dirty, "", " ")
if error != nil {
return json_dirty
}
json_pretty.Write([]byte("\n"))
return json_pretty.Bytes()
}
func CookieName(idx int) string {
if idx == 0 {
return COOKIE_NAME_AUTH
}
return COOKIE_NAME_AUTH + strconv.Itoa(idx)
}