Skip to content

Commit

Permalink
👔 up: map - add some new method for Map, SMap data
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 31, 2023
1 parent b8698a4 commit 678218d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cflag/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func (s *KVString) Get() any {

// Data map get
func (s *KVString) Data() maputil.SMap {
return s.SMap
return s.Init().SMap
}

// Set new value, will check value is right
Expand Down
10 changes: 10 additions & 0 deletions maputil/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ func (d Data) Sub(key string) Data {
return nil
}

// Slice get []any value from data map
func (d Data) Slice(key string) ([]any, error) {
val, ok := d.GetByPath(key)
if !ok {
return nil, nil
}

return arrutil.AnyToSlice(val)
}

// Keys of the data map
func (d Data) Keys() []string {
keys := make([]string, 0, len(d))
Expand Down
14 changes: 14 additions & 0 deletions maputil/smap.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ func (m SMap) Strings(key string) (ss []string) {
return
}

// IfExist key, then call the fn with value.
func (m SMap) IfExist(key string, fn func(val string)) {
if val, ok := m[key]; ok {
fn(val)
}
}

// IfValid value is not empty, then call the fn
func (m SMap) IfValid(key string, fn func(val string)) {
if val, ok := m[key]; ok && val != "" {
fn(val)
}
}

// Keys of the string-map
func (m SMap) Keys() []string {
keys := make([]string, 0, len(m))
Expand Down

0 comments on commit 678218d

Please sign in to comment.