add function gerror.HasStack; rename all api* interfaces to i*

This commit is contained in:
jianchenma
2021-09-17 19:26:56 +08:00
parent 5383672d78
commit 01a3dd1eb0
34 changed files with 193 additions and 192 deletions

View File

@ -8,8 +8,8 @@ package garray
import "strings"
// apiInterfaces is used for type assert api for Interfaces.
type apiInterfaces interface {
// iInterfaces is used for type assert api for Interfaces.
type iInterfaces interface {
Interfaces() []interface{}
}

View File

@ -119,7 +119,7 @@ func (c *Core) Transaction(ctx context.Context, f func(ctx context.Context, tx *
defer func() {
if err == nil {
if exception := recover(); exception != nil {
if v, ok := exception.(error); ok {
if v, ok := exception.(error); ok && gerror.HasStack(v) {
err = v
} else {
err = gerror.NewCodef(gcode.CodeInternalError, "%+v", exception)
@ -313,7 +313,7 @@ func (tx *TX) Transaction(ctx context.Context, f func(ctx context.Context, tx *T
defer func() {
if err == nil {
if exception := recover(); exception != nil {
if v, ok := exception.(error); ok {
if v, ok := exception.(error); ok && gerror.HasStack(v) {
err = v
} else {
err = gerror.NewCodef(gcode.CodeInternalError, "%+v", exception)

View File

@ -31,28 +31,28 @@ import (
"github.com/gogf/gf/util/gconv"
)
// apiString is the type assert api for String.
type apiString interface {
// iString is the type assert api for String.
type iString interface {
String() string
}
// apiIterator is the type assert api for Iterator.
type apiIterator interface {
// iIterator is the type assert api for Iterator.
type iIterator interface {
Iterator(f func(key, value interface{}) bool)
}
// apiInterfaces is the type assert api for Interfaces.
type apiInterfaces interface {
// iInterfaces is the type assert api for Interfaces.
type iInterfaces interface {
Interfaces() []interface{}
}
// apiMapStrAny is the interface support for converting struct parameter to map.
type apiMapStrAny interface {
// iMapStrAny is the interface support for converting struct parameter to map.
type iMapStrAny interface {
MapStrAny() map[string]interface{}
}
// apiTableName is the interface for retrieving table name fro struct.
type apiTableName interface {
// iTableName is the interface for retrieving table name fro struct.
type iTableName interface {
TableName() string
}
@ -104,7 +104,7 @@ func (m *Model) guessPrimaryTableName(tableStr string) string {
func getTableNameFromOrmTag(object interface{}) string {
var tableName string
// Use the interface value.
if r, ok := object.(apiTableName); ok {
if r, ok := object.(iTableName); ok {
tableName = r.TableName()
}
// User meta data tag "orm".
@ -222,7 +222,7 @@ func ConvertDataForTableRecord(value interface{}) map[string]interface{} {
default:
// Use string conversion in default.
if s, ok := v.(apiString); ok {
if s, ok := v.(iString); ok {
data[k] = s.String()
} else {
// Convert the value to JSON.
@ -246,7 +246,7 @@ func DataToMapDeep(value interface{}) map[string]interface{} {
default:
// Use string conversion in default.
if s, ok := v.(apiString); ok {
if s, ok := v.(iString); ok {
m[k] = s.String()
} else {
m[k] = v
@ -457,11 +457,11 @@ func formatWhere(db DB, in formatWhereInput) (newWhere string, newArgs []interfa
}
case reflect.Struct:
// If `where` struct implements apiIterator interface,
// If `where` struct implements iIterator interface,
// it then uses its Iterate function to iterate its key-value pairs.
// For example, ListMap and TreeMap are ordered map,
// which implement apiIterator interface and are index-friendly for where conditions.
if iterator, ok := in.Where.(apiIterator); ok {
// which implement iIterator interface and are index-friendly for where conditions.
if iterator, ok := in.Where.(iIterator); ok {
iterator.Iterator(func(key, value interface{}) bool {
ketStr := gconv.String(key)
if gregex.IsMatchString(regularFieldNameRegPattern, ketStr) {
@ -764,7 +764,7 @@ func handleArguments(sql string, args []interface{}) (newSql string, newArgs []i
default:
// It converts the struct to string in default
// if it has implemented the String interface.
if v, ok := arg.(apiString); ok {
if v, ok := arg.(iString); ok {
newArgs = append(newArgs, v.String())
continue
}

View File

@ -88,7 +88,7 @@ func (m *Model) Data(data ...interface{}) *Model {
model.data = ConvertDataForTableRecord(data[0])
case reflect.Struct:
if v, ok := data[0].(apiInterfaces); ok {
if v, ok := data[0].(iInterfaces); ok {
var (
array = v.Interfaces()
list = make(List, len(array))
@ -265,7 +265,7 @@ func (m *Model) doInsertWithOption(insertOption int) (result sql.Result, err err
list = List{ConvertDataForTableRecord(value)}
case reflect.Struct:
if v, ok := value.(apiInterfaces); ok {
if v, ok := value.(iInterfaces); ok {
var (
array = v.Interfaces()
)

View File

@ -38,8 +38,8 @@ type Options struct {
StrNumber bool // StrNumber causes the Decoder to unmarshal a number into an interface{} as a string instead of as a float64.
}
// apiInterface is used for type assert api for Interface().
type apiInterface interface {
// iInterface is used for type assert api for Interface().
type iInterface interface {
Interface() interface{}
}
@ -50,7 +50,7 @@ type apiInterface interface {
func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
if value != nil {
if utils.IsStruct(value) {
if v, ok := value.(apiInterface); ok {
if v, ok := value.(iInterface); ok {
value = v.Interface()
}
}

View File

@ -15,32 +15,32 @@ import (
"github.com/gogf/gf/errors/gcode"
)
// apiCode is the interface for Code feature.
type apiCode interface {
// iCode is the interface for Code feature.
type iCode interface {
Error() string
Code() gcode.Code
}
// apiStack is the interface for Stack feature.
type apiStack interface {
// iStack is the interface for Stack feature.
type iStack interface {
Error() string
Stack() string
}
// apiCause is the interface for Cause feature.
type apiCause interface {
// iCause is the interface for Cause feature.
type iCause interface {
Error() string
Cause() error
}
// apiCurrent is the interface for Current feature.
type apiCurrent interface {
// iCurrent is the interface for Current feature.
type iCurrent interface {
Error() string
Current() error
}
// apiNext is the interface for Next feature.
type apiNext interface {
// iNext is the interface for Next feature.
type iNext interface {
Error() string
Next() error
}
@ -258,7 +258,7 @@ func WrapCodeSkipf(code gcode.Code, skip int, err error, format string, args ...
// It returns CodeNil if it has no error code or it does not implements interface Code.
func Code(err error) gcode.Code {
if err != nil {
if e, ok := err.(apiCode); ok {
if e, ok := err.(iCode); ok {
return e.Code()
}
}
@ -268,7 +268,7 @@ func Code(err error) gcode.Code {
// Cause returns the root cause error of `err`.
func Cause(err error) error {
if err != nil {
if e, ok := err.(apiCause); ok {
if e, ok := err.(iCause); ok {
return e.Cause()
}
}
@ -281,7 +281,7 @@ func Stack(err error) string {
if err == nil {
return ""
}
if e, ok := err.(apiStack); ok {
if e, ok := err.(iStack); ok {
return e.Stack()
}
return err.Error()
@ -293,7 +293,7 @@ func Current(err error) error {
if err == nil {
return nil
}
if e, ok := err.(apiCurrent); ok {
if e, ok := err.(iCurrent); ok {
return e.Current()
}
return err
@ -305,8 +305,14 @@ func Next(err error) error {
if err == nil {
return nil
}
if e, ok := err.(apiNext); ok {
if e, ok := err.(iNext); ok {
return e.Next()
}
return nil
}
// HasStack checks and returns whether `err` implemented interface `iStack`.
func HasStack(err error) bool {
_, ok := err.(iStack)
return ok
}

View File

@ -80,7 +80,7 @@ func (err *Error) Cause() error {
if e, ok := loop.error.(*Error); ok {
// Internal Error struct.
loop = e
} else if e, ok := loop.error.(apiCause); ok {
} else if e, ok := loop.error.(iCause); ok {
// Other Error that implements ApiCause interface.
return e.Cause()
} else {

View File

@ -319,3 +319,12 @@ func Test_Json(t *testing.T) {
t.Assert(string(b), `"2: 1"`)
})
}
func Test_HasStack(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err1 := errors.New("1")
err2 := gerror.New("1")
t.Assert(gerror.HasStack(err1), false)
t.Assert(gerror.HasStack(err2), true)
})
}

View File

@ -12,22 +12,22 @@ import (
"time"
)
// apiString is used for type assert api for String().
type apiString interface {
// iString is used for type assert api for String().
type iString interface {
String() string
}
// apiInterfaces is used for type assert api for Interfaces.
type apiInterfaces interface {
// iInterfaces is used for type assert api for Interfaces.
type iInterfaces interface {
Interfaces() []interface{}
}
// apiMapStrAny is the interface support for converting struct parameter to map.
type apiMapStrAny interface {
// iMapStrAny is the interface support for converting struct parameter to map.
type iMapStrAny interface {
MapStrAny() map[string]interface{}
}
type apiTime interface {
type iTime interface {
Date() (year int, month time.Month, day int)
IsZero() bool
}
@ -88,25 +88,25 @@ func IsEmpty(value interface{}) bool {
// =========================
// Common interfaces checks.
// =========================
if f, ok := value.(apiTime); ok {
if f, ok := value.(iTime); ok {
if f == nil {
return true
}
return f.IsZero()
}
if f, ok := value.(apiString); ok {
if f, ok := value.(iString); ok {
if f == nil {
return true
}
return f.String() == ""
}
if f, ok := value.(apiInterfaces); ok {
if f, ok := value.(iInterfaces); ok {
if f == nil {
return true
}
return len(f.Interfaces()) == 0
}
if f, ok := value.(apiMapStrAny); ok {
if f, ok := value.(iMapStrAny); ok {
if f == nil {
return true
}
@ -210,25 +210,25 @@ func IsEmpty(value interface{}) bool {
// // =========================
// // Common interfaces checks.
// // =========================
// if f, ok := value.(apiTime); ok {
// if f, ok := value.(iTime); ok {
// if f == nil {
// return true
// }
// return f.IsZero()
// }
// if f, ok := value.(apiString); ok {
// if f, ok := value.(iString); ok {
// if f == nil {
// return true
// }
// return f.String() == ""
// }
// if f, ok := value.(apiInterfaces); ok {
// if f, ok := value.(iInterfaces); ok {
// if f == nil {
// return true
// }
// return len(f.Interfaces()) == 0
// }
// if f, ok := value.(apiMapStrAny); ok {
// if f, ok := value.(iMapStrAny); ok {
// if f == nil {
// return true
// }

View File

@ -95,12 +95,6 @@ type (
Handler *handlerItem // Handler object.
}
// errorStack is the interface for Stack feature.
errorStack interface {
Error() string
Stack() string
}
// Listening file descriptor mapping.
// The key is either "http" or "https" and the value is its FD.
listenerFdMap = map[string]string

View File

@ -29,9 +29,9 @@ func niceCallFunc(f func()) {
return
default:
if _, ok := exception.(errorStack); ok {
if v, ok := exception.(error); ok && gerror.HasStack(v) {
// It's already an error that has stack info.
panic(exception)
panic(v)
} else {
// Create a new error with stack info.
// Note that there's a skip pointing the start stacktrace

View File

@ -99,9 +99,9 @@ func (m *middleware) Next() {
loop = false
}
}, func(exception error) {
if e, ok := exception.(errorStack); ok {
if v, ok := exception.(error); ok && gerror.HasStack(v) {
// It's already an error that has stack info.
m.request.error = e
m.request.error = v
} else {
// Create a new error with stack info.
// Note that there's a skip pointing the start stacktrace

View File

@ -177,13 +177,13 @@ func (c *Config) SetPath(path string) error {
// It is off in default.
//
// Note that, turning on this feature is quite expensive, and it is not recommended
// to allow separators in the key names. It is best to avoid this on the application side.
// allowing separators in the key names. It is best to avoid this on the application side.
func (c *Config) SetViolenceCheck(check bool) {
c.violenceCheck = check
c.Clear()
}
// AddPath adds a absolute or relative path to the search paths.
// AddPath adds an absolute or relative path to the search paths.
func (c *Config) AddPath(path string) error {
var (
isDir = false

View File

@ -99,7 +99,7 @@ func (p *Pool) AddWithRecover(userFunc func(), recoverFunc ...func(err error)) e
defer func() {
if exception := recover(); exception != nil {
if len(recoverFunc) > 0 && recoverFunc[0] != nil {
if v, ok := exception.(error); ok {
if v, ok := exception.(error); ok && gerror.HasStack(v) {
recoverFunc[0](v)
} else {
recoverFunc[0](gerror.NewCodef(gcode.CodeInternalError, `%+v`, exception))

View File

@ -19,8 +19,8 @@ type Time struct {
wrapper
}
// apiUnixNano is an interface definition commonly for custom time.Time wrapper.
type apiUnixNano interface {
// iUnixNano is an interface definition commonly for custom time.Time wrapper.
type iUnixNano interface {
UnixNano() int64
}
@ -69,7 +69,7 @@ func New(param ...interface{}) *Time {
return NewFromTimeStamp(r)
default:
if v, ok := r.(apiUnixNano); ok {
if v, ok := r.(iUnixNano); ok {
return NewFromTimeStamp(v.UnixNano())
}
}

View File

@ -22,14 +22,6 @@ import (
"github.com/gogf/gf/encoding/gbinary"
)
type (
// errorStack is the interface for Stack feature.
errorStack interface {
Error() string
Stack() string
}
)
var (
// Empty strings.
emptyStringMap = map[string]struct{}{
@ -316,7 +308,7 @@ func Bytes(any interface{}) []byte {
case []byte:
return value
default:
if f, ok := value.(apiBytes); ok {
if f, ok := value.(iBytes); ok {
return f.Bytes()
}
var (
@ -427,12 +419,12 @@ func String(any interface{}) string {
if value == nil {
return ""
}
if f, ok := value.(apiString); ok {
if f, ok := value.(iString); ok {
// If the variable implements the String() interface,
// then use that interface to perform the conversion
return f.String()
}
if f, ok := value.(apiError); ok {
if f, ok := value.(iError); ok {
// If the variable implements the Error() interface,
// then use that interface to perform the conversion
return f.Error()
@ -488,7 +480,7 @@ func Bool(any interface{}) bool {
}
return true
default:
if f, ok := value.(apiBool); ok {
if f, ok := value.(iBool); ok {
return f.Bool()
}
rv := reflect.ValueOf(any)
@ -595,7 +587,7 @@ func Int64(any interface{}) int64 {
case []byte:
return gbinary.DecodeToInt64(value)
default:
if f, ok := value.(apiInt64); ok {
if f, ok := value.(iInt64); ok {
return f.Int64()
}
s := String(value)
@ -720,7 +712,7 @@ func Uint64(any interface{}) uint64 {
case []byte:
return gbinary.DecodeToUint64(value)
default:
if f, ok := value.(apiUint64); ok {
if f, ok := value.(iUint64); ok {
return f.Uint64()
}
s := String(value)
@ -758,7 +750,7 @@ func Float32(any interface{}) float32 {
case []byte:
return gbinary.DecodeToFloat32(value)
default:
if f, ok := value.(apiFloat32); ok {
if f, ok := value.(iFloat32); ok {
return f.Float32()
}
v, _ := strconv.ParseFloat(String(any), 64)
@ -779,7 +771,7 @@ func Float64(any interface{}) float64 {
case []byte:
return gbinary.DecodeToFloat64(value)
default:
if f, ok := value.(apiFloat64); ok {
if f, ok := value.(iFloat64); ok {
return f.Float64()
}
v, _ := strconv.ParseFloat(String(any), 64)

View File

@ -8,105 +8,105 @@ package gconv
import "github.com/gogf/gf/os/gtime"
// apiString is used for type assert api for String().
type apiString interface {
// iString is used for type assert api for String().
type iString interface {
String() string
}
// apiBool is used for type assert api for Bool().
type apiBool interface {
// iBool is used for type assert api for Bool().
type iBool interface {
Bool() bool
}
// apiInt64 is used for type assert api for Int64().
type apiInt64 interface {
// iInt64 is used for type assert api for Int64().
type iInt64 interface {
Int64() int64
}
// apiUint64 is used for type assert api for Uint64().
type apiUint64 interface {
// iUint64 is used for type assert api for Uint64().
type iUint64 interface {
Uint64() uint64
}
// apiFloat32 is used for type assert api for Float32().
type apiFloat32 interface {
// iFloat32 is used for type assert api for Float32().
type iFloat32 interface {
Float32() float32
}
// apiFloat64 is used for type assert api for Float64().
type apiFloat64 interface {
// iFloat64 is used for type assert api for Float64().
type iFloat64 interface {
Float64() float64
}
// apiError is used for type assert api for Error().
type apiError interface {
// iError is used for type assert api for Error().
type iError interface {
Error() string
}
// apiBytes is used for type assert api for Bytes().
type apiBytes interface {
// iBytes is used for type assert api for Bytes().
type iBytes interface {
Bytes() []byte
}
// apiInterface is used for type assert api for Interface().
type apiInterface interface {
// iInterface is used for type assert api for Interface().
type iInterface interface {
Interface() interface{}
}
// apiInterfaces is used for type assert api for Interfaces().
type apiInterfaces interface {
// iInterfaces is used for type assert api for Interfaces().
type iInterfaces interface {
Interfaces() []interface{}
}
// apiFloats is used for type assert api for Floats().
type apiFloats interface {
// iFloats is used for type assert api for Floats().
type iFloats interface {
Floats() []float64
}
// apiInts is used for type assert api for Ints().
type apiInts interface {
// iInts is used for type assert api for Ints().
type iInts interface {
Ints() []int
}
// apiStrings is used for type assert api for Strings().
type apiStrings interface {
// iStrings is used for type assert api for Strings().
type iStrings interface {
Strings() []string
}
// apiUints is used for type assert api for Uints().
type apiUints interface {
// iUints is used for type assert api for Uints().
type iUints interface {
Uints() []uint
}
// apiMapStrAny is the interface support for converting struct parameter to map.
type apiMapStrAny interface {
// iMapStrAny is the interface support for converting struct parameter to map.
type iMapStrAny 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 {
// iUnmarshalValue is the interface for custom defined types customizing value assignment.
// Note that only pointer can implement interface iUnmarshalValue.
type iUnmarshalValue interface {
UnmarshalValue(interface{}) error
}
// apiUnmarshalText is the interface for custom defined types customizing value assignment.
// Note that only pointer can implement interface apiUnmarshalText.
type apiUnmarshalText interface {
// iUnmarshalText is the interface for custom defined types customizing value assignment.
// Note that only pointer can implement interface iUnmarshalText.
type iUnmarshalText interface {
UnmarshalText(text []byte) error
}
// apiUnmarshalText is the interface for custom defined types customizing value assignment.
// Note that only pointer can implement interface apiUnmarshalJSON.
type apiUnmarshalJSON interface {
// iUnmarshalText is the interface for custom defined types customizing value assignment.
// Note that only pointer can implement interface iUnmarshalJSON.
type iUnmarshalJSON interface {
UnmarshalJSON(b []byte) error
}
// apiSet is the interface for custom value assignment.
type apiSet interface {
// iSet is the interface for custom value assignment.
type iSet interface {
Set(value interface{}) (old interface{})
}
// apiGTime is the interface for gtime.Time converting.
type apiGTime interface {
// iGTime is the interface for gtime.Time converting.
type iGTime interface {
GTime(format ...string) *gtime.Time
}

View File

@ -216,7 +216,7 @@ func doMapConvertForMapOrStructValue(isRoot bool, value interface{}, recursive b
return dataMap
case reflect.Struct:
// Map converting interface check.
if v, ok := value.(apiMapStrAny); ok {
if v, ok := value.(iMapStrAny); ok {
m := v.MapStrAny()
if recursive {
for k, v := range m {

View File

@ -96,8 +96,8 @@ func doMapToMap(params interface{}, pointer interface{}, mapping ...map[string]s
defer func() {
// Catch the panic, especially the reflect operation panics.
if exception := recover(); exception != nil {
if e, ok := exception.(errorStack); ok {
err = e
if v, ok := exception.(error); ok && gerror.HasStack(v) {
err = v
} else {
err = gerror.NewCodeSkipf(gcode.CodeInternalError, 1, "%+v", exception)
}

View File

@ -114,10 +114,10 @@ func doMapToMaps(params interface{}, pointer interface{}, mapping ...map[string]
return gerror.NewCode(gcode.CodeInvalidParameter, "pointer element should be type of map/*map")
}
defer func() {
// Catch the panic, especially the reflect operation panics.
// Catch the panic, especially the reflection operation panics.
if exception := recover(); exception != nil {
if e, ok := exception.(errorStack); ok {
err = e
if v, ok := exception.(error); ok && gerror.HasStack(v) {
err = v
} else {
err = gerror.NewCodeSkipf(gcode.CodeInternalError, 1, "%+v", exception)
}

View File

@ -22,7 +22,7 @@ func Interfaces(any interface{}) []interface{} {
}
if r, ok := any.([]interface{}); ok {
return r
} else if r, ok := any.(apiInterfaces); ok {
} else if r, ok := any.(iInterfaces); ok {
return r.Interfaces()
} else {
var array []interface{}

View File

@ -112,10 +112,10 @@ func Float32s(any interface{}) []float32 {
array[k] = Float32(v)
}
default:
if v, ok := any.(apiFloats); ok {
if v, ok := any.(iFloats); ok {
return Float32s(v.Floats())
}
if v, ok := any.(apiInterfaces); ok {
if v, ok := any.(iInterfaces); ok {
return Float32s(v.Interfaces())
}
// JSON format string value converting.
@ -240,10 +240,10 @@ func Float64s(any interface{}) []float64 {
array[k] = Float64(v)
}
default:
if v, ok := any.(apiFloats); ok {
if v, ok := any.(iFloats); ok {
return v.Floats()
}
if v, ok := any.(apiInterfaces); ok {
if v, ok := any.(iInterfaces); ok {
return Floats(v.Interfaces())
}
// JSON format string value converting.

View File

@ -114,10 +114,10 @@ func Ints(any interface{}) []int {
array[k] = Int(v)
}
default:
if v, ok := any.(apiInts); ok {
if v, ok := any.(iInts); ok {
return v.Ints()
}
if v, ok := any.(apiInterfaces); ok {
if v, ok := any.(iInterfaces); ok {
return Ints(v.Interfaces())
}
// JSON format string value converting.
@ -247,10 +247,10 @@ func Int32s(any interface{}) []int32 {
array[k] = Int32(v)
}
default:
if v, ok := any.(apiInts); ok {
if v, ok := any.(iInts); ok {
return Int32s(v.Ints())
}
if v, ok := any.(apiInterfaces); ok {
if v, ok := any.(iInterfaces); ok {
return Int32s(v.Interfaces())
}
// JSON format string value converting.
@ -380,10 +380,10 @@ func Int64s(any interface{}) []int64 {
array[k] = Int64(v)
}
default:
if v, ok := any.(apiInts); ok {
if v, ok := any.(iInts); ok {
return Int64s(v.Ints())
}
if v, ok := any.(apiInterfaces); ok {
if v, ok := any.(iInterfaces); ok {
return Int64s(v.Interfaces())
}
// JSON format string value converting.

View File

@ -100,10 +100,10 @@ func Strings(any interface{}) []string {
array[k] = String(v)
}
default:
if v, ok := any.(apiStrings); ok {
if v, ok := any.(iStrings); ok {
return v.Strings()
}
if v, ok := any.(apiInterfaces); ok {
if v, ok := any.(iInterfaces); ok {
return Strings(v.Interfaces())
}
// JSON format string value converting.

View File

@ -113,10 +113,10 @@ func Uints(any interface{}) []uint {
array[k] = Uint(v)
}
default:
if v, ok := any.(apiUints); ok {
if v, ok := any.(iUints); ok {
return v.Uints()
}
if v, ok := any.(apiInterfaces); ok {
if v, ok := any.(iInterfaces); ok {
return Uints(v.Interfaces())
}
// JSON format string value converting.
@ -246,10 +246,10 @@ func Uint32s(any interface{}) []uint32 {
array[k] = Uint32(v)
}
default:
if v, ok := any.(apiUints); ok {
if v, ok := any.(iUints); ok {
return Uint32s(v.Uints())
}
if v, ok := any.(apiInterfaces); ok {
if v, ok := any.(iInterfaces); ok {
return Uint32s(v.Interfaces())
}
// JSON format string value converting.
@ -379,10 +379,10 @@ func Uint64s(any interface{}) []uint64 {
array[k] = Uint64(v)
}
default:
if v, ok := any.(apiUints); ok {
if v, ok := any.(iUints); ok {
return Uint64s(v.Uints())
}
if v, ok := any.(apiInterfaces); ok {
if v, ok := any.(iInterfaces); ok {
return Uint64s(v.Interfaces())
}
// JSON format string value converting.

View File

@ -87,7 +87,7 @@ func doStructWithJsonCheck(params interface{}, pointer interface{}) (err error,
}
default:
// The `params` might be struct that implements interface function Interface, eg: gvar.Var.
if v, ok := params.(apiInterface); ok {
if v, ok := params.(iInterface); ok {
return doStructWithJsonCheck(v.Interface(), pointer)
}
}
@ -107,8 +107,8 @@ func doStruct(params interface{}, pointer interface{}, mapping map[string]string
defer func() {
// Catch the panic, especially the reflection operation panics.
if exception := recover(); exception != nil {
if e, ok := exception.(errorStack); ok {
err = e
if v, ok := exception.(error); ok && gerror.HasStack(v) {
err = v
} else {
err = gerror.NewCodeSkipf(gcode.CodeInternalError, 1, "%+v", exception)
}
@ -172,7 +172,7 @@ func doStruct(params interface{}, pointer interface{}, mapping map[string]string
e := reflect.New(pointerElemReflectValue.Type().Elem()).Elem()
pointerElemReflectValue.Set(e.Addr())
}
//if v, ok := pointerElemReflectValue.Interface().(apiUnmarshalValue); ok {
//if v, ok := pointerElemReflectValue.Interface().(iUnmarshalValue); ok {
// return v.UnmarshalValue(params)
//}
// Note that it's `pointerElemReflectValue` here not `pointerReflectValue`.
@ -364,11 +364,11 @@ func bindVarToReflectValueWithInterfaceCheck(reflectValue reflect.Value, value i
pointer = reflectValue.Interface()
}
// UnmarshalValue.
if v, ok := pointer.(apiUnmarshalValue); ok {
if v, ok := pointer.(iUnmarshalValue); ok {
return v.UnmarshalValue(value), ok
}
// UnmarshalText.
if v, ok := pointer.(apiUnmarshalText); ok {
if v, ok := pointer.(iUnmarshalText); ok {
var valueBytes []byte
if b, ok := value.([]byte); ok {
valueBytes = b
@ -380,7 +380,7 @@ func bindVarToReflectValueWithInterfaceCheck(reflectValue reflect.Value, value i
}
}
// UnmarshalJSON.
if v, ok := pointer.(apiUnmarshalJSON); ok {
if v, ok := pointer.(iUnmarshalJSON); ok {
var valueBytes []byte
if b, ok := value.([]byte); ok {
valueBytes = b
@ -400,7 +400,7 @@ func bindVarToReflectValueWithInterfaceCheck(reflectValue reflect.Value, value i
return v.UnmarshalJSON(valueBytes), ok
}
}
if v, ok := pointer.(apiSet); ok {
if v, ok := pointer.(iSet); ok {
v.Set(value)
return nil, ok
}
@ -428,7 +428,7 @@ func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}, ma
switch kind {
case reflect.Slice, reflect.Array, reflect.Ptr, reflect.Interface:
if !structFieldValue.IsNil() {
if v, ok := structFieldValue.Interface().(apiSet); ok {
if v, ok := structFieldValue.Interface().(iSet); ok {
v.Set(value)
return nil
}

View File

@ -59,8 +59,8 @@ func doStructs(params interface{}, pointer interface{}, mapping map[string]strin
defer func() {
// Catch the panic, especially the reflect operation panics.
if exception := recover(); exception != nil {
if e, ok := exception.(errorStack); ok {
err = e
if v, ok := exception.(error); ok && gerror.HasStack(v) {
err = v
} else {
err = gerror.NewCodeSkipf(gcode.CodeInternalError, 1, "%+v", exception)
}

View File

@ -51,7 +51,7 @@ func GTime(any interface{}, format ...string) *gtime.Time {
if any == nil {
return nil
}
if v, ok := any.(apiGTime); ok {
if v, ok := any.(iGTime); ok {
return v.GTime(format...)
}
// It's already this type.

View File

@ -16,7 +16,7 @@ import (
"github.com/gogf/gf/util/gconv"
)
type apiString interface {
type iString interface {
String() string
}
type S struct {
@ -26,7 +26,7 @@ func (s S) String() string {
return "22222"
}
type apiError interface {
type iError interface {
Error() string
}
type S1 struct {
@ -570,12 +570,12 @@ func Test_String_All(t *testing.T) {
t.AssertEQ(gconv.String(boolStruct{}), "{}")
t.AssertEQ(gconv.String(&boolStruct{}), "{}")
var info apiString
var info iString
info = new(S)
t.AssertEQ(gconv.String(info), "22222")
var errinfo apiError
errinfo = new(S1)
t.AssertEQ(gconv.String(errinfo), "22222")
var errInfo iError
errInfo = new(S1)
t.AssertEQ(gconv.String(errInfo), "22222")
})
}

View File

@ -26,7 +26,7 @@ func Throw(exception interface{}) {
func Try(try func()) (err error) {
defer func() {
if exception := recover(); exception != nil {
if v, ok := exception.(error); ok {
if v, ok := exception.(error); ok && gerror.HasStack(v) {
err = v
} else {
err = gerror.NewCodef(gcode.CodeInternalError, `%+v`, exception)
@ -42,7 +42,7 @@ func Try(try func()) (err error) {
func TryCatch(try func(), catch ...func(exception error)) {
defer func() {
if exception := recover(); exception != nil && len(catch) > 0 {
if v, ok := exception.(error); ok {
if v, ok := exception.(error); ok && gerror.HasStack(v) {
catch[0](v)
} else {
catch[0](fmt.Errorf(`%+v`, exception))

View File

@ -15,18 +15,18 @@ import (
"reflect"
)
// apiVal is used for type assert api for Val().
type apiVal interface {
// iVal is used for type assert api for Val().
type iVal interface {
Val() interface{}
}
// apiString is used for type assert api for String().
type apiString interface {
// iString is used for type assert api for String().
type iString interface {
String() string
}
// apiMapStrAny is the interface support for converting struct parameter to map.
type apiMapStrAny interface {
// iMapStrAny is the interface support for converting struct parameter to map.
type iMapStrAny interface {
MapStrAny() map[string]interface{}
}
@ -63,14 +63,14 @@ func Export(i ...interface{}) string {
value = gconv.Map(value)
case reflect.Struct:
converted := false
if r, ok := value.(apiVal); ok {
if r, ok := value.(iVal); ok {
if result := r.Val(); result != nil {
value = result
converted = true
}
}
if !converted {
if r, ok := value.(apiMapStrAny); ok {
if r, ok := value.(iMapStrAny); ok {
if result := r.MapStrAny(); result != nil {
value = result
converted = true
@ -78,7 +78,7 @@ func Export(i ...interface{}) string {
}
}
if !converted {
if r, ok := value.(apiString); ok {
if r, ok := value.(iString); ok {
value = r.String()
}
}

View File

@ -74,8 +74,8 @@ type fieldRule struct {
Rule string // Rule string like: "max:6"
}
// apiNoValidation is an interface that marks current struct not validated by package `gvalid`.
type apiNoValidation interface {
// iNoValidation is an interface that marks current struct not validated by package `gvalid`.
type iNoValidation interface {
NoValidation()
}

View File

@ -37,7 +37,7 @@ func (v *Validator) doCheckStruct(object interface{}) Error {
for _, field := range fieldMap {
if field.IsEmbedded() {
// No validation interface implements check.
if _, ok := field.Value.Interface().(apiNoValidation); ok {
if _, ok := field.Value.Interface().(iNoValidation); ok {
continue
}
if _, ok := field.TagLookup(noValidationTagName); ok {

View File

@ -22,7 +22,7 @@ import (
"github.com/gogf/gf/util/gutil"
)
type apiTime interface {
type iTime interface {
Date() (year int, month time.Month, day int)
IsZero() bool
}
@ -244,7 +244,7 @@ func (v *Validator) doCheckBuildInRules(input doCheckBuildInRulesInput) (match b
// Date rules.
case "date":
// support for time value, eg: gtime.Time/*gtime.Time, time.Time/*time.Time.
if v, ok := input.Value.(apiTime); ok {
if v, ok := input.Value.(iTime); ok {
return !v.IsZero(), nil
}
match = gregex.IsMatchString(`\d{4}[\.\-\_/]{0,1}\d{2}[\.\-\_/]{0,1}\d{2}`, valueStr)
@ -252,7 +252,7 @@ func (v *Validator) doCheckBuildInRules(input doCheckBuildInRulesInput) (match b
// Date rule with specified format.
case "date-format":
// support for time value, eg: gtime.Time/*gtime.Time, time.Time/*time.Time.
if v, ok := input.Value.(apiTime); ok {
if v, ok := input.Value.(iTime); ok {
return !v.IsZero(), nil
}
if _, err := gtime.StrToTimeFormat(valueStr, input.RulePattern); err == nil {