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