forked from hzr1140521792/dangerous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
67 lines (59 loc) · 1.32 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
package dangerous
import (
"bytes"
"compress/zlib"
"fmt"
"io"
"reflect"
)
func ByteCompare(a, b []byte) bool {
return bytes.Compare(a, b) == 0
}
func ApplyKwargs(struct1 interface{}, kwargs map[string]interface{}) error {
values1 := reflect.ValueOf(struct1).Elem()
if values1.Type().Kind() != reflect.Struct {
return fmt.Errorf("please input struct")
}
defer func() {
if r := recover(); r != nil {
fmt.Println("Recover from Panic:", r, ">This error occurs when you set a not exist field.<")
}
}()
for k, v := range kwargs {
values1.FieldByName(k).Set(reflect.ValueOf(v))
}
return nil
}
func RSplit(b, sep []byte) ([]byte, []byte) {
index := bytes.LastIndex(b, sep)
if index == -1 {
return b, []byte("")
}
return b[:index], b[index+1:]
}
func Concentrate(b ...interface{}) ([]byte, error) {
var _bytes bytes.Buffer
for _, itf := range b {
_byte, ok := itf.([]byte)
if !ok {
return _bytes.Bytes(), fmt.Errorf("Concentrate bytes only")
}
_bytes.Write(_byte)
}
return _bytes.Bytes(), nil
}
func Compress(src []byte) []byte {
var in bytes.Buffer
w := zlib.NewWriter(&in)
w.Write(src)
w.Close()
return in.Bytes()
}
func UnCompress(data []byte) ([]byte, error) {
b := bytes.NewReader(data)
var out bytes.Buffer
r, err := zlib.NewReader(b)
io.Copy(&out, r)
r.Close()
return out.Bytes(), err
}