diff --git a/cflag/ext.go b/cflag/ext.go index 4c34e8efe..219358b62 100644 --- a/cflag/ext.go +++ b/cflag/ext.go @@ -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 diff --git a/maputil/data.go b/maputil/data.go index 07008e78f..379657af5 100644 --- a/maputil/data.go +++ b/maputil/data.go @@ -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)) diff --git a/maputil/smap.go b/maputil/smap.go index bcc74cc5e..8905825b5 100644 --- a/maputil/smap.go +++ b/maputil/smap.go @@ -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))