Add StructUtil for provide more rich functions (#79)

* add support json tag attribute for StructToMap function

* add the structutil to provide more rich functions and fixed #77
This commit is contained in:
zm authored and GitHub committed 2023-03-13 19:28:37 +08:00
1 parent 1755dd249b
commit 924589d2da
9 files changed
+263 -64

No files matched your search

+6 -7
View File
@@ -180,7 +180,7 @@ func TestToMap(t *testing.T) {
func TestStructToMap(t *testing.T) {
assert := internal.NewAssert(t, "TestStructToMap")
t.Run("StructToMap", func(t *testing.T) {
t.Run("StructToMap", func(_ *testing.T) {
type People struct {
Name string `json:"name"`
age int
@@ -194,7 +194,7 @@ func TestStructToMap(t *testing.T) {
assert.Equal(expected, pm)
})
t.Run("StructToMapWithJsonAttr", func(t *testing.T) {
t.Run("StructToMapWithJsonAttr", func(_ *testing.T) {
type People struct {
Name string `json:"name,omitempty"` // json tag with attribute
Phone string `json:"phone"` // json tag without attribute
@@ -202,13 +202,12 @@ func TestStructToMap(t *testing.T) {
age int // no tag
}
p := People{
"test",
"1111",
"male",
100,
Phone: "1111",
Sex: "male",
age: 100,
}
pm, _ := StructToMap(p)
var expected = map[string]any{"name": "test", "phone": "1111"}
var expected = map[string]any{"phone": "1111"}
assert.Equal(expected, pm)
})
}