diff --git a/fsutil/operate.go b/fsutil/operate.go index ea59c9494..8c99110d8 100644 --- a/fsutil/operate.go +++ b/fsutil/operate.go @@ -262,14 +262,14 @@ func Unzip(archive, targetDir string) (err error) { targetFile, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode()) if err != nil { - fileReader.Close() + _ = fileReader.Close() return err } _, err = io.Copy(targetFile, fileReader) // close all - fileReader.Close() + _ = fileReader.Close() targetFile.Close() if err != nil { diff --git a/maputil/data.go b/maputil/data.go index 7a2d9edb1..131bce33f 100644 --- a/maputil/data.go +++ b/maputil/data.go @@ -10,17 +10,6 @@ import ( // Data an map data type type Data map[string]interface{} -// Value get from the data map -func (d Data) Value(key string) (interface{}, bool) { - val, ok := d[key] - return val, ok -} - -// Get value from the data map -func (d Data) Get(key string) interface{} { - return d[key] -} - // Set value to the data map func (d Data) Set(key string, val interface{}) { d[key] = val @@ -37,6 +26,31 @@ func (d Data) Emtpy() bool { return len(d) == 0 } +// Get value from the data map +func (d Data) Get(key string) interface{} { + return d[key] +} + +// Value get from the data map +func (d Data) Value(key string) (interface{}, bool) { + val, ok := d[key] + return val, ok +} + +// GetByPath get value from the data map by path. eg: top.sub +func (d Data) GetByPath(path string) (interface{}, bool) { + return GetByPath(path, d) +} + +// Default get value from the data map with default value +func (d Data) Default(key string, def interface{}) interface{} { + val, ok := d[key] + if ok { + return val + } + return def +} + // Int value get func (d Data) Int(key string) int { val, ok := d[key] @@ -44,7 +58,7 @@ func (d Data) Int(key string) int { return 0 } - return mathutil.MustInt(val) + return mathutil.QuietInt(val) } // Int64 value get @@ -54,7 +68,7 @@ func (d Data) Int64(key string) int64 { return 0 } - return mathutil.MustInt64(val) + return mathutil.QuietInt64(val) } // Str value get by key @@ -63,7 +77,7 @@ func (d Data) Str(key string) string { if !ok { return "" } - return strutil.MustString(val) + return strutil.QuietString(val) } // Bool value get @@ -82,15 +96,6 @@ func (d Data) Bool(key string) bool { return false } -// Default get value from the data map with default value -func (d Data) Default(key string, def interface{}) interface{} { - val, ok := d[key] - if ok { - return val - } - return def -} - // Strings get []string value func (d Data) Strings(key string) []string { val, ok := d[key] @@ -111,7 +116,7 @@ func (d Data) StringsByStr(key string) []string { return nil } - str := strutil.MustString(val) + str := strutil.QuietString(val) return strings.Split(str, ",") } diff --git a/maputil/data_test.go b/maputil/data_test.go index 97bdd87af..915aeba71 100644 --- a/maputil/data_test.go +++ b/maputil/data_test.go @@ -33,6 +33,10 @@ func TestData(t *testing.T) { mp.Set("new", "val1") assert.Equal(t, "val1", mp.Str("new")) + val, ok := mp.Value("new") + assert.True(t, ok) + assert.Equal(t, "val1", val) + // not exists assert.False(t, mp.Bool("notExists")) assert.Equal(t, 0, mp.Int("notExists")) diff --git a/maputil/maputil.go b/maputil/maputil.go index 0a557c107..54c79300a 100644 --- a/maputil/maputil.go +++ b/maputil/maputil.go @@ -52,7 +52,7 @@ func GetByPath(key string, mp map[string]interface{}) (val interface{}, ok bool) if item, ok = tData[k]; !ok { return } - case []interface{}: // is an slice + case []interface{}: // is a slice if item, ok = getBySlice(k, tData); !ok { return }