68f2a5dd by mingqi.zhou

add convertor for cache

1 parent 3c92cce9
1 package cache
2
3 import (
4 "fmt"
5 "strconv"
6 )
7
8 func GetString(v interface{}) string {
9 switch result := v.(type) {
10 case string:
11 return result
12 case []byte:
13 return string(result)
14 default:
15 if v == nil {
16 return ""
17 } else {
18 return fmt.Sprintf("%v", result)
19 }
20 }
21 }
22
23 func GetInt(v interface{}) int {
24 switch result := v.(type) {
25 case int:
26 return result
27 case int32:
28 return int(result)
29 case int64:
30 return int(result)
31 default:
32 d := GetString(v)
33 if d != "" {
34 value, err := strconv.Atoi(d)
35 if err == nil {
36 return value
37 }
38 }
39 }
40 return 0
41 }
42
43 func GetInt64(v interface{}) int64 {
44 switch result := v.(type) {
45 case int:
46 return int64(result)
47 case int32:
48 return int64(result)
49 case int64:
50 return result
51 default:
52 d := GetString(v)
53 if d != "" {
54 result, err := strconv.ParseInt(d, 10, 64)
55 if err == nil {
56 return result
57 }
58 }
59 }
60 return 0
61 }
62
63 func GetFloat64(v interface{}) float64 {
64 switch result := v.(type) {
65 case float64:
66 return result
67 default:
68 d := GetString(v)
69 if d != "" {
70 value, err := strconv.ParseFloat(d, 64)
71 if err == nil {
72 return value
73 }
74 }
75 }
76 return 0
77 }
78
79 func GetBool(v interface{}) bool {
80 switch result := v.(type) {
81 case bool:
82 return result
83 default:
84 d := GetString(v)
85 if d != "" {
86 result, err := strconv.ParseBool(d)
87 if err == nil {
88 return result
89 }
90 }
91 }
92 return false
93 }
94
95 func getByteArray(v interface{}) []byte {
96 switch result := v.(type) {
97 case []byte:
98 return result
99 case string:
100 return []byte(result)
101 default:
102 return nil
103 }
104 }
1 package cache
2
3 import (
4 "testing"
5 )
6
7 func TestGetString(t *testing.T) {
8 var t1 = "test1"
9 if "test1" != GetString(t1) {
10 t.Error("get string from string error")
11 }
12 var t2 = []byte("test2")
13 if "test2" != GetString(t2) {
14 t.Error("get string from byte array error")
15 }
16 var t3 int = 1
17 if "1" != GetString(t3) {
18 t.Error("get string from int error")
19 }
20 var t4 int64 = 1
21 if "1" != GetString(t4) {
22 t.Error("get string from int64 error")
23 }
24 var t5 float64 = 1.1
25 if "1.1" != GetString(t5) {
26 t.Error("get string from float64 error")
27 }
28
29 if "" != GetString(nil) {
30 t.Error("get string from nil error")
31 }
32 }
33
34 func TestGetInt(t *testing.T) {
35 var t1 int = 1
36 if 1 != GetInt(t1) {
37 t.Error("get int from int error")
38 }
39 var t2 int32 = 32
40 if 32 != GetInt(t2) {
41 t.Error("get int from int32 error")
42 }
43 var t3 int64 = 64
44 if 64 != GetInt(t3) {
45 t.Error("get int from int64 error")
46 }
47 var t4 = "128"
48 if 128 != GetInt(t4) {
49 t.Error("get int from num string error")
50 }
51 if 0 != GetInt(nil) {
52 t.Error("get int from nil error")
53 }
54 }
55
56 func TestGetInt64(t *testing.T) {
57 var i int64 = 1
58 var t1 int = 1
59 if i != GetInt64(t1) {
60 t.Error("get int64 from int error")
61 }
62 var t2 int32 = 1
63 if i != GetInt64(t2) {
64 t.Error("get int64 from int32 error")
65 }
66 var t3 int64 = 1
67 if i != GetInt64(t3) {
68 t.Error("get int64 from int64 error")
69 }
70 var t4 = "1"
71 if i != GetInt64(t4) {
72 t.Error("get int64 from num string error")
73 }
74 if 0 != GetInt64(nil) {
75 t.Error("get int64 from nil")
76 }
77 }
78
79 func TestGetFloat64(t *testing.T) {
80 var f float64 = 1.11
81 var t1 float32 = 1.11
82 if f != GetFloat64(t1) {
83 t.Error("get float64 from float32 error")
84 }
85 var t2 float64 = 1.11
86 if f != GetFloat64(t2) {
87 t.Error("get float64 from float64 error")
88 }
89 var t3 = "1.11"
90 if f != GetFloat64(t3) {
91 t.Error("get float64 from string error")
92 }
93
94 var f2 float64 = 1
95 var t4 int = 1
96 if f2 != GetFloat64(t4) {
97 t.Error("get float64 from int error")
98 }
99
100 if 0 != GetFloat64(nil) {
101 t.Error("get float64 from nil error")
102 }
103 }
104
105 func TestGetBool(t *testing.T) {
106 var t1 = true
107 if true != GetBool(t1) {
108 t.Error("get bool from bool error")
109 }
110 var t2 = "true"
111 if true != GetBool(t2) {
112 t.Error("get bool from string error")
113 }
114 if false != GetBool(nil) {
115 t.Error("get bool from nil error")
116 }
117 }
118
119 func TestGetByteArray(t *testing.T) {
120 var b = []byte("test")
121 var t1 = []byte("test")
122 if !byteArrayEquals(b, getByteArray(t1)) {
123 t.Error("get byte array from byte array error")
124 }
125 var t2 = "test"
126 if !byteArrayEquals(b, getByteArray(t2)) {
127 t.Error("get byte array from string error")
128 }
129 if nil != getByteArray(nil) {
130 t.Error("get byte array from nil error")
131 }
132 }
133
134 func byteArrayEquals(a []byte, b []byte) bool {
135 if len(a) != len(b) {
136 return false
137 }
138 for i, v := range a {
139 if v != b[i] {
140 return false
141 }
142 }
143 return true
144 }
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!