diff --git a/mapx/type_assert.go b/mapx/type_assert.go index 67118303..f76de985 100644 --- a/mapx/type_assert.go +++ b/mapx/type_assert.go @@ -18,7 +18,7 @@ var ErrKeyDoesNotExist = errors.New("key is not present in map") var ErrKeyCanNotBeTypeAsserted = errors.New("key could not be type asserted") // GetString returns a string for a given key in values. -func GetString(values map[interface{}]interface{}, key interface{}) (string, error) { +func GetString[K comparable](values map[K]any, key K) (string, error) { if v, ok := values[key]; !ok { return "", ErrKeyDoesNotExist } else if sv, ok := v.(string); !ok { @@ -29,12 +29,12 @@ func GetString(values map[interface{}]interface{}, key interface{}) (string, err } // GetStringSlice returns a string slice for a given key in values. -func GetStringSlice(values map[interface{}]interface{}, key interface{}) ([]string, error) { +func GetStringSlice[K comparable](values map[K]any, key K) ([]string, error) { if v, ok := values[key]; !ok { return []string{}, ErrKeyDoesNotExist } else if sv, ok := v.([]string); ok { return sv, nil - } else if sv, ok := v.([]interface{}); ok { + } else if sv, ok := v.([]any); ok { vs := make([]string, len(sv)) for k, v := range sv { vv, ok := v.(string) @@ -49,7 +49,7 @@ func GetStringSlice(values map[interface{}]interface{}, key interface{}) ([]stri } // GetTime returns a string slice for a given key in values. -func GetTime(values map[interface{}]interface{}, key interface{}) (time.Time, error) { +func GetTime[K comparable](values map[K]any, key K) (time.Time, error) { v, ok := values[key] if !ok { return time.Time{}, ErrKeyDoesNotExist @@ -73,7 +73,7 @@ func GetTime(values map[interface{}]interface{}, key interface{}) (time.Time, er } // GetInt64Default returns a int64 or the default value for a given key in values. -func GetInt64Default(values map[interface{}]interface{}, key interface{}, defaultValue int64) int64 { +func GetInt64Default[K comparable](values map[K]any, key K, defaultValue int64) int64 { f, err := GetInt64(values, key) if err != nil { return defaultValue @@ -82,7 +82,7 @@ func GetInt64Default(values map[interface{}]interface{}, key interface{}, defaul } // GetInt64 returns an int64 for a given key in values. -func GetInt64(values map[interface{}]interface{}, key interface{}) (int64, error) { +func GetInt64[K comparable](values map[K]any, key K) (int64, error) { v, ok := values[key] if !ok { return 0, ErrKeyDoesNotExist @@ -113,7 +113,7 @@ func GetInt64(values map[interface{}]interface{}, key interface{}) (int64, error } // GetInt32Default returns a int32 or the default value for a given key in values. -func GetInt32Default(values map[interface{}]interface{}, key interface{}, defaultValue int32) int32 { +func GetInt32Default[K comparable](values map[K]any, key K, defaultValue int32) int32 { f, err := GetInt32(values, key) if err != nil { return defaultValue @@ -122,7 +122,7 @@ func GetInt32Default(values map[interface{}]interface{}, key interface{}, defaul } // GetInt32 returns an int32 for a given key in values. -func GetInt32(values map[interface{}]interface{}, key interface{}) (int32, error) { +func GetInt32[K comparable](values map[K]any, key K) (int32, error) { v, err := GetInt64(values, key) if err != nil { return 0, err @@ -134,7 +134,7 @@ func GetInt32(values map[interface{}]interface{}, key interface{}) (int32, error } // GetIntDefault returns a int or the default value for a given key in values. -func GetIntDefault(values map[interface{}]interface{}, key interface{}, defaultValue int) int { +func GetIntDefault[K comparable](values map[K]any, key K, defaultValue int) int { f, err := GetInt(values, key) if err != nil { return defaultValue @@ -143,7 +143,7 @@ func GetIntDefault(values map[interface{}]interface{}, key interface{}, defaultV } // GetInt returns an int for a given key in values. -func GetInt(values map[interface{}]interface{}, key interface{}) (int, error) { +func GetInt[K comparable](values map[K]any, key K) (int, error) { v, err := GetInt64(values, key) if err != nil { return 0, err @@ -155,7 +155,7 @@ func GetInt(values map[interface{}]interface{}, key interface{}) (int, error) { } // GetFloat32Default returns a float32 or the default value for a given key in values. -func GetFloat32Default(values map[interface{}]interface{}, key interface{}, defaultValue float32) float32 { +func GetFloat32Default[K comparable](values map[K]any, key K, defaultValue float32) float32 { f, err := GetFloat32(values, key) if err != nil { return defaultValue @@ -164,7 +164,7 @@ func GetFloat32Default(values map[interface{}]interface{}, key interface{}, defa } // GetFloat32 returns a float32 for a given key in values. -func GetFloat32(values map[interface{}]interface{}, key interface{}) (float32, error) { +func GetFloat32[K comparable](values map[K]any, key K) (float32, error) { if v, ok := values[key]; !ok { return 0, ErrKeyDoesNotExist } else if j, ok := v.(json.Number); ok { @@ -177,7 +177,7 @@ func GetFloat32(values map[interface{}]interface{}, key interface{}) (float32, e } // GetFloat64Default returns a float64 or the default value for a given key in values. -func GetFloat64Default(values map[interface{}]interface{}, key interface{}, defaultValue float64) float64 { +func GetFloat64Default[K comparable](values map[K]any, key K, defaultValue float64) float64 { f, err := GetFloat64(values, key) if err != nil { return defaultValue @@ -186,7 +186,7 @@ func GetFloat64Default(values map[interface{}]interface{}, key interface{}, defa } // GetFloat64 returns a float64 for a given key in values. -func GetFloat64(values map[interface{}]interface{}, key interface{}) (float64, error) { +func GetFloat64[K comparable](values map[K]any, key K) (float64, error) { if v, ok := values[key]; !ok { return 0, ErrKeyDoesNotExist } else if j, ok := v.(json.Number); ok { @@ -198,7 +198,7 @@ func GetFloat64(values map[interface{}]interface{}, key interface{}) (float64, e } // GetStringDefault returns a string or the default value for a given key in values. -func GetStringDefault(values map[interface{}]interface{}, key interface{}, defaultValue string) string { +func GetStringDefault[K comparable](values map[K]any, key K, defaultValue string) string { if s, err := GetString(values, key); err == nil { return s } @@ -206,37 +206,39 @@ func GetStringDefault(values map[interface{}]interface{}, key interface{}, defau } // GetStringSliceDefault returns a string slice or the default value for a given key in values. -func GetStringSliceDefault(values map[interface{}]interface{}, key interface{}, defaultValue []string) []string { +func GetStringSliceDefault[K comparable](values map[K]any, key K, defaultValue []string) []string { if s, err := GetStringSlice(values, key); err == nil { return s } return defaultValue } -// KeyStringToInterface converts map[string]interface{} to map[interface{}]interface{} -func KeyStringToInterface(i map[string]interface{}) map[interface{}]interface{} { - o := make(map[interface{}]interface{}) +// KeyStringToInterface converts map[string]any to map[any]any +// Deprecated: with generics, this should not be necessary anymore. +func KeyStringToInterface(i map[string]any) map[any]any { + o := make(map[any]any) for k, v := range i { o[k] = v } return o } -// ToJSONMap converts all map[interface{}]interface{} occurrences (nested as well) to map[string]interface{}. -func ToJSONMap(i interface{}) interface{} { +// ToJSONMap converts all map[any]any occurrences (nested as well) to map[string]any. +// Deprecated: with generics, this should not be necessary anymore. +func ToJSONMap(i any) any { switch t := i.(type) { - case []interface{}: + case []any: for k, v := range t { t[k] = ToJSONMap(v) } return t - case map[string]interface{}: + case map[string]any: for k, v := range t { t[k] = ToJSONMap(v) } return t - case map[interface{}]interface{}: - res := make(map[string]interface{}) + case map[any]any: + res := make(map[string]any) for k, v := range t { res[fmt.Sprintf("%s", k)] = ToJSONMap(v) }