mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
improve empty checks for common interfaces implementer
This commit is contained in:
parent
a5a267567c
commit
b6ab1a992c
@ -9,12 +9,12 @@ package gmap_test
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_AnyAnyMap_Var(t *testing.T) {
|
||||
@ -218,6 +218,18 @@ func Test_AnyAnyMap_FilterEmpty(t *testing.T) {
|
||||
t.Assert(m.Get(1), nil)
|
||||
t.Assert(m.Get(2), 2)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewAnyAnyMap()
|
||||
m.Set(1, 0)
|
||||
m.Set("time1", time.Time{})
|
||||
m.Set("time2", time.Now())
|
||||
t.Assert(m.Get(1), 0)
|
||||
t.Assert(m.Get("time1"), time.Time{})
|
||||
m.FilterEmpty()
|
||||
t.Assert(m.Get(1), nil)
|
||||
t.Assert(m.Get("time1"), nil)
|
||||
t.AssertNE(m.Get("time2"), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AnyAnyMap_Json(t *testing.T) {
|
||||
|
@ -11,6 +11,21 @@ import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// apiString is used for type assert api for String().
|
||||
type apiString interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
// apiInterfaces is used for type assert api for Interfaces.
|
||||
type apiInterfaces interface {
|
||||
Interfaces() []interface{}
|
||||
}
|
||||
|
||||
// apiMapStrAny is the interface support for converting struct parameter to map.
|
||||
type apiMapStrAny interface {
|
||||
MapStrAny() map[string]interface{}
|
||||
}
|
||||
|
||||
// IsEmpty checks whether given <value> empty.
|
||||
// It returns true if <value> is in: 0, nil, false, "", len(slice/map/chan) == 0,
|
||||
// or else it returns false.
|
||||
@ -52,6 +67,16 @@ func IsEmpty(value interface{}) bool {
|
||||
case []rune:
|
||||
return len(value) == 0
|
||||
default:
|
||||
// Common interfaces checks.
|
||||
if f, ok := value.(apiString); ok {
|
||||
return f.String() == ""
|
||||
}
|
||||
if f, ok := value.(apiInterfaces); ok {
|
||||
return len(f.Interfaces()) == 0
|
||||
}
|
||||
if f, ok := value.(apiMapStrAny); ok {
|
||||
return len(f.MapStrAny()) == 0
|
||||
}
|
||||
// Finally using reflect.
|
||||
var rv reflect.Value
|
||||
if v, ok := value.(reflect.Value); ok {
|
||||
|
@ -19,16 +19,6 @@ import (
|
||||
"github.com/gogf/gf/encoding/gbinary"
|
||||
)
|
||||
|
||||
// Type assert api for String().
|
||||
type apiString interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
// Type assert api for Error().
|
||||
type apiError interface {
|
||||
Error() string
|
||||
}
|
||||
|
||||
var (
|
||||
// Empty strings.
|
||||
emptyStringMap = map[string]struct{}{
|
||||
|
53
util/gconv/gconv_interface.go
Normal file
53
util/gconv/gconv_interface.go
Normal file
@ -0,0 +1,53 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gconv
|
||||
|
||||
// apiString is used for type assert api for String().
|
||||
type apiString interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
// apiError is used for type assert api for Error().
|
||||
type apiError interface {
|
||||
Error() string
|
||||
}
|
||||
|
||||
// apiInterfaces is used for type assert api for Interfaces().
|
||||
type apiInterfaces interface {
|
||||
Interfaces() []interface{}
|
||||
}
|
||||
|
||||
// apiFloats is used for type assert api for Floats().
|
||||
type apiFloats interface {
|
||||
Floats() []float64
|
||||
}
|
||||
|
||||
// apiInts is used for type assert api for Ints().
|
||||
type apiInts interface {
|
||||
Ints() []int
|
||||
}
|
||||
|
||||
// apiStrings is used for type assert api for Strings().
|
||||
type apiStrings interface {
|
||||
Strings() []string
|
||||
}
|
||||
|
||||
// apiUints is used for type assert api for Uints().
|
||||
type apiUints interface {
|
||||
Uints() []uint
|
||||
}
|
||||
|
||||
// apiMapStrAny is the interface support for converting struct parameter to map.
|
||||
type apiMapStrAny interface {
|
||||
MapStrAny() map[string]interface{}
|
||||
}
|
||||
|
||||
// apiUnmarshalValue is the interface for custom defined types customizing value assignment.
|
||||
// Note that only pointer can implement interface apiUnmarshalValue.
|
||||
type apiUnmarshalValue interface {
|
||||
UnmarshalValue(interface{}) error
|
||||
}
|
@ -17,11 +17,6 @@ import (
|
||||
"github.com/gogf/gf/internal/utils"
|
||||
)
|
||||
|
||||
// apiMapStrAny is the interface support for converting struct parameter to map.
|
||||
type apiMapStrAny interface {
|
||||
MapStrAny() map[string]interface{}
|
||||
}
|
||||
|
||||
// Map converts any variable <value> to map[string]interface{}. If the parameter <value> is not a
|
||||
// map/struct/*struct type, then the conversion will fail and returns nil.
|
||||
//
|
||||
|
@ -11,11 +11,6 @@ import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// apiInterfaces is used for type assert api for Interfaces.
|
||||
type apiInterfaces interface {
|
||||
Interfaces() []interface{}
|
||||
}
|
||||
|
||||
// SliceAny is alias of Interfaces.
|
||||
func SliceAny(i interface{}) []interface{} {
|
||||
return Interfaces(i)
|
||||
|
@ -6,11 +6,6 @@
|
||||
|
||||
package gconv
|
||||
|
||||
// apiFloats is used for type assert api for Floats.
|
||||
type apiFloats interface {
|
||||
Floats() []float64
|
||||
}
|
||||
|
||||
// SliceFloat is alias of Floats.
|
||||
func SliceFloat(i interface{}) []float64 {
|
||||
return Floats(i)
|
||||
|
@ -6,11 +6,6 @@
|
||||
|
||||
package gconv
|
||||
|
||||
// apiInts is used for type assert api for Ints.
|
||||
type apiInts interface {
|
||||
Ints() []int
|
||||
}
|
||||
|
||||
// SliceInt is alias of Ints.
|
||||
func SliceInt(i interface{}) []int {
|
||||
return Ints(i)
|
||||
|
@ -6,11 +6,6 @@
|
||||
|
||||
package gconv
|
||||
|
||||
// apiStrings is used for type assert api for Strings.
|
||||
type apiStrings interface {
|
||||
Strings() []string
|
||||
}
|
||||
|
||||
// SliceStr is alias of Strings.
|
||||
func SliceStr(i interface{}) []string {
|
||||
return Strings(i)
|
||||
|
@ -6,11 +6,6 @@
|
||||
|
||||
package gconv
|
||||
|
||||
// apiUints is used for type assert api for Uints.
|
||||
type apiUints interface {
|
||||
Uints() []uint
|
||||
}
|
||||
|
||||
// SliceUint is alias of Uints.
|
||||
func SliceUint(i interface{}) []uint {
|
||||
return Uints(i)
|
||||
|
@ -19,12 +19,6 @@ import (
|
||||
"github.com/gogf/gf/internal/utils"
|
||||
)
|
||||
|
||||
// apiUnmarshalValue is the interface for custom defined types customizing value assignment.
|
||||
// Note that only pointer can implement interface apiUnmarshalValue.
|
||||
type apiUnmarshalValue interface {
|
||||
UnmarshalValue(interface{}) error
|
||||
}
|
||||
|
||||
var (
|
||||
// replaceCharReg is the regular expression object for replacing chars
|
||||
// in map keys and attribute names.
|
||||
|
Loading…
Reference in New Issue
Block a user