Skip to content

Commit

Permalink
💥 up: replace all interface{} to any, will not support go < 1.18
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 5, 2023
1 parent 5750165 commit 1e18ae6
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 53 deletions.
24 changes: 12 additions & 12 deletions converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,37 @@ var (
*************************************************************/

// Int convert string to int
func Int(in interface{}) (int, error) { return ToInt(in) }
func Int(in any) (int, error) { return ToInt(in) }

// MustInt convert string to int
func MustInt(in interface{}) int {
func MustInt(in any) int {
val, _ := ToInt(in)
return val
}

// ToInt convert string to int
func ToInt(in interface{}) (int, error) { return mathutil.ToInt(in) }
func ToInt(in any) (int, error) { return mathutil.ToInt(in) }

// Uint convert string to uint
func Uint(in interface{}) (uint64, error) { return ToUint(in) }
func Uint(in any) (uint64, error) { return ToUint(in) }

// MustUint convert string to uint
func MustUint(in interface{}) uint64 {
func MustUint(in any) uint64 {
val, _ := ToUint(in)
return val
}

// ToUint convert string to uint
func ToUint(in interface{}) (uint64, error) { return mathutil.ToUint(in) }
func ToUint(in any) (uint64, error) { return mathutil.ToUint(in) }

// Int64 convert value to int64
func Int64(in interface{}) (int64, error) { return ToInt64(in) }
func Int64(in any) (int64, error) { return ToInt64(in) }

// ToInt64 convert value to int64
func ToInt64(val interface{}) (int64, error) { return mathutil.ToInt64(val) }
func ToInt64(val any) (int64, error) { return mathutil.ToInt64(val) }

// MustInt64 convert value to int64
func MustInt64(in interface{}) int64 {
func MustInt64(in any) int64 {
i64, _ := ToInt64(in)
return i64
}
Expand Down Expand Up @@ -93,16 +93,16 @@ func MustBool(s string) bool {
}

// String convert val to string
func String(val interface{}) (string, error) { return ToString(val) }
func String(val any) (string, error) { return ToString(val) }

// MustString convert value to string
func MustString(in interface{}) string {
func MustString(in any) string {
val, _ := ToString(in)
return val
}

// ToString convert value to string
func ToString(val interface{}) (string, error) { return strutil.ToString(val) }
func ToString(val any) (string, error) { return strutil.ToString(val) }

/*************************************************************
* change string case
Expand Down
6 changes: 3 additions & 3 deletions converters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestValToInt(t *testing.T) {
is := assert.New(t)

tests := []interface{}{
tests := []any{
2,
int8(2), int16(2), int32(2), int64(2),
uint(2), uint8(2), uint16(2), uint32(2), uint64(2),
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestValToInt(t *testing.T) {
func TestValToStr(t *testing.T) {
is := assert.New(t)

tests := []interface{}{
tests := []any{
2,
int8(2), int16(2), int32(2), int64(2),
uint(2), uint8(2), uint16(2), uint32(2), uint64(2),
Expand All @@ -79,7 +79,7 @@ func TestValToStr(t *testing.T) {
is.Eq("2", MustString(in))
}

tests1 := []interface{}{
tests1 := []any{
float32(2.3), 2.3,
}
for _, in := range tests1 {
Expand Down
2 changes: 1 addition & 1 deletion filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func URLDecode(s string) string {
}

// Unique value in the given array, slice.
func Unique(val interface{}) interface{} {
func Unique(val any) any {
switch tv := val.(type) {
case []int:
mp := make(map[int]int)
Expand Down
46 changes: 23 additions & 23 deletions filtration.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ import (
type Filtration struct {
err error
// raw data
data map[string]interface{}
data map[string]any
// mark has apply filters
filtered bool
// filtered and clean data
cleanData map[string]interface{}
cleanData map[string]any
// filter rules
filterRules []*Rule
}

// New a Filtration
func New(data map[string]interface{}) *Filtration {
func New(data map[string]any) *Filtration {
return &Filtration{
data: data,
// init map
cleanData: make(map[string]interface{}),
cleanData: make(map[string]any),
}
}

// LoadData set raw data for filtering.
func (f *Filtration) LoadData(data map[string]interface{}) {
func (f *Filtration) LoadData(data map[string]any) {
f.data = data
}

Expand All @@ -43,10 +43,10 @@ func (f *Filtration) ResetData(resetRaw bool) {

// reset data.
if resetRaw {
f.data = make(map[string]interface{})
f.data = make(map[string]any)
}

f.cleanData = make(map[string]interface{})
f.cleanData = make(map[string]any)
}

// ResetRules reset rules and filtered data
Expand All @@ -58,12 +58,12 @@ func (f *Filtration) ResetRules() {
f.filterRules = f.filterRules[:0]

// clear cleanData
f.cleanData = make(map[string]interface{})
f.cleanData = make(map[string]any)
}

// Clear all data and rules
func (f *Filtration) Clear() {
f.data = make(map[string]interface{})
f.data = make(map[string]any)
f.ResetRules()
}

Expand All @@ -78,7 +78,7 @@ func (f *Filtration) Clear() {
// f.AddRule("name", "trim")
// f.AddRule("age", "int")
// f.AddRule("age", "trim|int")
func (f *Filtration) AddRule(field string, rule interface{}) *Rule {
func (f *Filtration) AddRule(field string, rule any) *Rule {
fields := strutil.Split(field, ",")
if len(fields) == 0 {
panic("filter: invalid fields parameters, cannot be empty")
Expand All @@ -95,7 +95,7 @@ func (f *Filtration) AddRule(field string, rule interface{}) *Rule {
}

r.AddFilters(rules...)
} else if fn, ok := rule.(func(interface{}) (interface{}, error)); ok {
} else if fn, ok := rule.(func(any) (any, error)); ok {
r.SetFilterFunc(fn)
} else {
panic("filter: 'rule' params cannot be empty and type allow: string, func")
Expand Down Expand Up @@ -158,23 +158,23 @@ func (f *Filtration) Err() error {
*************************************************************/

// Raw get raw value by key
func (f *Filtration) Raw(key string) (interface{}, bool) {
func (f *Filtration) Raw(key string) (any, bool) {
return maputil.GetByPath(key, f.data)
}

// Safe get filtered value by key
func (f *Filtration) Safe(key string) (interface{}, bool) {
func (f *Filtration) Safe(key string) (any, bool) {
return maputil.GetByPath(key, f.cleanData)
}

// SafeVal get filtered value by key
func (f *Filtration) SafeVal(key string) interface{} {
func (f *Filtration) SafeVal(key string) any {
val, _ := maputil.GetByPath(key, f.cleanData)
return val
}

// Get value by key
func (f *Filtration) Get(key string) (interface{}, bool) {
func (f *Filtration) Get(key string) (any, bool) {
val, ok := maputil.GetByPath(key, f.cleanData)
if !ok {
val, ok = maputil.GetByPath(key, f.data)
Expand All @@ -184,7 +184,7 @@ func (f *Filtration) Get(key string) (interface{}, bool) {
}

// MustGet value by key
func (f *Filtration) MustGet(key string) interface{} {
func (f *Filtration) MustGet(key string) any {
val, _ := f.Get(key)
return val
}
Expand Down Expand Up @@ -229,7 +229,7 @@ func (f *Filtration) String(key string) string {
}

// BindStruct bind the filtered data to struct.
func (f *Filtration) BindStruct(ptr interface{}) error {
func (f *Filtration) BindStruct(ptr any) error {
bts, err := json.Marshal(f.cleanData)
if err != nil {
return err
Expand All @@ -239,12 +239,12 @@ func (f *Filtration) BindStruct(ptr interface{}) error {
}

// RawData get raw data
func (f *Filtration) RawData() map[string]interface{} {
func (f *Filtration) RawData() map[string]any {
return f.data
}

// CleanData get filtered data
func (f *Filtration) CleanData() map[string]interface{} {
func (f *Filtration) CleanData() map[string]any {
return f.cleanData
}

Expand All @@ -261,9 +261,9 @@ type Rule struct {
// filter args. { index: "args" }
filterArgs map[int]string
// user custom filter func
filterFunc func(val interface{}) (interface{}, error)
filterFunc func(val any) (any, error)
// default value for the rule
defaultVal interface{}
defaultVal any
}

func newRule(fields []string) *Rule {
Expand All @@ -275,13 +275,13 @@ func newRule(fields []string) *Rule {
}

// SetDefaultVal set default value for the rule
func (r *Rule) SetDefaultVal(defaultVal interface{}) *Rule {
func (r *Rule) SetDefaultVal(defaultVal any) *Rule {
r.defaultVal = defaultVal
return r
}

// SetFilterFunc user custom filter func
func (r *Rule) SetFilterFunc(fn func(val interface{}) (interface{}, error)) *Rule {
func (r *Rule) SetFilterFunc(fn func(val any) (any, error)) *Rule {
r.filterFunc = fn
return r
}
Expand Down
20 changes: 10 additions & 10 deletions filtration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
func TestFiltration(t *testing.T) {
is := assert.New(t)

fl := New(map[string]interface{}{
fl := New(map[string]any{
"key0": " abc ",
"key1": "2",
"sub": map[string]string{"k0": "v0"},
"sub1": map[string]interface{}{"k0": "v0"},
"sub2": map[interface{}]interface{}{"k0": "v0"},
"sub1": map[string]any{"k0": "v0"},
"sub2": map[any]any{"k0": "v0"},
})

is.Eq("strToTime", Name("str2time"))
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestFiltration(t *testing.T) {
is.False(ok)
is.Eq(nil, val)

f := New(map[string]interface{}{
f := New(map[string]any{
"key0": "34",
"name": " inhere ",
"email": " my@email.com ",
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestFiltration_AddRule(t *testing.T) {
is := assert.New(t)

f := New(nil)
f.LoadData(map[string]interface{}{
f.LoadData(map[string]any{
"name": " INHERE ",
"age": "50 ",
})
Expand All @@ -112,7 +112,7 @@ func TestFiltration_AddRule(t *testing.T) {
f.AddRule("name", []int{1})
})

f.AddRule("name", func(v interface{}) (interface{}, error) {
f.AddRule("name", func(v any) (any, error) {
return strings.TrimSpace(v.(string)), nil
})

Expand All @@ -136,7 +136,7 @@ func TestFiltration_AddRule(t *testing.T) {
f.ResetData(true)
is.Empty(f.RawData())
is.Empty(f.CleanData())
f.LoadData(map[string]interface{}{
f.LoadData(map[string]any{
"name": " Inhere0 ",
})
is.NoErr(f.Filtering())
Expand All @@ -149,7 +149,7 @@ func TestFiltration_AddRule(t *testing.T) {
is.Eq("def val", f.String("not-exist"))

// trimStrings error
f = New(map[string]interface{}{
f = New(map[string]any{
"ints": []int{1, 2, 3},
})
f.AddRule("ints", "trimStrings")
Expand All @@ -166,7 +166,7 @@ func TestFiltration_AddRule(t *testing.T) {
func TestFiltration_Filtering(t *testing.T) {
is := assert.New(t)

data := map[string]interface{}{
data := map[string]any{
"name": "inhere",
"age": "50",
"money": "50.34",
Expand Down Expand Up @@ -253,7 +253,7 @@ func TestFiltration_Filtering(t *testing.T) {
is.Eq("a.com?p%3D1", f.String("url"))

// bind
f = New(map[string]interface{}{
f = New(map[string]any{
"name": " inhere ",
"age": " 89 ",
})
Expand Down
7 changes: 3 additions & 4 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// Apply a filter by name. for filter value.
func Apply(name string, val interface{}, args []string) (interface{}, error) {
func Apply(name string, val any, args []string) (any, error) {
var err error
realName := Name(name)

Expand Down Expand Up @@ -97,8 +97,8 @@ func Apply(name string, val interface{}, args []string) (interface{}, error) {
return val, err
}

// GetByPath get value from a map[string]interface{}. eg "top" "top.sub"
func GetByPath(key string, mp map[string]interface{}) (interface{}, bool) {
// GetByPath get value from a map[string]any. eg "top" "top.sub"
func GetByPath(key string, mp map[string]any) (any, bool) {
return maputil.GetByPath(key, mp)
}

Expand All @@ -110,6 +110,5 @@ func parseArgString(argStr string) (ss []string) {
if len(argStr) == 1 { // one char
return []string{argStr}
}

return strutil.Split(argStr, ",")
}

0 comments on commit 1e18ae6

Please sign in to comment.