/
util.go
142 lines (111 loc) · 2.62 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
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
package hraftd
import (
"bytes"
"io"
"net/http"
"os"
"os/signal"
"strings"
"time"
jsoniter "github.com/json-iterator/go"
)
// EmptyThen returns t if s is empty.
func EmptyThen(s, t string) string {
if s == "" {
return t
}
return s
}
// EqualsThen returns t if s equals to e.
func EqualsThen(s, e, t string) string {
if s == e {
return t
}
return s
}
// If returns s if i else t.
func If(i bool, s, t string) string {
if i {
return s
}
return t
}
// CloneMap clones a map.
func CloneMap(m map[string]string) map[string]string {
o := make(map[string]string)
for k, v := range m {
o[k] = v
}
return o
}
// WriteAsText writes s as text/plain.
func WriteAsText(s string, w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
_, _ = w.Write([]byte(s))
}
// WriteAsJSON writes m as JSON.
func WriteAsJSON(m interface{}, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_, _ = w.Write(JsonifyBytes(m))
}
// ReadString ...
func ReadString(object io.ReadCloser) string {
return string(ReadBytes(object))
}
// ReadBytes ...
func ReadBytes(object io.ReadCloser) []byte {
defer object.Close()
buf := new(bytes.Buffer)
_, _ = buf.ReadFrom(object)
return buf.Bytes()
}
// PathExists returns true if the given path exists.
func PathExists(p string) bool {
if _, err := os.Lstat(p); err != nil && os.IsNotExist(err) {
return false
}
return true
}
const dfmt = "2006-01-02 15:04:05.000"
// FormatTime format time.
// FormatTime format time.
func FormatTime(t time.Time) string {
return t.Format(dfmt)
}
// ParseTime parses time.
func ParseTime(s string) (time.Time, error) {
return time.Parse(dfmt, s)
}
// Jsonify jsonifies v to JSON string.
func Jsonify(v interface{}) string {
b, _ := jsoniter.MarshalToString(v)
return b
}
// Jsonify4Print jsonifies v to JSON string for printing only.
func Jsonify4Print(v interface{}) string {
all := strings.ReplaceAll(Jsonify(v), `\"`, ``)
return strings.ReplaceAll(all, `"`, ``)
}
// JsonifyBytes jsonifies v to JSON []byte.
func JsonifyBytes(v interface{}) []byte {
b, _ := jsoniter.Marshal(v)
return b
}
// TryParseJSON try parse v as JSON,return parsed object or failed to source v.
func TryParseJSON(v string) interface{} {
if jv, err := ParseJSON(v); err == nil {
return jv
}
return v
}
// ParseJSON parses string to v.
func ParseJSON(v string) (m interface{}, err error) {
err = jsoniter.UnmarshalFromString(v, &m)
return
}
// WaitInterrupt waits on interrupt signal.
func WaitInterrupt() {
terminate := make(chan os.Signal, 1)
signal.Notify(terminate, os.Interrupt)
<-terminate
}