-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.go
73 lines (64 loc) · 1.62 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
package main
import (
"bytes"
"encoding/gob"
"fmt"
"log"
"reflect"
"strconv"
"strings"
"sync/atomic"
)
func encode(value interface{}) ([]byte, error) {
buffer := new(bytes.Buffer)
encoder := gob.NewEncoder(buffer)
if err := encoder.Encode(value); err != nil {
log.Printf("[Encode] Error when encode object, %s", err)
return nil, err
}
return buffer.Bytes(), nil
}
func decode(data []byte, vType reflect.Type) (interface{}, error) {
v := reflect.New(vType)
buffer := bytes.NewBuffer(data)
decoder := gob.NewDecoder(buffer)
if err := decoder.DecodeValue(v); err != nil {
log.Printf("[Decode] Error when decode object, %s", err)
return nil, err
}
return reflect.Indirect(v).Interface(), nil
}
func parseComputerSize(size string) (int, error) {
oneKBytes := 1 << 10
oneMBytes := 1 << 20
oneGBytes := 1 << 30
var (
count int
bits byte
)
if _, err := fmt.Sscanf(strings.ToLower(size), "%d%c", &count, &bits); err != nil {
return 0, err
}
switch bits {
case 'k':
return count * oneKBytes, nil
case 'm':
return count * oneMBytes, nil
case 'g':
return count * oneGBytes, nil
}
return 0, fmt.Errorf("[Config] Format error")
}
type AtomicInt int64
func (i *AtomicInt) Add(n int64) {
atomic.AddInt64((*int64)(i), n)
}
func (i *AtomicInt) Set(n int64) {
atomic.StoreInt64((*int64)(i), n)
}
func (i *AtomicInt) Get() int64 {
return atomic.LoadInt64((*int64)(i))
}
func (i *AtomicInt) String() string {
return strconv.FormatInt(i.Get(), 10)
}