github.com/zhongdalu/gf@v1.0.0/g/util/gconv/gconv_z_unit_map_test.go (about) 1 // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 package gconv_test 8 9 import ( 10 "github.com/zhongdalu/gf/g" 11 "github.com/zhongdalu/gf/g/test/gtest" 12 "github.com/zhongdalu/gf/g/util/gconv" 13 "testing" 14 ) 15 16 func Test_Map_Basic(t *testing.T) { 17 gtest.Case(t, func() { 18 m1 := map[string]string{ 19 "k": "v", 20 } 21 m2 := map[int]string{ 22 3: "v", 23 } 24 m3 := map[float64]float32{ 25 1.22: 3.1, 26 } 27 gtest.Assert(gconv.Map(m1), g.Map{ 28 "k": "v", 29 }) 30 gtest.Assert(gconv.Map(m2), g.Map{ 31 "3": "v", 32 }) 33 gtest.Assert(gconv.Map(m3), g.Map{ 34 "1.22": "3.1", 35 }) 36 }) 37 } 38 39 func Test_Map_StructWithGconvTag(t *testing.T) { 40 gtest.Case(t, func() { 41 type User struct { 42 Uid int 43 Name string 44 SiteUrl string `gconv:"-"` 45 NickName string `gconv:"nickname, omitempty"` 46 Pass1 string `gconv:"password1"` 47 Pass2 string `gconv:"password2"` 48 } 49 user1 := User{ 50 Uid: 100, 51 Name: "john", 52 SiteUrl: "https://goframe.org", 53 Pass1: "123", 54 Pass2: "456", 55 } 56 user2 := &user1 57 map1 := gconv.Map(user1) 58 map2 := gconv.Map(user2) 59 gtest.Assert(map1["Uid"], 100) 60 gtest.Assert(map1["Name"], "john") 61 gtest.Assert(map1["SiteUrl"], nil) 62 gtest.Assert(map1["NickName"], nil) 63 gtest.Assert(map1["nickname"], nil) 64 gtest.Assert(map1["password1"], "123") 65 gtest.Assert(map1["password2"], "456") 66 67 gtest.Assert(map2["Uid"], 100) 68 gtest.Assert(map2["Name"], "john") 69 gtest.Assert(map2["SiteUrl"], nil) 70 gtest.Assert(map2["NickName"], nil) 71 gtest.Assert(map2["nickname"], nil) 72 gtest.Assert(map2["password1"], "123") 73 gtest.Assert(map2["password2"], "456") 74 }) 75 } 76 77 func Test_Map_StructWithJsonTag(t *testing.T) { 78 gtest.Case(t, func() { 79 type User struct { 80 Uid int 81 Name string 82 SiteUrl string `json:"-"` 83 NickName string `json:"nickname, omitempty"` 84 Pass1 string `json:"password1"` 85 Pass2 string `json:"password2"` 86 } 87 user1 := User{ 88 Uid: 100, 89 Name: "john", 90 SiteUrl: "https://goframe.org", 91 Pass1: "123", 92 Pass2: "456", 93 } 94 user2 := &user1 95 map1 := gconv.Map(user1) 96 map2 := gconv.Map(user2) 97 gtest.Assert(map1["Uid"], 100) 98 gtest.Assert(map1["Name"], "john") 99 gtest.Assert(map1["SiteUrl"], nil) 100 gtest.Assert(map1["NickName"], nil) 101 gtest.Assert(map1["nickname"], nil) 102 gtest.Assert(map1["password1"], "123") 103 gtest.Assert(map1["password2"], "456") 104 105 gtest.Assert(map2["Uid"], 100) 106 gtest.Assert(map2["Name"], "john") 107 gtest.Assert(map2["SiteUrl"], nil) 108 gtest.Assert(map2["NickName"], nil) 109 gtest.Assert(map2["nickname"], nil) 110 gtest.Assert(map2["password1"], "123") 111 gtest.Assert(map2["password2"], "456") 112 }) 113 } 114 115 func Test_Map_PrivateAttribute(t *testing.T) { 116 type User struct { 117 Id int 118 name string 119 } 120 gtest.Case(t, func() { 121 user := &User{1, "john"} 122 gtest.Assert(gconv.Map(user), g.Map{"Id": 1}) 123 }) 124 } 125 126 func Test_Map_StructInherit(t *testing.T) { 127 gtest.Case(t, func() { 128 type Ids struct { 129 Id int `json:"id"` 130 Uid int `json:"uid"` 131 } 132 type Base struct { 133 Ids 134 CreateTime string `json:"create_time"` 135 } 136 type User struct { 137 Base 138 Passport string `json:"passport"` 139 Password string `json:"password"` 140 Nickname string `json:"nickname"` 141 } 142 user := new(User) 143 user.Id = 100 144 user.Nickname = "john" 145 user.CreateTime = "2019" 146 m := gconv.MapDeep(user) 147 gtest.Assert(m["id"], user.Id) 148 gtest.Assert(m["nickname"], user.Nickname) 149 gtest.Assert(m["create_time"], user.CreateTime) 150 }) 151 }