Skip to content

Commit

Permalink
up: update some for map data use logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 9, 2022
1 parent bddc281 commit 62ef51c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
4 changes: 2 additions & 2 deletions fsutil/operate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
53 changes: 29 additions & 24 deletions maputil/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,14 +26,39 @@ 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]
if !ok {
return 0
}

return mathutil.MustInt(val)
return mathutil.QuietInt(val)
}

// Int64 value get
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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, ",")
}

Expand Down
4 changes: 4 additions & 0 deletions maputil/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion maputil/maputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 62ef51c

Please sign in to comment.