mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
improve function gutil.Dump/DumpWithType
This commit is contained in:
parent
56c12ad7c3
commit
0c43e7986f
@ -12,6 +12,7 @@ import (
|
||||
"github.com/gogf/gf/v2/internal/structs"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ExportOption specifies the behavior of function Export.
|
||||
@ -56,12 +57,17 @@ type doExportOption struct {
|
||||
}
|
||||
|
||||
func doExport(value interface{}, indent string, buffer *bytes.Buffer, option doExportOption) {
|
||||
if value == nil {
|
||||
buffer.WriteString(`<nil>`)
|
||||
return
|
||||
}
|
||||
var (
|
||||
reflectValue = reflect.ValueOf(value)
|
||||
reflectKind = reflectValue.Kind()
|
||||
reflectTypeName = reflectValue.Type().String()
|
||||
reflectTypeName = reflect.TypeOf(value).String()
|
||||
newIndent = indent + dumpIndent
|
||||
)
|
||||
reflectTypeName = strings.ReplaceAll(reflectTypeName, `[]uint8`, `[]byte`)
|
||||
if option.WithoutType {
|
||||
reflectTypeName = ""
|
||||
}
|
||||
@ -73,10 +79,10 @@ func doExport(value interface{}, indent string, buffer *bytes.Buffer, option doE
|
||||
case reflect.Slice, reflect.Array:
|
||||
if _, ok := value.([]byte); ok {
|
||||
if option.WithoutType {
|
||||
buffer.WriteString(fmt.Sprintf("\"%v\"\n", value))
|
||||
buffer.WriteString(fmt.Sprintf(`"%s"`, value))
|
||||
} else {
|
||||
buffer.WriteString(fmt.Sprintf(
|
||||
"%s(%d) \"%v\"\n",
|
||||
`%s(%d) "%s"`,
|
||||
reflectTypeName,
|
||||
len(reflectValue.String()),
|
||||
value,
|
||||
|
@ -73,7 +73,7 @@ func Test_Dump(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_DumpBrief(t *testing.T) {
|
||||
func TestDumpWithType(t *testing.T) {
|
||||
type CommonReq struct {
|
||||
AppId int64 `json:"appId" v:"required" in:"path" des:"应用Id" sum:"应用Id Summary"`
|
||||
ResourceId string `json:"resourceId" in:"query" des:"资源Id" sum:"资源Id Summary"`
|
||||
@ -127,6 +127,7 @@ func Test_DumpBrief(t *testing.T) {
|
||||
100: 100,
|
||||
})
|
||||
gutil.DumpWithType(req)
|
||||
gutil.DumpWithType([][]byte{[]byte("hello")})
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ func (v *Validator) doCheckStruct(ctx context.Context, object interface{}) Error
|
||||
if _, ok := field.TagLookup(noValidationTagName); ok {
|
||||
continue
|
||||
}
|
||||
if err := v.doCheckStruct(ctx, field.Value); err != nil {
|
||||
if err = v.doCheckStruct(ctx, field.Value); err != nil {
|
||||
// It merges the errors into single error map.
|
||||
for k, m := range err.(*validationError).errors {
|
||||
errorMaps[k] = m
|
||||
|
Loading…
Reference in New Issue
Block a user