github.com/wfusion/gofusion@v1.1.14/test/internal/mock/faker.go (about) 1 package mock 2 3 import ( 4 "math" 5 "time" 6 "unsafe" 7 8 "github.com/go-faker/faker/v4" 9 "github.com/go-faker/faker/v4/pkg/interfaces" 10 "github.com/go-faker/faker/v4/pkg/options" 11 12 "github.com/wfusion/gofusion/common/utils" 13 "github.com/wfusion/gofusion/common/utils/serialize" 14 ) 15 16 type randomOption struct { 17 ignored []string 18 } 19 20 func IgnoreFields(ignored ...string) utils.OptionFunc[randomOption] { 21 return func(o *randomOption) { 22 o.ignored = ignored 23 } 24 } 25 26 func GenObjBySerializeAlgo(algo serialize.Algorithm) (data any) { 27 switch algo { 28 case serialize.AlgorithmGob: 29 // gob may unmarshal time.Time value to a different internal member time.Time 30 // gob unmarshal false boolean pointer as a nil value 31 ignoreFields := []string{"Time", "Timep", "Boolp"} 32 data = newRandomObj(IgnoreFields(ignoreFields...)) 33 case serialize.AlgorithmJson, serialize.AlgorithmMsgpack, serialize.AlgorithmCbor: 34 // json unmarshal all number to any type as float64 or json.Number type 35 // msgpack unmarshal a compact number type to any type, e.g. int -> int8 36 // cbor unmarshal integer type to any type as uint64 37 ignoreFields := []string{"AnySlice", "AnypSlice"} 38 data = newCommonObj(IgnoreFields(ignoreFields...)) 39 default: 40 data = newRandomObj() 41 } 42 return 43 } 44 45 func GenObjListBySerializeAlgo(algo serialize.Algorithm, num int) (dataList any) { 46 switch algo { 47 case serialize.AlgorithmGob: 48 // gob may unmarshal time.Time value to a different internal member time.Time 49 // gob unmarshal false boolean pointer as a nil value 50 ignoreFields := []string{"Time", "Timep", "Boolp"} 51 dataList = newRandomObjList(num, IgnoreFields(ignoreFields...)) 52 case serialize.AlgorithmJson, serialize.AlgorithmMsgpack, serialize.AlgorithmCbor: 53 // json unmarshal all number to any type as float64 or json.Number type 54 // msgpack unmarshal a compact number type to any type, e.g. int -> int8 55 // cbor unmarshal integer type to any type as uint64 56 ignoreFields := []string{"AnySlice", "AnypSlice"} 57 dataList = newCommonObjList(num, IgnoreFields(ignoreFields...)) 58 default: 59 dataList = newRandomObjList(num) 60 } 61 return 62 } 63 64 func newCommonObj(opts ...utils.OptionExtender) (v *CommonObj) { 65 opt := utils.ApplyOptions[randomOption](opts...) 66 v = &CommonObj{ 67 CommonType: *mockCommonType(opt), 68 Basic: *mockCommonType(opt), 69 Basicp: mockCommonType(opt), 70 Int64Map: map[int64]*CommonType{ 71 -1: mockCommonType(opt), 72 0: mockCommonType(opt), 73 1: mockCommonType(opt), 74 2: mockCommonType(opt), 75 }, 76 StringMap: map[string]*CommonType{ 77 "": mockCommonType(opt), 78 "string": mockCommonType(opt), 79 }, 80 Array: [2]*CommonType{mockCommonType(opt), mockCommonType(opt)}, 81 } 82 83 return 84 } 85 86 func newCommonObjList(num int, opts ...utils.OptionExtender) (vList []*CommonObj) { 87 vList = make([]*CommonObj, 0, num) 88 for i := 0; i < num; i++ { 89 vList = append(vList, newCommonObj(opts...)) 90 } 91 return 92 } 93 94 type CommonObj struct { 95 CommonType 96 97 Basic CommonType 98 Basicp *CommonType 99 100 Int64Map map[int64]*CommonType 101 StringMap map[string]*CommonType 102 103 Array [2]*CommonType 104 105 Nil *CommonType 106 } 107 108 func (r *CommonObj) EventType() string { 109 return "common_object_created" 110 } 111 112 type CommonType struct { 113 Str string 114 Strp *string 115 116 StrSlice []string 117 StrpSlice []*string 118 119 Rune rune 120 Runep *rune 121 122 RuneSlice []rune 123 RunepSlice []*rune 124 125 Byte byte 126 Bytep *byte 127 128 ByteSlice []byte 129 BytepSlice []*byte 130 131 Bool bool 132 Boolp *bool 133 134 BoolSlice []bool 135 BoolpSlice []*bool 136 137 Int int 138 Int8 int8 139 Int16 int16 140 Int32 int32 141 Int64 int64 142 143 IntSlice []int 144 Int8Slice []int8 145 Int16Slice []int16 146 Int32Slice []int32 147 Int64Slice []int64 148 149 Intp *int 150 Int8p *int8 151 Int16p *int16 152 Int32p *int32 153 Int64p *int64 154 155 IntpSlice []*int 156 Int8pSlice []*int8 157 Int16pSlice []*int16 158 Int32pSlice []*int32 159 Int64pSlice []*int64 160 161 Uint uint 162 Uint8 uint8 163 Uint16 uint16 164 Uint32 uint32 165 Uint64 uint64 166 167 UintSlice []uint 168 Uint8Slice []uint8 169 Uint16Slice []uint16 170 Uint32Slice []uint32 171 Uint64Slice []uint64 172 173 Uintp *uint 174 Uint8p *uint8 175 Uint16p *uint16 176 Uint32p *uint32 177 Uint64p *uint64 178 179 UintpSlice []*uint 180 Uint8pSlice []*uint8 181 Uint16pSlice []*uint16 182 Uint32pSlice []*uint32 183 Uint64pSlice []*uint64 184 185 Float32 float32 186 Float64 float64 187 188 Float32Slice []float32 189 Float64Slice []float64 190 191 Float32p *float32 192 Float64p *float64 193 194 Float32pSlice []*float32 195 Float64pSlice []*float64 196 197 Any any `faker:"-"` 198 Anyp *any `faker:"-"` 199 200 AnySlice []any `faker:"-"` 201 AnypSlice []*any `faker:"-"` 202 } 203 204 func mockCommonType(opt *randomOption) (v *CommonType) { 205 utils.MustSuccess(faker.FakeData(&v, 206 options.WithNilIfLenIsZero(true), 207 options.WithGenerateUniqueValues(true), 208 options.WithRandomIntegerBoundaries(interfaces.RandomIntegerBoundary{Start: 1, End: math.MaxInt8}), 209 options.WithRandomFloatBoundaries(interfaces.RandomFloatBoundary{Start: 1, End: math.MaxInt8}), 210 options.WithFieldsToIgnore(opt.ignored...), 211 )) 212 213 ignored := utils.NewSet(opt.ignored...) 214 setter := func(name string, fn func()) { 215 if !ignored.Contains(name) { 216 fn() 217 } 218 } 219 220 setter("Strp", func() { v.Strp = utils.AnyPtr(v.Str) }) 221 setter("StrpSlice", func() { v.StrpSlice = utils.SliceMapping(v.StrSlice, utils.AnyPtr[string]) }) 222 223 setter("Runep", func() { v.Runep = utils.AnyPtr(v.Rune) }) 224 setter("RunepSlice", func() { v.RunepSlice = utils.SliceMapping(v.RuneSlice, utils.AnyPtr[rune]) }) 225 226 setter("Bytep", func() { v.Bytep = utils.AnyPtr(v.Byte) }) 227 setter("BytepSlice", func() { v.BytepSlice = utils.SliceMapping(v.ByteSlice, utils.AnyPtr[byte]) }) 228 229 setter("Boolp", func() { v.Boolp = utils.AnyPtr(v.Bool) }) 230 231 setter("Intp", func() { v.Intp = utils.AnyPtr(v.Int) }) 232 setter("Int8p", func() { v.Int8p = utils.AnyPtr(v.Int8) }) 233 setter("Int16p", func() { v.Int16p = utils.AnyPtr(v.Int16) }) 234 setter("Int32p", func() { v.Int32p = utils.AnyPtr(v.Int32) }) 235 setter("Int64p", func() { v.Int64p = utils.AnyPtr(v.Int64) }) 236 237 setter("IntpSlice", func() { v.IntpSlice = utils.SliceMapping(v.IntSlice, utils.AnyPtr[int]) }) 238 setter("Int8pSlice", func() { v.Int8pSlice = utils.SliceMapping(v.Int8Slice, utils.AnyPtr[int8]) }) 239 setter("Int16pSlice", func() { v.Int16pSlice = utils.SliceMapping(v.Int16Slice, utils.AnyPtr[int16]) }) 240 setter("Int32pSlice", func() { v.Int32pSlice = utils.SliceMapping(v.Int32Slice, utils.AnyPtr[int32]) }) 241 setter("Int64pSlice", func() { v.Int64pSlice = utils.SliceMapping(v.Int64Slice, utils.AnyPtr[int64]) }) 242 243 setter("Uintp", func() { v.Uintp = utils.AnyPtr(v.Uint) }) 244 setter("Uint8p", func() { v.Uint8p = utils.AnyPtr(v.Uint8) }) 245 setter("Uint16p", func() { v.Uint16p = utils.AnyPtr(v.Uint16) }) 246 setter("Uint32p", func() { v.Uint32p = utils.AnyPtr(v.Uint32) }) 247 setter("Uint64p", func() { v.Uint64p = utils.AnyPtr(v.Uint64) }) 248 setter("UintpSlice", func() { v.UintpSlice = utils.SliceMapping(v.UintSlice, utils.AnyPtr[uint]) }) 249 setter("Uint8pSlice", func() { v.Uint8pSlice = utils.SliceMapping(v.Uint8Slice, utils.AnyPtr[uint8]) }) 250 setter("Uint16pSlice", func() { v.Uint16pSlice = utils.SliceMapping(v.Uint16Slice, utils.AnyPtr[uint16]) }) 251 setter("Uint32pSlice", func() { v.Uint32pSlice = utils.SliceMapping(v.Uint32Slice, utils.AnyPtr[uint32]) }) 252 setter("Uint64pSlice", func() { v.Uint64pSlice = utils.SliceMapping(v.Uint64Slice, utils.AnyPtr[uint64]) }) 253 254 setter("Float32p", func() { v.Float32p = utils.AnyPtr(v.Float32) }) 255 setter("Float64p", func() { v.Float64p = utils.AnyPtr(v.Float64) }) 256 setter("Float32pSlice", func() { v.Float32pSlice = utils.SliceMapping(v.Float32Slice, utils.AnyPtr[float32]) }) 257 setter("Float64pSlice", func() { v.Float64pSlice = utils.SliceMapping(v.Float64Slice, utils.AnyPtr[float64]) }) 258 259 setter("Any", func() { v.Any = "any" }) 260 setter("Anyp", func() { v.Anyp = utils.AnyPtr(v.Any) }) 261 setter("AnySlice", func() { v.AnySlice = []any{1, 1.1, "any", true, nil} }) 262 setter("AnypSlice", func() { v.AnypSlice = utils.SliceMapping(v.AnySlice, utils.AnyPtr[any]) }) 263 264 return 265 } 266 267 func newRandomObj(opts ...utils.OptionExtender) (v *RandomObj) { 268 opt := utils.ApplyOptions[randomOption](opts...) 269 v = &RandomObj{ 270 AllType: *mockAllType(opt), 271 Basic: *mockAllType(opt), 272 Basicp: mockAllType(opt), 273 Int64Map: map[int64]*AllType{ 274 -1: mockAllType(opt), 275 0: mockAllType(opt), 276 1: mockAllType(opt), 277 2: mockAllType(opt), 278 }, 279 Float64Map: map[float64]*AllType{ 280 -1.1: mockAllType(opt), 281 -0.0: mockAllType(opt), 282 0.1: mockAllType(opt), 283 1.1: mockAllType(opt), 284 }, 285 StringMap: map[string]*AllType{ 286 "": mockAllType(opt), 287 "string": mockAllType(opt), 288 }, 289 Array: [2]*AllType{mockAllType(opt), mockAllType(opt)}, 290 } 291 292 return 293 } 294 295 func newRandomObjList(num int, opts ...utils.OptionExtender) (vList []*RandomObj) { 296 vList = make([]*RandomObj, 0, num) 297 for i := 0; i < num; i++ { 298 vList = append(vList, newRandomObj(opts...)) 299 } 300 return 301 } 302 303 type RandomObj struct { 304 AllType 305 306 Basic AllType 307 Basicp *AllType 308 309 Int64Map map[int64]*AllType 310 Float64Map map[float64]*AllType 311 StringMap map[string]*AllType 312 313 Array [2]*AllType 314 315 Nil *AllType 316 } 317 318 func (r *RandomObj) EventType() string { 319 return "random_object_created" 320 } 321 322 type AllType struct { 323 Str string 324 Strp *string 325 326 StrSlice []string 327 StrpSlice []*string 328 329 Rune rune 330 Runep *rune 331 332 RuneSlice []rune 333 RunepSlice []*rune 334 335 Byte byte 336 Bytep *byte 337 338 ByteSlice []byte 339 BytepSlice []*byte 340 341 Bool bool 342 Boolp *bool 343 344 BoolSlice []bool 345 BoolpSlice []*bool 346 347 Int int 348 Int8 int8 349 Int16 int16 350 Int32 int32 351 Int64 int64 352 353 IntSlice []int 354 Int8Slice []int8 355 Int16Slice []int16 356 Int32Slice []int32 357 Int64Slice []int64 358 359 Intp *int 360 Int8p *int8 361 Int16p *int16 362 Int32p *int32 363 Int64p *int64 364 365 IntpSlice []*int 366 Int8pSlice []*int8 367 Int16pSlice []*int16 368 Int32pSlice []*int32 369 Int64pSlice []*int64 370 371 Uint uint 372 Uint8 uint8 373 Uint16 uint16 374 Uint32 uint32 375 Uint64 uint64 376 377 UintSlice []uint 378 Uint8Slice []uint8 379 Uint16Slice []uint16 380 Uint32Slice []uint32 381 Uint64Slice []uint64 382 383 Uintp *uint 384 Uint8p *uint8 385 Uint16p *uint16 386 Uint32p *uint32 387 Uint64p *uint64 388 389 UintpSlice []*uint 390 Uint8pSlice []*uint8 391 Uint16pSlice []*uint16 392 Uint32pSlice []*uint32 393 Uint64pSlice []*uint64 394 395 Float32 float32 396 Float64 float64 397 398 Float32Slice []float32 399 Float64Slice []float64 400 401 Float32p *float32 402 Float64p *float64 403 404 Float32pSlice []*float32 405 Float64pSlice []*float64 406 407 Complex64 complex64 `faker:"-" json:"-"` 408 Complex128 complex128 `faker:"-" json:"-"` 409 410 Complex64Slice []complex64 `faker:"-" json:"-"` 411 Complex128Slice []complex128 `faker:"-" json:"-"` 412 413 Complex64p *complex64 `faker:"-" json:"-"` 414 Complex128p *complex128 `faker:"-" json:"-"` 415 416 Complex64pSlice []*complex64 `faker:"-" json:"-"` 417 Complex128pSlice []*complex128 `faker:"-" json:"-"` 418 419 Uintptr uintptr `faker:"-"` 420 Uintptrp *uintptr `faker:"-"` 421 422 UintptrSlice []uintptr `faker:"-"` 423 UintptrpSlice []*uintptr `faker:"-"` 424 425 Any any `faker:"-"` 426 Anyp *any `faker:"-"` 427 428 AnySlice []any `faker:"-"` 429 AnypSlice []*any `faker:"-"` 430 431 Time time.Time 432 Timep *time.Time 433 } 434 435 func mockAllType(opt *randomOption) (v *AllType) { 436 utils.MustSuccess(faker.FakeData(&v, 437 options.WithNilIfLenIsZero(true), 438 options.WithGenerateUniqueValues(true), 439 options.WithRandomIntegerBoundaries(interfaces.RandomIntegerBoundary{Start: 1, End: math.MaxInt8}), 440 options.WithRandomFloatBoundaries(interfaces.RandomFloatBoundary{Start: 1, End: math.MaxInt8}), 441 options.WithFieldsToIgnore(opt.ignored...), 442 )) 443 444 ignored := utils.NewSet(opt.ignored...) 445 setter := func(name string, fn func()) { 446 if !ignored.Contains(name) { 447 fn() 448 } 449 } 450 451 setter("Strp", func() { v.Strp = utils.AnyPtr(v.Str) }) 452 setter("StrpSlice", func() { v.StrpSlice = utils.SliceMapping(v.StrSlice, utils.AnyPtr[string]) }) 453 454 setter("Runep", func() { v.Runep = utils.AnyPtr(v.Rune) }) 455 setter("RunepSlice", func() { v.RunepSlice = utils.SliceMapping(v.RuneSlice, utils.AnyPtr[rune]) }) 456 457 setter("Bytep", func() { v.Bytep = utils.AnyPtr(v.Byte) }) 458 setter("BytepSlice", func() { v.BytepSlice = utils.SliceMapping(v.ByteSlice, utils.AnyPtr[byte]) }) 459 460 setter("Boolp", func() { v.Boolp = utils.AnyPtr(v.Bool) }) 461 462 setter("Intp", func() { v.Intp = utils.AnyPtr(v.Int) }) 463 setter("Int8p", func() { v.Int8p = utils.AnyPtr(v.Int8) }) 464 setter("Int16p", func() { v.Int16p = utils.AnyPtr(v.Int16) }) 465 setter("Int32p", func() { v.Int32p = utils.AnyPtr(v.Int32) }) 466 setter("Int64p", func() { v.Int64p = utils.AnyPtr(v.Int64) }) 467 468 setter("IntpSlice", func() { v.IntpSlice = utils.SliceMapping(v.IntSlice, utils.AnyPtr[int]) }) 469 setter("Int8pSlice", func() { v.Int8pSlice = utils.SliceMapping(v.Int8Slice, utils.AnyPtr[int8]) }) 470 setter("Int16pSlice", func() { v.Int16pSlice = utils.SliceMapping(v.Int16Slice, utils.AnyPtr[int16]) }) 471 setter("Int32pSlice", func() { v.Int32pSlice = utils.SliceMapping(v.Int32Slice, utils.AnyPtr[int32]) }) 472 setter("Int64pSlice", func() { v.Int64pSlice = utils.SliceMapping(v.Int64Slice, utils.AnyPtr[int64]) }) 473 474 setter("Uintp", func() { v.Uintp = utils.AnyPtr(v.Uint) }) 475 setter("Uint8p", func() { v.Uint8p = utils.AnyPtr(v.Uint8) }) 476 setter("Uint16p", func() { v.Uint16p = utils.AnyPtr(v.Uint16) }) 477 setter("Uint32p", func() { v.Uint32p = utils.AnyPtr(v.Uint32) }) 478 setter("Uint64p", func() { v.Uint64p = utils.AnyPtr(v.Uint64) }) 479 setter("UintpSlice", func() { v.UintpSlice = utils.SliceMapping(v.UintSlice, utils.AnyPtr[uint]) }) 480 setter("Uint8pSlice", func() { v.Uint8pSlice = utils.SliceMapping(v.Uint8Slice, utils.AnyPtr[uint8]) }) 481 setter("Uint16pSlice", func() { v.Uint16pSlice = utils.SliceMapping(v.Uint16Slice, utils.AnyPtr[uint16]) }) 482 setter("Uint32pSlice", func() { v.Uint32pSlice = utils.SliceMapping(v.Uint32Slice, utils.AnyPtr[uint32]) }) 483 setter("Uint64pSlice", func() { v.Uint64pSlice = utils.SliceMapping(v.Uint64Slice, utils.AnyPtr[uint64]) }) 484 485 setter("Float32p", func() { v.Float32p = utils.AnyPtr(v.Float32) }) 486 setter("Float64p", func() { v.Float64p = utils.AnyPtr(v.Float64) }) 487 setter("Float32pSlice", func() { v.Float32pSlice = utils.SliceMapping(v.Float32Slice, utils.AnyPtr[float32]) }) 488 setter("Float64pSlice", func() { v.Float64pSlice = utils.SliceMapping(v.Float64Slice, utils.AnyPtr[float64]) }) 489 490 setter("Complex64", func() { v.Complex64 = complex(1, 1) }) 491 setter("Complex64p", func() { v.Complex64p = utils.AnyPtr(v.Complex64) }) 492 setter("Complex128", func() { v.Complex128 = complex(1, 1) }) 493 setter("Complex128p", func() { v.Complex128p = utils.AnyPtr(v.Complex128) }) 494 setter("Complex64Slice", func() { 495 v.Complex64Slice = []complex64{complex(1, 1), complex(-1, 1), complex(1, -1), complex(-1, -1)} 496 }) 497 setter("Complex64Slice", func() { 498 v.Complex64pSlice = utils.SliceMapping(v.Complex64Slice, utils.AnyPtr[complex64]) 499 }) 500 501 setter("Complex128Slice", func() { 502 v.Complex128Slice = []complex128{complex(1, 1), complex(-1, 1), complex(1, -1), complex(-1, -1)} 503 }) 504 setter("Complex128pSlice", func() { 505 v.Complex128pSlice = utils.SliceMapping(v.Complex128Slice, utils.AnyPtr[complex128]) 506 }) 507 setter("Uintptr", func() { v.Uintptr = uintptr(unsafe.Pointer(&v)) }) 508 setter("Uintptrp", func() { v.Uintptrp = utils.AnyPtr(v.Uintptr) }) 509 setter("UintptrSlice", func() { 510 v.UintptrSlice = []uintptr{uintptr(unsafe.Pointer(&v)), 511 uintptr(unsafe.Pointer(&v.Int)), uintptr(unsafe.Pointer(&v.Uint))} 512 }) 513 setter("UintptrpSlice", func() { v.UintptrpSlice = utils.SliceMapping(v.UintptrSlice, utils.AnyPtr[uintptr]) }) 514 515 setter("Any", func() { v.Any = "any" }) 516 setter("Anyp", func() { v.Anyp = utils.AnyPtr(v.Any) }) 517 setter("AnySlice", func() { v.AnySlice = []any{1, 1.1, "any", true, complex(1, 1), nil} }) 518 setter("AnypSlice", func() { v.AnypSlice = utils.SliceMapping(v.AnySlice, utils.AnyPtr[any]) }) 519 520 setter("Timep", func() { v.Timep = utils.AnyPtr(v.Time) }) 521 522 return 523 } 524 525 func GenObj[T any](opts ...utils.OptionExtender) (v T) { 526 opt := utils.ApplyOptions[randomOption](opts...) 527 utils.MustSuccess(faker.FakeData(&v, 528 options.WithNilIfLenIsZero(true), 529 options.WithGenerateUniqueValues(true), 530 options.WithRandomIntegerBoundaries(interfaces.RandomIntegerBoundary{Start: 1, End: math.MaxInt8}), 531 options.WithRandomFloatBoundaries(interfaces.RandomFloatBoundary{Start: 1, End: math.MaxInt8}), 532 options.WithFieldsToIgnore(opt.ignored...), 533 )) 534 return 535 } 536 537 func GenObjList[T any](num int, opts ...utils.OptionExtender) (vList []T) { 538 vList = make([]T, 0, num) 539 for i := 0; i < num; i++ { 540 vList = append(vList, GenObj[T](opts...)) 541 } 542 return 543 }