add json features '-' and 'omitempty' for gconv.Map, like package 'json' from stdlib; add internal package 'empty', to check empty variable
@@ -14,7 +14,7 @@ import (
)
-func Test_Map(t *testing.T) {
+func Test_Map_Basic(t *testing.T) {
gtest.Case(t, func() {
m1 := map[string]string{
"k" : "v",
@@ -37,3 +37,78 @@ func Test_Map(t *testing.T) {
})
}
+func Test_Map_StructWithGconvTag(t *testing.T) {
+ gtest.Case(t, func() {
+ type User struct {
+ Uid int
+ Name string
+ SiteUrl string `gconv:"-"`
+ NickName string `gconv:"nickname, omitempty"`
+ Pass1 string `gconv:"password1"`
+ Pass2 string `gconv:"password2"`
+ }
+ user1 := User{
+ Uid : 100,
+ Name : "john",
+ SiteUrl : "https://goframe.org",
+ Pass1 : "123",
+ Pass2 : "456",
+ }
+ user2 := &user1
+ map1 := gconv.Map(user1)
+ map2 := gconv.Map(user2)
+ gtest.Assert(map1["Uid"], 100)
+ gtest.Assert(map1["Name"], "john")
+ gtest.Assert(map1["SiteUrl"], nil)
+ gtest.Assert(map1["NickName"], nil)
+ gtest.Assert(map1["nickname"], nil)
+ gtest.Assert(map1["password1"], "123")
+ gtest.Assert(map1["password2"], "456")
+
+ gtest.Assert(map2["Uid"], 100)
+ gtest.Assert(map2["Name"], "john")
+ gtest.Assert(map2["SiteUrl"], nil)
+ gtest.Assert(map2["NickName"], nil)
+ gtest.Assert(map2["nickname"], nil)
+ gtest.Assert(map2["password1"], "123")
+ gtest.Assert(map2["password2"], "456")
+ })
+}
+
+func Test_Map_StructWithJsonTag(t *testing.T) {
+ gtest.Case(t, func() {
+ type User struct {
+ Uid int
+ Name string
+ SiteUrl string `json:"-"`
+ NickName string `json:"nickname, omitempty"`
+ Pass1 string `json:"password1"`
+ Pass2 string `json:"password2"`
+ }
+ user1 := User{
+ Uid : 100,
+ Name : "john",
+ SiteUrl : "https://goframe.org",
+ Pass1 : "123",
+ Pass2 : "456",
+ }
+ user2 := &user1
+ map1 := gconv.Map(user1)
+ map2 := gconv.Map(user2)
+ gtest.Assert(map1["Uid"], 100)
+ gtest.Assert(map1["Name"], "john")
+ gtest.Assert(map1["SiteUrl"], nil)
+ gtest.Assert(map1["NickName"], nil)
+ gtest.Assert(map1["nickname"], nil)
+ gtest.Assert(map1["password1"], "123")
+ gtest.Assert(map1["password2"], "456")
+
+ gtest.Assert(map2["Uid"], 100)
+ gtest.Assert(map2["Name"], "john")
+ gtest.Assert(map2["SiteUrl"], nil)
+ gtest.Assert(map2["NickName"], nil)
+ gtest.Assert(map2["nickname"], nil)
+ gtest.Assert(map2["password1"], "123")
+ gtest.Assert(map2["password2"], "456")
+ })
+}
