improve function gutil.Dump/DumpWithType

This commit is contained in:
John Guo 2021-11-03 13:27:37 +08:00
parent 56c12ad7c3
commit 0c43e7986f
3 changed files with 12 additions and 5 deletions

View File

@ -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,

View File

@ -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")})
})
}

View File

@ -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