build(deps): bump github.com/gookit/config/v2 from 2.2.6 to 2.2.7
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.2.6 to 2.2.7. - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.2.6...v2.2.7) --- updated-dependencies: - dependency-name: github.com/gookit/config/v2 dependency-version: 2.2.7 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com>
This commit is contained in:
+106
-7
@@ -11,8 +11,20 @@ import (
|
||||
"github.com/gookit/goutil/strutil"
|
||||
)
|
||||
|
||||
// alias functions
|
||||
var (
|
||||
// ToStrMap convert map[string]any to map[string]string
|
||||
ToStrMap = ToStringMap
|
||||
// ToL2StrMap convert map[string]any to map[string]map[string]string
|
||||
ToL2StrMap = ToL2StringMap
|
||||
)
|
||||
|
||||
// KeyToLower convert keys to lower case.
|
||||
func KeyToLower(src map[string]string) map[string]string {
|
||||
if len(src) == 0 {
|
||||
return src
|
||||
}
|
||||
|
||||
newMp := make(map[string]string, len(src))
|
||||
for k, v := range src {
|
||||
k = strings.ToLower(k)
|
||||
@@ -21,7 +33,22 @@ func KeyToLower(src map[string]string) map[string]string {
|
||||
return newMp
|
||||
}
|
||||
|
||||
// ToStringMap convert map[string]any to map[string]string
|
||||
// AnyToStrMap try convert any(map[string]any, map[string]string) to map[string]string
|
||||
func AnyToStrMap(src any) map[string]string {
|
||||
if src == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if m, ok := src.(map[string]string); ok {
|
||||
return m
|
||||
}
|
||||
if m, ok := src.(map[string]any); ok {
|
||||
return ToStringMap(m)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ToStringMap simple convert map[string]any to map[string]string
|
||||
func ToStringMap(src map[string]any) map[string]string {
|
||||
strMp := make(map[string]string, len(src))
|
||||
for k, v := range src {
|
||||
@@ -30,7 +57,25 @@ func ToStringMap(src map[string]any) map[string]string {
|
||||
return strMp
|
||||
}
|
||||
|
||||
// CombineToSMap combine two string-slice to SMap(map[string]string)
|
||||
// ToL2StringMap convert map[string]any to map[string]map[string]string
|
||||
func ToL2StringMap(groupsMap map[string]any) map[string]map[string]string {
|
||||
if len(groupsMap) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
l2sMap := make(map[string]map[string]string, len(groupsMap))
|
||||
|
||||
for k, v := range groupsMap {
|
||||
if mp, ok := v.(map[string]any); ok {
|
||||
l2sMap[k] = ToStringMap(mp)
|
||||
} else if smp, ok := v.(map[string]string); ok {
|
||||
l2sMap[k] = smp
|
||||
}
|
||||
}
|
||||
return l2sMap
|
||||
}
|
||||
|
||||
// CombineToSMap combine two string-slices to SMap(map[string]string)
|
||||
func CombineToSMap(keys, values []string) SMap {
|
||||
return arrutil.CombineToSMap(keys, values)
|
||||
}
|
||||
@@ -40,6 +85,54 @@ func CombineToMap[K comdef.SortedType, V any](keys []K, values []V) map[K]V {
|
||||
return arrutil.CombineToMap(keys, values)
|
||||
}
|
||||
|
||||
// SliceToSMap convert string k-v pairs slice to map[string]string
|
||||
// - eg: []string{k1,v1,k2,v2} -> map[string]string{k1:v1, k2:v2}
|
||||
func SliceToSMap(kvPairs ...string) map[string]string {
|
||||
ln := len(kvPairs)
|
||||
// check kvPairs length must be even
|
||||
if ln == 0 || ln%2 != 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
sMap := make(map[string]string, ln/2)
|
||||
for i := 0; i < ln; i += 2 {
|
||||
sMap[kvPairs[i]] = kvPairs[i+1]
|
||||
}
|
||||
return sMap
|
||||
}
|
||||
|
||||
// SliceToMap convert any k-v pairs slice to map[string]any
|
||||
func SliceToMap(kvPairs ...any) map[string]any {
|
||||
ln := len(kvPairs)
|
||||
// check kvPairs length must be even
|
||||
if ln == 0 || ln%2 != 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
mp := make(map[string]any, ln/2)
|
||||
for i := 0; i < ln; i += 2 {
|
||||
kStr := strutil.SafeString(kvPairs[i])
|
||||
mp[kStr] = kvPairs[i+1]
|
||||
}
|
||||
return mp
|
||||
}
|
||||
|
||||
// SliceToTypeMap convert k-v pairs slice to map[string]T
|
||||
func SliceToTypeMap[T any](valFunc func(any) T, kvPairs ...any) map[string]T {
|
||||
ln := len(kvPairs)
|
||||
// check kvPairs length must be even
|
||||
if ln == 0 || ln%2 != 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
mp := make(map[string]T, ln/2)
|
||||
for i := 0; i < ln; i += 2 {
|
||||
kStr := strutil.SafeString(kvPairs[i])
|
||||
mp[kStr] = valFunc(kvPairs[i+1])
|
||||
}
|
||||
return mp
|
||||
}
|
||||
|
||||
// ToAnyMap convert map[TYPE1]TYPE2 to map[string]any
|
||||
func ToAnyMap(mp any) map[string]any {
|
||||
amp, _ := TryAnyMap(mp)
|
||||
@@ -51,15 +144,23 @@ func TryAnyMap(mp any) (map[string]any, error) {
|
||||
if aMp, ok := mp.(map[string]any); ok {
|
||||
return aMp, nil
|
||||
}
|
||||
if sMp, ok := mp.(map[string]string); ok {
|
||||
anyMp := make(map[string]any, len(sMp))
|
||||
for k, v := range sMp {
|
||||
anyMp[k] = v
|
||||
}
|
||||
return anyMp, nil
|
||||
}
|
||||
|
||||
rv := reflect.Indirect(reflect.ValueOf(mp))
|
||||
if rv.Kind() != reflect.Map {
|
||||
return nil, errors.New("input is not a map value")
|
||||
return nil, errors.New("input is not a map value type")
|
||||
}
|
||||
|
||||
anyMp := make(map[string]any, rv.Len())
|
||||
for _, key := range rv.MapKeys() {
|
||||
anyMp[key.String()] = rv.MapIndex(key).Interface()
|
||||
keyStr := strutil.SafeString(key.Interface())
|
||||
anyMp[keyStr] = rv.MapIndex(key).Interface()
|
||||
}
|
||||
return anyMp, nil
|
||||
}
|
||||
@@ -125,9 +226,7 @@ func ToString(mp map[string]any) string {
|
||||
}
|
||||
|
||||
// ToString2 simple and quickly convert a map to string.
|
||||
func ToString2(mp any) string {
|
||||
return NewFormatter(mp).Format()
|
||||
}
|
||||
func ToString2(mp any) string { return NewFormatter(mp).Format() }
|
||||
|
||||
// FormatIndent format map data to string with newline and indent.
|
||||
func FormatIndent(mp any, indent string) string {
|
||||
|
||||
+127
-70
@@ -4,6 +4,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gookit/goutil/arrutil"
|
||||
"github.com/gookit/goutil/internal/comfunc"
|
||||
"github.com/gookit/goutil/mathutil"
|
||||
"github.com/gookit/goutil/strutil"
|
||||
)
|
||||
@@ -25,37 +26,10 @@ func (d Data) IsEmpty() bool {
|
||||
return len(d) == 0
|
||||
}
|
||||
|
||||
// Value get from the data map
|
||||
func (d Data) Value(key string) (any, bool) {
|
||||
val, ok := d.GetByPath(key)
|
||||
return val, ok
|
||||
}
|
||||
|
||||
// Get value from the data map.
|
||||
// Supports dot syntax to get deep values. eg: top.sub
|
||||
func (d Data) Get(key string) any {
|
||||
if val, ok := d.GetByPath(key); ok {
|
||||
return val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetByPath get value from the data map by path. eg: top.sub
|
||||
// Supports dot syntax to get deep values.
|
||||
func (d Data) GetByPath(path string) (any, bool) {
|
||||
if val, ok := d[path]; ok {
|
||||
return val, true
|
||||
}
|
||||
|
||||
// is key path.
|
||||
if strings.ContainsRune(path, '.') {
|
||||
val, ok := GetByPath(path, d)
|
||||
if ok {
|
||||
return val, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
//
|
||||
// endregion
|
||||
// region T: set value(s)
|
||||
//
|
||||
|
||||
// Set value to the data map
|
||||
func (d Data) Set(key string, val any) {
|
||||
@@ -65,7 +39,7 @@ func (d Data) Set(key string, val any) {
|
||||
// SetByPath sets a value in the map.
|
||||
// Supports dot syntax to set deep values.
|
||||
//
|
||||
// For example:
|
||||
// Example:
|
||||
//
|
||||
// d.SetByPath("name.first", "Mat")
|
||||
func (d Data) SetByPath(path string, value any) error {
|
||||
@@ -78,7 +52,7 @@ func (d Data) SetByPath(path string, value any) error {
|
||||
// SetByKeys sets a value in the map by path keys.
|
||||
// Supports dot syntax to set deep values.
|
||||
//
|
||||
// For example:
|
||||
// Example:
|
||||
//
|
||||
// d.SetByKeys([]string{"name", "first"}, "Mat")
|
||||
func (d Data) SetByKeys(keys []string, value any) error {
|
||||
@@ -102,6 +76,61 @@ func (d Data) SetByKeys(keys []string, value any) error {
|
||||
// return SetByKeys((*map[string]any)(d), keys, value)
|
||||
}
|
||||
|
||||
//
|
||||
// endregion
|
||||
// region T: read value(s)
|
||||
//
|
||||
|
||||
// Value get from the data map
|
||||
func (d Data) Value(key string) (any, bool) {
|
||||
val, ok := d.GetByPath(key)
|
||||
return val, ok
|
||||
}
|
||||
|
||||
// Get value from the data map.
|
||||
// Supports dot syntax to get deep values. eg: top.sub
|
||||
func (d Data) Get(key string) any {
|
||||
if val, ok := d.GetByPath(key); ok {
|
||||
return val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// One get value from the data by multi paths. will return first founded value
|
||||
func (d Data) One(keys ...string) any {
|
||||
if val, ok := d.TryOne(keys...); ok {
|
||||
return val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TryOne get value from the data by multi paths. will return first founded value
|
||||
func (d Data) TryOne(keys ...string) (any, bool) {
|
||||
for _, path := range keys {
|
||||
if val, ok := d.GetByPath(path); ok {
|
||||
return val, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// GetByPath get value from the data map by path. eg: top.sub
|
||||
// Supports dot syntax to get deep values.
|
||||
func (d Data) GetByPath(path string) (any, bool) {
|
||||
if val, ok := d[path]; ok {
|
||||
return val, true
|
||||
}
|
||||
|
||||
// is a key path.
|
||||
if strings.ContainsRune(path, '.') {
|
||||
val, ok := GetByPath(path, d)
|
||||
if ok {
|
||||
return val, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Default get value from the data map with default value
|
||||
func (d Data) Default(key string, def any) any {
|
||||
if val, ok := d.GetByPath(key); ok {
|
||||
@@ -142,10 +171,20 @@ func (d Data) Uint64(key string) uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Str value get by key
|
||||
// Str value gets by key
|
||||
func (d Data) Str(key string) string {
|
||||
if val, ok := d.GetByPath(key); ok {
|
||||
return strutil.QuietString(val)
|
||||
return strutil.SafeString(val)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// StrOne value gets by multi keys, will return first value
|
||||
func (d Data) StrOne(keys ...string) string {
|
||||
for _, key := range keys {
|
||||
if val, ok := d.GetByPath(key); ok {
|
||||
return strutil.SafeString(val)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -156,45 +195,46 @@ func (d Data) Bool(key string) bool {
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
switch tv := val.(type) {
|
||||
case string:
|
||||
return strutil.QuietBool(tv)
|
||||
case bool:
|
||||
return tv
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return comfunc.Bool(val)
|
||||
}
|
||||
|
||||
// Strings get []string value
|
||||
func (d Data) Strings(key string) []string {
|
||||
val, ok := d.GetByPath(key)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch typVal := val.(type) {
|
||||
case string:
|
||||
return []string{typVal}
|
||||
case []string:
|
||||
return typVal
|
||||
case []any:
|
||||
return arrutil.SliceToStrings(typVal)
|
||||
default:
|
||||
return nil
|
||||
// BoolOne value gets from multi keys, return first value
|
||||
func (d Data) BoolOne(keys ...string) bool {
|
||||
for _, key := range keys {
|
||||
if val, ok := d.GetByPath(key); ok {
|
||||
return comfunc.Bool(val)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// StrSplit get strings by split key value
|
||||
func (d Data) StrSplit(key, sep string) []string {
|
||||
if val, ok := d.GetByPath(key); ok {
|
||||
return strings.Split(strutil.QuietString(val), sep)
|
||||
// StringsOne get []string value by multi keys, return first founded value
|
||||
func (d Data) StringsOne(keys ...string) []string {
|
||||
for _, key := range keys {
|
||||
if val, ok := d.GetByPath(key); ok {
|
||||
return arrutil.AnyToStrings(val)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// StringsByStr value get by key
|
||||
// Strings get []string value by key
|
||||
func (d Data) Strings(key string) []string {
|
||||
if val, ok := d.GetByPath(key); ok {
|
||||
return arrutil.AnyToStrings(val)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// StrSplit get strings by split string value
|
||||
func (d Data) StrSplit(key, sep string) []string {
|
||||
if val, ok := d.GetByPath(key); ok {
|
||||
return strings.Split(strutil.SafeString(val), sep)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// StringsByStr value gets by key, will split string value by ","
|
||||
func (d Data) StringsByStr(key string) []string {
|
||||
return d.StrSplit(key, ",")
|
||||
}
|
||||
@@ -221,23 +261,40 @@ func (d Data) StringMap(key string) map[string]string {
|
||||
}
|
||||
}
|
||||
|
||||
// Sub get sub value as new Data
|
||||
// Sub get sub value(map[string]any) as new Data
|
||||
func (d Data) Sub(key string) Data {
|
||||
if val, ok := d.GetByPath(key); ok {
|
||||
if sub, ok := val.(map[string]any); ok {
|
||||
return sub
|
||||
}
|
||||
return d.toAnyMap(val)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AnyMap get sub value as map[string]any
|
||||
func (d Data) AnyMap(key string) map[string]any {
|
||||
if val, ok := d.GetByPath(key); ok {
|
||||
return d.toAnyMap(val)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AnyMap get sub value as map[string]any
|
||||
func (d Data) toAnyMap(val any) map[string]any {
|
||||
switch tv := val.(type) {
|
||||
case map[string]string:
|
||||
return ToAnyMap(tv)
|
||||
case map[string]any:
|
||||
return tv
|
||||
default:
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
+8
@@ -202,6 +202,14 @@ func TypedKeys[K comdef.SimpleType, V any](mp map[K]V) (keys []K) {
|
||||
return
|
||||
}
|
||||
|
||||
// FirstKey returns the first key of the given map.
|
||||
func FirstKey[T any](mp map[string]T) string {
|
||||
for key := range mp {
|
||||
return key
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Values get all values from the given map.
|
||||
func Values(mp any) (values []any) {
|
||||
rv := reflect.Indirect(reflect.ValueOf(mp))
|
||||
|
||||
+24
@@ -58,6 +58,11 @@ func MergeSMap(src, dst map[string]string, ignoreCase bool) map[string]string {
|
||||
return MergeStringMap(src, dst, ignoreCase)
|
||||
}
|
||||
|
||||
// MergeStrMap simple merge two string map. merge src to dst map
|
||||
func MergeStrMap(src, dst map[string]string) map[string]string {
|
||||
return MergeStringMap(src, dst, false)
|
||||
}
|
||||
|
||||
// MergeStringMap simple merge two string map. merge src to dst map
|
||||
func MergeStringMap(src, dst map[string]string, ignoreCase bool) map[string]string {
|
||||
if len(src) == 0 {
|
||||
@@ -87,6 +92,25 @@ func MergeMultiSMap(mps ...map[string]string) map[string]string {
|
||||
return newMp
|
||||
}
|
||||
|
||||
// MergeL2StrMap merge multi level2 string-map data. The back map covers the front.
|
||||
func MergeL2StrMap(mps ...map[string]map[string]string) map[string]map[string]string {
|
||||
newMp := make(map[string]map[string]string)
|
||||
for _, mp := range mps {
|
||||
for k, v := range mp {
|
||||
// merge level 2 value
|
||||
if oldV, ok := newMp[k]; ok {
|
||||
for k1, v1 := range v {
|
||||
oldV[k1] = v1
|
||||
}
|
||||
newMp[k] = oldV
|
||||
} else {
|
||||
newMp[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
return newMp
|
||||
}
|
||||
|
||||
// FilterSMap filter empty elem for the string map.
|
||||
func FilterSMap(sm map[string]string) map[string]string {
|
||||
for key, val := range sm {
|
||||
|
||||
+3
-3
@@ -171,7 +171,7 @@ func setMapByKeys(rv reflect.Value, keys []string, nv reflect.Value) (err error)
|
||||
|
||||
if isSlice {
|
||||
// key is slice index
|
||||
if strutil.IsNumeric(key) {
|
||||
if strutil.IsInt(key) {
|
||||
idx, _ = strconv.Atoi(key)
|
||||
}
|
||||
|
||||
@@ -231,7 +231,7 @@ func setMapByKeys(rv reflect.Value, keys []string, nv reflect.Value) (err error)
|
||||
|
||||
// next key is index number.
|
||||
nxtKey := keys[i+1]
|
||||
if strutil.IsNumeric(nxtKey) {
|
||||
if strutil.IsInt(nxtKey) {
|
||||
idx, _ = strconv.Atoi(nxtKey)
|
||||
sliLen := tmpV.Len()
|
||||
wantLen := idx + 1
|
||||
@@ -275,7 +275,7 @@ func setMapByKeys(rv reflect.Value, keys []string, nv reflect.Value) (err error)
|
||||
}
|
||||
|
||||
break
|
||||
} else if isSlice && strutil.IsNumeric(key) { // (E). slice from ptr slice
|
||||
} else if isSlice && strutil.IsInt(key) { // (E). slice from ptr slice
|
||||
idx, _ = strconv.Atoi(key)
|
||||
sliLen := rv.Len()
|
||||
wantLen := idx + 1
|
||||
|
||||
+36
-25
@@ -5,22 +5,23 @@ import (
|
||||
"github.com/gookit/goutil/strutil"
|
||||
)
|
||||
|
||||
// SMap is alias of map[string]string
|
||||
type SMap map[string]string
|
||||
// SMap and StrMap is alias of map[string]string
|
||||
type SMap = StrMap
|
||||
type StrMap map[string]string
|
||||
|
||||
// IsEmpty of the data map
|
||||
func (m SMap) IsEmpty() bool {
|
||||
func (m StrMap) IsEmpty() bool {
|
||||
return len(m) == 0
|
||||
}
|
||||
|
||||
// Has key on the data map
|
||||
func (m SMap) Has(key string) bool {
|
||||
func (m StrMap) Has(key string) bool {
|
||||
_, ok := m[key]
|
||||
return ok
|
||||
}
|
||||
|
||||
// HasValue on the data map
|
||||
func (m SMap) HasValue(val string) bool {
|
||||
func (m StrMap) HasValue(val string) bool {
|
||||
for _, v := range m {
|
||||
if v == val {
|
||||
return true
|
||||
@@ -30,25 +31,25 @@ func (m SMap) HasValue(val string) bool {
|
||||
}
|
||||
|
||||
// Load data to the map
|
||||
func (m SMap) Load(data map[string]string) {
|
||||
func (m StrMap) Load(data map[string]string) {
|
||||
for k, v := range data {
|
||||
m[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
// Set value to the data map
|
||||
func (m SMap) Set(key string, val any) {
|
||||
func (m StrMap) Set(key string, val any) {
|
||||
m[key] = strutil.MustString(val)
|
||||
}
|
||||
|
||||
// Value get from the data map
|
||||
func (m SMap) Value(key string) (string, bool) {
|
||||
func (m StrMap) Value(key string) (string, bool) {
|
||||
val, ok := m[key]
|
||||
return val, ok
|
||||
}
|
||||
|
||||
// Default get value by key. if not found, return defVal
|
||||
func (m SMap) Default(key, defVal string) string {
|
||||
func (m StrMap) Default(key, defVal string) string {
|
||||
if val, ok := m[key]; ok {
|
||||
return val
|
||||
}
|
||||
@@ -56,41 +57,51 @@ func (m SMap) Default(key, defVal string) string {
|
||||
}
|
||||
|
||||
// Get value by key
|
||||
func (m SMap) Get(key string) string {
|
||||
func (m StrMap) Get(key string) string {
|
||||
return m[key]
|
||||
}
|
||||
|
||||
// Int value get
|
||||
func (m SMap) Int(key string) int {
|
||||
func (m StrMap) Int(key string) int {
|
||||
if val, ok := m[key]; ok {
|
||||
return mathutil.QuietInt(val)
|
||||
return mathutil.SafeInt(val)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Int64 value get
|
||||
func (m SMap) Int64(key string) int64 {
|
||||
func (m StrMap) Int64(key string) int64 {
|
||||
if val, ok := m[key]; ok {
|
||||
return mathutil.QuietInt64(val)
|
||||
return mathutil.SafeInt64(val)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Str value get
|
||||
func (m SMap) Str(key string) string {
|
||||
func (m StrMap) Str(key string) string {
|
||||
return m[key]
|
||||
}
|
||||
|
||||
// StrOne get first founded value by keys
|
||||
func (m SMap) StrOne(keys ...string) string {
|
||||
for _, key := range keys {
|
||||
if val, ok := m[key]; ok {
|
||||
return val
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Bool value get
|
||||
func (m SMap) Bool(key string) bool {
|
||||
func (m StrMap) Bool(key string) bool {
|
||||
if val, ok := m[key]; ok {
|
||||
return strutil.QuietBool(val)
|
||||
return strutil.SafeBool(val)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Ints value to []int
|
||||
func (m SMap) Ints(key string) []int {
|
||||
func (m StrMap) Ints(key string) []int {
|
||||
if val, ok := m[key]; ok {
|
||||
return strutil.Ints(val, ValSepStr)
|
||||
}
|
||||
@@ -98,7 +109,7 @@ func (m SMap) Ints(key string) []int {
|
||||
}
|
||||
|
||||
// Strings value to []string
|
||||
func (m SMap) Strings(key string) (ss []string) {
|
||||
func (m StrMap) Strings(key string) (ss []string) {
|
||||
if val, ok := m[key]; ok {
|
||||
return strutil.ToSlice(val, ValSepStr)
|
||||
}
|
||||
@@ -106,21 +117,21 @@ func (m SMap) Strings(key string) (ss []string) {
|
||||
}
|
||||
|
||||
// IfExist key, then call the fn with value.
|
||||
func (m SMap) IfExist(key string, fn func(val string)) {
|
||||
func (m StrMap) 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)) {
|
||||
func (m StrMap) 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 {
|
||||
func (m StrMap) Keys() []string {
|
||||
keys := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
@@ -129,7 +140,7 @@ func (m SMap) Keys() []string {
|
||||
}
|
||||
|
||||
// Values of the string-map
|
||||
func (m SMap) Values() []string {
|
||||
func (m StrMap) Values() []string {
|
||||
ss := make([]string, 0, len(m))
|
||||
for _, v := range m {
|
||||
ss = append(ss, v)
|
||||
@@ -138,7 +149,7 @@ func (m SMap) Values() []string {
|
||||
}
|
||||
|
||||
// ToKVPairs slice convert. eg: {k1:v1,k2:v2} => {k1,v1,k2,v2}
|
||||
func (m SMap) ToKVPairs() []string {
|
||||
func (m StrMap) ToKVPairs() []string {
|
||||
pairs := make([]string, 0, len(m)*2)
|
||||
for k, v := range m {
|
||||
pairs = append(pairs, k, v)
|
||||
@@ -147,6 +158,6 @@ func (m SMap) ToKVPairs() []string {
|
||||
}
|
||||
|
||||
// String data to string
|
||||
func (m SMap) String() string {
|
||||
func (m StrMap) String() string {
|
||||
return ToString2(m)
|
||||
}
|
||||
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package maputil
|
||||
|
||||
import "github.com/gookit/goutil/strutil"
|
||||
|
||||
// L2StrMap is alias of map[string]map[string]string
|
||||
type L2StrMap map[string]map[string]string
|
||||
|
||||
// Load data, merge new data to old
|
||||
func (m L2StrMap) Load(mp map[string]map[string]string) {
|
||||
for k, v := range mp {
|
||||
if oldV, ok := m[k]; ok {
|
||||
for k1, v1 := range v {
|
||||
oldV[k1] = v1
|
||||
}
|
||||
m[k] = oldV
|
||||
} else {
|
||||
m[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Value get by key path. eg: "top.sub"
|
||||
func (m L2StrMap) Value(key string) (val string, ok bool) {
|
||||
top, sub, found := strutil.Cut(key, KeySepStr)
|
||||
if !found {
|
||||
return "", false
|
||||
}
|
||||
|
||||
if vals, ok1 := m[top]; ok1 {
|
||||
val, ok = vals[sub]
|
||||
return
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Get value by key path. eg: "top.sub"
|
||||
func (m L2StrMap) Get(key string) string {
|
||||
val, _ := m.Value(key)
|
||||
return val
|
||||
}
|
||||
|
||||
// Exists check key path exists. eg: "top.sub"
|
||||
func (m L2StrMap) Exists(key string) bool {
|
||||
_, ok := m.Value(key)
|
||||
return ok
|
||||
}
|
||||
|
||||
// StrMap get by top key. eg: "top"
|
||||
func (m L2StrMap) StrMap(top string) StrMap {
|
||||
return m[top]
|
||||
}
|
||||
Reference in New Issue
Block a user