github.com/wangyougui/gf/v2@v2.6.5/util/gutil/gutil_z_unit_dump_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). 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/wangyougui/gf. 6 7 package gutil_test 8 9 import ( 10 "bytes" 11 "testing" 12 13 "github.com/wangyougui/gf/v2/container/gtype" 14 "github.com/wangyougui/gf/v2/frame/g" 15 "github.com/wangyougui/gf/v2/net/ghttp" 16 "github.com/wangyougui/gf/v2/os/gtime" 17 "github.com/wangyougui/gf/v2/test/gtest" 18 "github.com/wangyougui/gf/v2/text/gstr" 19 "github.com/wangyougui/gf/v2/util/gmeta" 20 "github.com/wangyougui/gf/v2/util/gutil" 21 ) 22 23 func Test_Dump(t *testing.T) { 24 type CommonReq struct { 25 AppId int64 `json:"appId" v:"required" in:"path" des:"应用Id" sum:"应用Id Summary"` 26 ResourceId string `json:"resourceId" in:"query" des:"资源Id" sum:"资源Id Summary"` 27 } 28 type SetSpecInfo struct { 29 StorageType string `v:"required|in:CLOUD_PREMIUM,CLOUD_SSD,CLOUD_HSSD" des:"StorageType"` 30 Shards int32 `des:"shards 分片数" sum:"Shards Summary"` 31 Params []string `des:"默认参数(json 串-ClickHouseParams)" sum:"Params Summary"` 32 } 33 type CreateResourceReq struct { 34 CommonReq 35 gmeta.Meta `path:"/CreateResourceReq" method:"POST" tags:"default" sum:"CreateResourceReq sum"` 36 Name string 37 CreatedAt *gtime.Time 38 SetMap map[string]*SetSpecInfo 39 SetSlice []SetSpecInfo 40 Handler ghttp.HandlerFunc 41 internal string 42 } 43 req := &CreateResourceReq{ 44 CommonReq: CommonReq{ 45 AppId: 12345678, 46 ResourceId: "tdchqy-xxx", 47 }, 48 Name: "john", 49 CreatedAt: gtime.Now(), 50 SetMap: map[string]*SetSpecInfo{ 51 "test1": { 52 StorageType: "ssd", 53 Shards: 2, 54 Params: []string{"a", "b", "c"}, 55 }, 56 "test2": { 57 StorageType: "hssd", 58 Shards: 10, 59 Params: []string{}, 60 }, 61 }, 62 SetSlice: []SetSpecInfo{ 63 { 64 StorageType: "hssd", 65 Shards: 10, 66 Params: []string{"h"}, 67 }, 68 }, 69 } 70 gtest.C(t, func(t *gtest.T) { 71 gutil.Dump(map[int]int{ 72 100: 100, 73 }) 74 gutil.Dump(req) 75 gutil.Dump(true, false) 76 gutil.Dump(make(chan int)) 77 gutil.Dump(func() {}) 78 gutil.Dump(nil) 79 gutil.Dump(gtype.NewInt(1)) 80 }) 81 } 82 83 func Test_Dump_Map(t *testing.T) { 84 gtest.C(t, func(t *gtest.T) { 85 buffer := bytes.NewBuffer(nil) 86 m := g.Map{ 87 "k1": g.Map{ 88 "k2": "v2", 89 }, 90 } 91 gutil.DumpTo(buffer, m, gutil.DumpOption{}) 92 t.Assert(buffer.String(), `{ 93 "k1": { 94 "k2": "v2", 95 }, 96 }`) 97 }) 98 } 99 100 func TestDumpWithType(t *testing.T) { 101 type CommonReq struct { 102 AppId int64 `json:"appId" v:"required" in:"path" des:"应用Id" sum:"应用Id Summary"` 103 ResourceId string `json:"resourceId" in:"query" des:"资源Id" sum:"资源Id Summary"` 104 } 105 type SetSpecInfo struct { 106 StorageType string `v:"required|in:CLOUD_PREMIUM,CLOUD_SSD,CLOUD_HSSD" des:"StorageType"` 107 Shards int32 `des:"shards 分片数" sum:"Shards Summary"` 108 Params []string `des:"默认参数(json 串-ClickHouseParams)" sum:"Params Summary"` 109 } 110 type CreateResourceReq struct { 111 CommonReq 112 gmeta.Meta `path:"/CreateResourceReq" method:"POST" tags:"default" sum:"CreateResourceReq sum"` 113 Name string 114 CreatedAt *gtime.Time 115 SetMap map[string]*SetSpecInfo `v:"required" des:"配置Map"` 116 SetSlice []SetSpecInfo `v:"required" des:"配置Slice"` 117 Handler ghttp.HandlerFunc 118 internal string 119 } 120 req := &CreateResourceReq{ 121 CommonReq: CommonReq{ 122 AppId: 12345678, 123 ResourceId: "tdchqy-xxx", 124 }, 125 Name: "john", 126 CreatedAt: gtime.Now(), 127 SetMap: map[string]*SetSpecInfo{ 128 "test1": { 129 StorageType: "ssd", 130 Shards: 2, 131 Params: []string{"a", "b", "c"}, 132 }, 133 "test2": { 134 StorageType: "hssd", 135 Shards: 10, 136 Params: []string{}, 137 }, 138 }, 139 SetSlice: []SetSpecInfo{ 140 { 141 StorageType: "hssd", 142 Shards: 10, 143 Params: []string{"h"}, 144 }, 145 }, 146 } 147 gtest.C(t, func(t *gtest.T) { 148 gutil.DumpWithType(map[int]int{ 149 100: 100, 150 }) 151 gutil.DumpWithType(req) 152 gutil.DumpWithType([][]byte{[]byte("hello")}) 153 }) 154 } 155 156 func Test_Dump_Slashes(t *testing.T) { 157 type Req struct { 158 Content string 159 } 160 req := &Req{ 161 Content: `{"name":"john", "age":18}`, 162 } 163 gtest.C(t, func(t *gtest.T) { 164 gutil.Dump(req) 165 gutil.Dump(req.Content) 166 167 gutil.DumpWithType(req) 168 gutil.DumpWithType(req.Content) 169 }) 170 } 171 172 // https://github.com/wangyougui/gf/issues/1661 173 func Test_Dump_Issue1661(t *testing.T) { 174 type B struct { 175 ba int 176 bb string 177 } 178 type A struct { 179 aa int 180 ab string 181 cc []B 182 } 183 gtest.C(t, func(t *gtest.T) { 184 var q1 []A 185 var q2 []A 186 q2 = make([]A, 0) 187 q1 = []A{{aa: 1, ab: "1", cc: []B{{ba: 1}, {ba: 2}, {ba: 3}}}, {aa: 2, ab: "2", cc: []B{{ba: 1}, {ba: 2}, {ba: 3}}}} 188 for _, q1v := range q1 { 189 x := []string{"11", "22"} 190 for _, iv2 := range x { 191 ls := q1v 192 for i := range ls.cc { 193 sj := iv2 194 ls.cc[i].bb = sj 195 } 196 q2 = append(q2, ls) 197 } 198 } 199 buffer := bytes.NewBuffer(nil) 200 gutil.DumpTo(buffer, q2, gutil.DumpOption{}) 201 t.Assert(buffer.String(), `[ 202 { 203 aa: 1, 204 ab: "1", 205 cc: [ 206 { 207 ba: 1, 208 bb: "22", 209 }, 210 { 211 ba: 2, 212 bb: "22", 213 }, 214 { 215 ba: 3, 216 bb: "22", 217 }, 218 ], 219 }, 220 { 221 aa: 1, 222 ab: "1", 223 cc: [ 224 { 225 ba: 1, 226 bb: "22", 227 }, 228 { 229 ba: 2, 230 bb: "22", 231 }, 232 { 233 ba: 3, 234 bb: "22", 235 }, 236 ], 237 }, 238 { 239 aa: 2, 240 ab: "2", 241 cc: [ 242 { 243 ba: 1, 244 bb: "22", 245 }, 246 { 247 ba: 2, 248 bb: "22", 249 }, 250 { 251 ba: 3, 252 bb: "22", 253 }, 254 ], 255 }, 256 { 257 aa: 2, 258 ab: "2", 259 cc: [ 260 { 261 ba: 1, 262 bb: "22", 263 }, 264 { 265 ba: 2, 266 bb: "22", 267 }, 268 { 269 ba: 3, 270 bb: "22", 271 }, 272 ], 273 }, 274 ]`) 275 }) 276 } 277 278 func Test_Dump_Cycle_Attribute(t *testing.T) { 279 type Abc struct { 280 ab int 281 cd *Abc 282 } 283 abc := Abc{ab: 3} 284 abc.cd = &abc 285 gtest.C(t, func(t *gtest.T) { 286 buffer := bytes.NewBuffer(nil) 287 g.DumpTo(buffer, abc, gutil.DumpOption{}) 288 t.Assert(gstr.Contains(buffer.String(), "cycle"), true) 289 }) 290 } 291 292 func Test_DumpJson(t *testing.T) { 293 gtest.C(t, func(t *gtest.T) { 294 var jsonContent = `{"a":1,"b":2}` 295 gutil.DumpJson(jsonContent) 296 }) 297 }