Commit ca1fd96e authored by astaxie's avatar astaxie

Merge pull request #687 from fuxiaohei/develop

code style simplify
parents 19c3a5b4 007db805
......@@ -51,7 +51,7 @@ func Register(name string, adapter Cache) {
if adapter == nil {
panic("cache: Register adapter is nil")
}
if _, dup := adapters[name]; dup {
if _, ok := adapters[name]; ok {
panic("cache: Register called twice for adapter " + name)
}
adapters[name] = adapter
......@@ -60,14 +60,15 @@ func Register(name string, adapter Cache) {
// Create a new cache driver by adapter name and config string.
// config need to be correct JSON as string: {"interval":360}.
// it will start gc automatically.
func NewCache(adapterName, config string) (Cache, error) {
func NewCache(adapterName, config string) (adapter Cache, e error) {
adapter, ok := adapters[adapterName]
if !ok {
return nil, fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName)
e = fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName)
return
}
err := adapter.StartAndGC(config)
if err != nil {
return nil, err
adapter = nil
}
return adapter, nil
return
}
......@@ -22,12 +22,11 @@ func GetString(v interface{}) string {
case []byte:
return string(result)
default:
if v == nil {
return ""
} else {
if v != nil {
return fmt.Sprintf("%v", result)
}
}
return ""
}
// convert interface to int.
......@@ -40,12 +39,9 @@ func GetInt(v interface{}) int {
case int64:
return int(result)
default:
d := GetString(v)
if d != "" {
value, err := strconv.Atoi(d)
if err == nil {
return value
}
if d := GetString(v); d != "" {
value, _ := strconv.Atoi(d)
return value
}
}
return 0
......@@ -61,12 +57,10 @@ func GetInt64(v interface{}) int64 {
case int64:
return result
default:
d := GetString(v)
if d != "" {
result, err := strconv.ParseInt(d, 10, 64)
if err == nil {
return result
}
if d := GetString(v); d != "" {
value, _ := strconv.ParseInt(d, 10, 64)
return value
}
}
return 0
......@@ -78,12 +72,9 @@ func GetFloat64(v interface{}) float64 {
case float64:
return result
default:
d := GetString(v)
if d != "" {
value, err := strconv.ParseFloat(d, 64)
if err == nil {
return value
}
if d := GetString(v); d != "" {
value, _ := strconv.ParseFloat(d, 64)
return value
}
}
return 0
......@@ -95,12 +86,9 @@ func GetBool(v interface{}) bool {
case bool:
return result
default:
d := GetString(v)
if d != "" {
result, err := strconv.ParseBool(d)
if err == nil {
return result
}
if d := GetString(v); d != "" {
value, _ := strconv.ParseBool(d)
return value
}
}
return false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment