007db805 by fuxiaohei

code style simplify

1 parent 19c3a5b4
...@@ -51,7 +51,7 @@ func Register(name string, adapter Cache) { ...@@ -51,7 +51,7 @@ func Register(name string, adapter Cache) {
51 if adapter == nil { 51 if adapter == nil {
52 panic("cache: Register adapter is nil") 52 panic("cache: Register adapter is nil")
53 } 53 }
54 if _, dup := adapters[name]; dup { 54 if _, ok := adapters[name]; ok {
55 panic("cache: Register called twice for adapter " + name) 55 panic("cache: Register called twice for adapter " + name)
56 } 56 }
57 adapters[name] = adapter 57 adapters[name] = adapter
...@@ -60,14 +60,15 @@ func Register(name string, adapter Cache) { ...@@ -60,14 +60,15 @@ func Register(name string, adapter Cache) {
60 // Create a new cache driver by adapter name and config string. 60 // Create a new cache driver by adapter name and config string.
61 // config need to be correct JSON as string: {"interval":360}. 61 // config need to be correct JSON as string: {"interval":360}.
62 // it will start gc automatically. 62 // it will start gc automatically.
63 func NewCache(adapterName, config string) (Cache, error) { 63 func NewCache(adapterName, config string) (adapter Cache, e error) {
64 adapter, ok := adapters[adapterName] 64 adapter, ok := adapters[adapterName]
65 if !ok { 65 if !ok {
66 return nil, fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName) 66 e = fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName)
67 return
67 } 68 }
68 err := adapter.StartAndGC(config) 69 err := adapter.StartAndGC(config)
69 if err != nil { 70 if err != nil {
70 return nil, err 71 adapter = nil
71 } 72 }
72 return adapter, nil 73 return
73 } 74 }
......
...@@ -22,12 +22,11 @@ func GetString(v interface{}) string { ...@@ -22,12 +22,11 @@ func GetString(v interface{}) string {
22 case []byte: 22 case []byte:
23 return string(result) 23 return string(result)
24 default: 24 default:
25 if v == nil { 25 if v != nil {
26 return ""
27 } else {
28 return fmt.Sprintf("%v", result) 26 return fmt.Sprintf("%v", result)
29 } 27 }
30 } 28 }
29 return ""
31 } 30 }
32 31
33 // convert interface to int. 32 // convert interface to int.
...@@ -40,14 +39,11 @@ func GetInt(v interface{}) int { ...@@ -40,14 +39,11 @@ func GetInt(v interface{}) int {
40 case int64: 39 case int64:
41 return int(result) 40 return int(result)
42 default: 41 default:
43 d := GetString(v) 42 if d := GetString(v); d != "" {
44 if d != "" { 43 value, _ := strconv.Atoi(d)
45 value, err := strconv.Atoi(d)
46 if err == nil {
47 return value 44 return value
48 } 45 }
49 } 46 }
50 }
51 return 0 47 return 0
52 } 48 }
53 49
...@@ -61,12 +57,10 @@ func GetInt64(v interface{}) int64 { ...@@ -61,12 +57,10 @@ func GetInt64(v interface{}) int64 {
61 case int64: 57 case int64:
62 return result 58 return result
63 default: 59 default:
64 d := GetString(v) 60
65 if d != "" { 61 if d := GetString(v); d != "" {
66 result, err := strconv.ParseInt(d, 10, 64) 62 value, _ := strconv.ParseInt(d, 10, 64)
67 if err == nil { 63 return value
68 return result
69 }
70 } 64 }
71 } 65 }
72 return 0 66 return 0
...@@ -78,14 +72,11 @@ func GetFloat64(v interface{}) float64 { ...@@ -78,14 +72,11 @@ func GetFloat64(v interface{}) float64 {
78 case float64: 72 case float64:
79 return result 73 return result
80 default: 74 default:
81 d := GetString(v) 75 if d := GetString(v); d != "" {
82 if d != "" { 76 value, _ := strconv.ParseFloat(d, 64)
83 value, err := strconv.ParseFloat(d, 64)
84 if err == nil {
85 return value 77 return value
86 } 78 }
87 } 79 }
88 }
89 return 0 80 return 0
90 } 81 }
91 82
...@@ -95,12 +86,9 @@ func GetBool(v interface{}) bool { ...@@ -95,12 +86,9 @@ func GetBool(v interface{}) bool {
95 case bool: 86 case bool:
96 return result 87 return result
97 default: 88 default:
98 d := GetString(v) 89 if d := GetString(v); d != "" {
99 if d != "" { 90 value, _ := strconv.ParseBool(d)
100 result, err := strconv.ParseBool(d) 91 return value
101 if err == nil {
102 return result
103 }
104 } 92 }
105 } 93 }
106 return false 94 return false
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!