mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 03:05:05 +08:00
improve Dump feature for package gutil
This commit is contained in:
parent
afa1f78a02
commit
acd1989fa1
@ -18,10 +18,10 @@ import (
|
||||
// New
|
||||
func ExampleVarNew() {
|
||||
v := gvar.New(400)
|
||||
g.Dump(v)
|
||||
fmt.Println(v)
|
||||
|
||||
// Output:
|
||||
// "400"
|
||||
// 400
|
||||
}
|
||||
|
||||
// Clone
|
||||
|
@ -83,6 +83,8 @@ func ReflectValueToInterface(v reflect.Value) (value interface{}, ok bool) {
|
||||
return v.String(), true
|
||||
case reflect.Ptr:
|
||||
return ReflectValueToInterface(v.Elem())
|
||||
case reflect.Interface:
|
||||
return ReflectValueToInterface(v.Elem())
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
|
@ -169,6 +169,9 @@ func doDump(value interface{}, indent string, buffer *bytes.Buffer, option doDum
|
||||
buffer.WriteString(fmt.Sprintf(`<%s>`, reflectTypeName))
|
||||
}
|
||||
|
||||
case reflect.Interface:
|
||||
doDump(exportInternalInput.ReflectValue.Elem(), indent, buffer, option)
|
||||
|
||||
default:
|
||||
doDumpDefault(exportInternalInput)
|
||||
}
|
||||
@ -283,7 +286,17 @@ func doDumpStruct(in doDumpInternalInput) {
|
||||
Pointer: in.Value,
|
||||
RecursiveOption: gstructs.RecursiveOptionEmbedded,
|
||||
})
|
||||
if len(structFields) == 0 {
|
||||
var (
|
||||
hasNoExportedFields = true
|
||||
_, isReflectValue = in.Value.(reflect.Value)
|
||||
)
|
||||
for _, field := range structFields {
|
||||
if field.IsExported() {
|
||||
hasNoExportedFields = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isReflectValue && (len(structFields) == 0 || hasNoExportedFields) {
|
||||
var (
|
||||
structContentStr = ""
|
||||
attributeCountStr = "0"
|
||||
@ -295,6 +308,11 @@ func doDumpStruct(in doDumpInternalInput) {
|
||||
} else if v, ok := in.Value.(iMarshalJSON); ok {
|
||||
b, _ := v.MarshalJSON()
|
||||
structContentStr = string(b)
|
||||
} else {
|
||||
// Has no common interface implements.
|
||||
if len(structFields) != 0 {
|
||||
goto dumpStructFields
|
||||
}
|
||||
}
|
||||
if structContentStr == "" {
|
||||
structContentStr = "{}"
|
||||
@ -314,6 +332,8 @@ func doDumpStruct(in doDumpInternalInput) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
dumpStructFields:
|
||||
var (
|
||||
maxSpaceNum = 0
|
||||
tmpSpaceNum = 0
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
@ -72,6 +73,23 @@ func Test_Dump(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Dump_Map(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
m := g.Map{
|
||||
"k1": g.Map{
|
||||
"k2": "v2",
|
||||
},
|
||||
}
|
||||
gutil.DumpTo(buffer, m, gutil.DumpOption{})
|
||||
t.Assert(buffer.String(), `{
|
||||
"k1": {
|
||||
"k2": "v2",
|
||||
},
|
||||
}`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDumpWithType(t *testing.T) {
|
||||
type CommonReq struct {
|
||||
AppId int64 `json:"appId" v:"required" in:"path" des:"应用Id" sum:"应用Id Summary"`
|
||||
|
Loading…
x
Reference in New Issue
Block a user