From 7198e34fe9a1ca4ccfddd1686a0ec5cf5d1cd9cb Mon Sep 17 00:00:00 2001 From: Ivan Boyarkin Date: Tue, 25 Feb 2020 05:22:59 +0100 Subject: [PATCH] chores(docs): Move example from markdown to tests (#89) It's required because: - package users can use godoc.org, pkg.go.dev for exploring the package documentation - testable examples can be more in sync with actual code This commit moves examples from the plain markdown to testable examples. It allows to test examples during test phase and have them more in sync with actual code. Also the examples can be shown by documentation servers and in IDEs and make work with package much more user-friendly as not everyone looks into README.md at repo's root. Related to #68 --- README.md | 11 +- SingleFakeData.md | 111 ------- WithStructTag.md | 314 ------------------ ...omFaker.md => example_custom_faker_test.go | 26 +- example_single_fake_data_test.go | 71 ++++ example_with_tags_lenbounds_test.go | 84 +++++ example_with_tags_test.go | 111 +++++++ example_with_tags_unique_test.go | 27 ++ WithoutTag.md => example_without_tag_test.go | 16 +- 9 files changed, 314 insertions(+), 457 deletions(-) delete mode 100644 SingleFakeData.md delete mode 100644 WithStructTag.md rename CustomFaker.md => example_custom_faker_test.go (55%) create mode 100644 example_single_fake_data_test.go create mode 100644 example_with_tags_lenbounds_test.go create mode 100644 example_with_tags_test.go create mode 100644 example_with_tags_unique_test.go rename WithoutTag.md => example_without_tag_test.go (89%) diff --git a/README.md b/README.md index 25bdb2c..d74d021 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,13 @@ go get -u github.com/bxcodec/faker/v3 --- - - Using Struct's tag: [WithStructTag.md](/WithStructTag.md) - - Custom Struct's tag (define your own faker data): [CustomFaker.md](/CustomFaker.md) - - Without struct's tag: [WithoutTag.md](/WithoutTag.md) - - Single Fake Data Function: [SingleFakeData.md](/SingleFakeData.md) + - Using Struct's tag: + - [basic tags: example_with_tags_test.go](/example_with_tags_test.go) + - [length and bounds: example_with_tags_lenbounds_test.go](/example_with_tags_lenbounds_test.go) + - [unique: example_with_tags_unique_test.go](example_with_tags_unique_test.go) + - Custom Struct's tag (define your own faker data): [example_custom_faker_test.go](/example_custom_faker_test.go) + - Without struct's tag: [example_without_tag_test.go](/example_without_tag_test.go) + - Single Fake Data Function: [example_single_fake_data_test.go](/example_single_fake_data_test.go) ## DEMO diff --git a/SingleFakeData.md b/SingleFakeData.md deleted file mode 100644 index 5e1df70..0000000 --- a/SingleFakeData.md +++ /dev/null @@ -1,111 +0,0 @@ -## Single Fake Function - -### Address - ------- - -```go -faker.Latitude() // => 81.12195 -faker.Longitude() // => -84.38158 -``` - -### Datetime - ---- - -```go -faker.Unixtime() // => 1197930901 -faker.Date() // => 1982-02-27 -faker.TimeString() // => 03:10:25 -faker.MonthName() // => February -faker.YearString() // => 1994 -faker.DayOfWeek() // => Sunday -faker.DayOfMonth() // => 20 -faker.Timestamp() // => 1973-06-21 14:50:46 -faker.Century() // => IV -faker.Timezone() // => Asia/Jakarta -faker.Timeperiod() // => PM -``` - -### Internet - ---- - -```go -faker.Email() // => mJBJtbv@OSAaT.com -faker.MacAddress() // => cd:65:e1:d4:76:c6 -faker.DomainName() // => FWZcaRE.org -faker.URL() // => https://www.oEuqqAY.org/QgqfOhd -faker.Username() // => lVxELHS -faker.IPv4() // => 99.23.42.63 -faker.IPv6() // => 975c:fb2c:2133:fbdd:beda:282e:1e0a:ec7d -faker.Password() // => dfJdyHGuVkHBgnHLQQgpINApynzexnRpgIKBpiIjpTP -``` - -### Words and Sentences - ---- - -```go -faker.Word() // => nesciunt -faker.Sentence() // => Consequatur perferendis voluptatem accusantium. -faker.Paragraph() // => Aut consequatur sit perferendis accusantium voluptatem. Accusantium perferendis consequatur voluptatem sit aut. Aut sit accusantium consequatur voluptatem perferendis. Perferendis voluptatem aut accusantium consequatur sit. -``` - -### Payment - ---- - -```go -faker.CCType() // => American Express -faker.CCNumber() // => 373641309057568 -faker.Currency() // => USD -faker.AmountWithCurrency() // => USD 49257.100 -``` - -### Person - ---- - -```go -faker.TitleMale() // => Mr. -faker.TitleFemale() // => Mrs. -faker.FirstName() // => Whitney -faker.FirstNameMale() // => Kenny -faker.FirstNameFemale() // => Jana -faker.LastName() // => Rohan -faker.Name() // => Mrs. Casandra Kiehn -``` - - - -### Phone - ---- - -```go -faker.Phonenumber() // -> 201-886-0269 -faker.TollFreePhoneNumber() // => (777) 831-964572 -faker.E164PhoneNumber() // => +724891571063 -``` - -### UUID - ---- - -```go -faker.UUIDHyphenated() // => 8f8e4463-9560-4a38-9b0c-ef24481e4e27 -faker.UUIDDigit() // => 90ea6479fd0e4940af741f0a87596b73 -``` - -### Unique values - ---- - -```go -faker.SetGenerateUniqueValues(true) // Enable unique data generation on single fake data functions -faker.Word() -//... -faker.SetGenerateUniqueValues(false) // Disable unique data generation on single fake data functions -faker.ResetUnique() // Forget all generated unique values -``` diff --git a/WithStructTag.md b/WithStructTag.md deleted file mode 100644 index 283348a..0000000 --- a/WithStructTag.md +++ /dev/null @@ -1,314 +0,0 @@ -## With Tag - -Supported tag: - -**Internet :** - -* Email -* Mac address -* Domain name -* URL -* UserName -* IP Address (IPv4, IPv6 ) -* Password - - -**Payment :** -* Credit Card Type (VISA, MASTERCARD, AMERICAN EXPRESS, DISCOVER, JCB, DINERS CLUB) -* Credit Card Number - -**Address :** -* Latitude and Longitude - -**Phone :** -* Phone number -* Toll free phone number -* E164PhoneNumber - -**Person :** -* Title male -* Title female -* FirstName -* FirstName male -* FirstName female -* LastName -* Name - -**DateTime :** -* UnixTime -* Date -* Time -* MonthName -* Year -* DayOfWeek -* DayOfMonth -* Timestamp -* Century -* TimeZone -* TimePeriod - -**Lorem :** -* Word -* Sentence -* Paragraph - -**Price :** -* Currency -* Amount -* Amount with Currency - -**UUID :** -* UUID Digit (32 bytes) -* UUID Hyphenated (36 bytes) - -**Skip :** -* \- - -**Unique :** -* unique - -```go -package main - -import ( - "fmt" - - "github.com/bxcodec/faker/v3" -) - -// SomeStruct ... -type SomeStruct struct { - Latitude float32 `faker:"lat"` - Longitude float32 `faker:"long"` - CreditCardNumber string `faker:"cc_number"` - CreditCardType string `faker:"cc_type"` - Email string `faker:"email"` - DomainName string `faker:"domain_name"` - IPV4 string `faker:"ipv4"` - IPV6 string `faker:"ipv6"` - Password string `faker:"password"` - PhoneNumber string `faker:"phone_number"` - MacAddress string `faker:"mac_address"` - URL string `faker:"url"` - UserName string `faker:"username"` - TollFreeNumber string `faker:"toll_free_number"` - E164PhoneNumber string `faker:"e_164_phone_number"` - TitleMale string `faker:"title_male"` - TitleFemale string `faker:"title_female"` - FirstName string `faker:"first_name"` - FirstNameMale string `faker:"first_name_male"` - FirstNameFemale string `faker:"first_name_female"` - LastName string `faker:"last_name"` - Name string `faker:"name"` - UnixTime int64 `faker:"unix_time"` - Date string `faker:"date"` - Time string `faker:"time"` - MonthName string `faker:"month_name"` - Year string `faker:"year"` - DayOfWeek string `faker:"day_of_week"` - DayOfMonth string `faker:"day_of_month"` - Timestamp string `faker:"timestamp"` - Century string `faker:"century"` - TimeZone string `faker:"timezone"` - TimePeriod string `faker:"time_period"` - Word string `faker:"word"` - Sentence string `faker:"sentence"` - Paragraph string `faker:"paragraph"` - Currency string `faker:"currency"` - Amount float64 `faker:"amount"` - AmountWithCurrency string `faker:"amount_with_currency"` - UUIDHypenated string `faker:"uuid_hyphenated"` - UUID string `faker:"uuid_digit"` - Skip string `faker:"-"` -} - -func main() { - - a := SomeStruct{} - err := faker.FakeData(&a) - if err != nil { - fmt.Println(err) - } - fmt.Printf("%+v", a) - /* - Output : - { - Latitude: 81.12195 - Longitude: -84.38158 - CreditCardType: American Express - CreditCardNumber: 373641309057568 - Email: mJBJtbv@OSAaT.ru - DomainName: FWZcaRE.ru, - IPV4: 99.23.42.63 - IPV6: 975c:fb2c:2133:fbdd:beda:282e:1e0a:ec7d - Password: dfJdyHGuVkHBgnHLQQgpINApynzexnRpgIKBpiIjpTPOmNyMFb - PhoneNumber: 792-153-4861 - MacAddress: cd:65:e1:d4:76:c6 - URL: https://www.oEuqqAY.org/QgqfOhd - UserName: lVxELHS - TollFreeNumber: (777) 831-964572 - E164PhoneNumber: +724891571063 - TitleMale: Mr. - TitleFemale: Queen - FirstName: Whitney - FirstNameMale: Kenny - FirstNameFemale: Jana - LastName: Rohan - Name: Miss Casandra Kiehn - UnixTime: 1197930901 - Date: 1982-02-27 - Time: 03:10:25 - MonthName: February - Year: 1996 - DayOfWeek: Sunday - DayOfMonth: 20 - Timestamp: 1973-06-21 14:50:46 - Century: IV - TimeZone: Canada/Eastern - TimePeriod: AM - Word: nesciunt - Sentence: Consequatur perferendis aut sit voluptatem accusantium. - Paragraph: Aut consequatur sit perferendis accusantium voluptatem. Accusantium perferendis consequatur voluptatem sit aut. Aut sit accusantium consequatur voluptatem perferendis. Perferendis voluptatem aut accusantium consequatur sit. - Currency: IRR, - Amount: 88.990000, - AmountWithCurrency: XBB 49257.100000, - UUIDHypenated: 8f8e4463-9560-4a38-9b0c-ef24481e4e27, - UUID: 90ea6479fd0e4940af741f0a87596b73, - Skip: - } - */ -} -``` - -## Length And Boundary - ---- - -You can set length for your random strings also set boundary for your integers. - -```go -package main - -import ( - "fmt" - - "github.com/bxcodec/faker/v3" -) - -// SomeStruct ... -type SomeStruct struct { - Inta int `faker:"boundary_start=5, boundary_end=10"` - Int8 int8 `faker:"boundary_start=100, boundary_end=1000"` - Int16 int16 `faker:"boundary_start=123, boundary_end=1123"` - Int32 int32 `faker:"boundary_start=-10, boundary_end=8123"` - Int64 int64 `faker:"boundary_start=31, boundary_end=88"` - - UInta uint `faker:"boundary_start=35, boundary_end=152"` - UInt8 uint8 `faker:"boundary_start=5, boundary_end=1425"` - UInt16 uint16 `faker:"boundary_start=245, boundary_end=2125"` - UInt32 uint32 `faker:"boundary_start=0, boundary_end=40"` - UInt64 uint64 `faker:"boundary_start=14, boundary_end=50"` - - ASString []string `faker:"len=50"` - SString string `faker:"len=25"` - MSString map[string]string `faker:"len=30"` - MIint map[int]int `faker:"boundary_start=5, boundary_end=10"` -} - -func main() { - faker.SetRandomMapAndSliceSize(20) //Random generated map or array size wont exceed 20... - a := SomeStruct{} - err := faker.FakeData(&a) - if err != nil { - fmt.Println(err) - } - fmt.Printf("%+v", a) -} - -``` - -Result: - -``` -{ - Inta:7 - Int8:-102 - Int16:556 - Int32:113 - Int64:70 - UInta:78 - UInt8:54 - UInt16:1797 - UInt32:8 - UInt64:34 - ASString:[ - geHYIpEoQhQdijFooVEAOyvtTwJOofbQPJdbHvEEdjueZaKIgI - WVJBBtmrrVccyIydAiLSkMwWbFzFMEotEXsyUXqcmBTVORlkJK - xYiRTRSZRuGDcMWYoPALVMZgIXoTQtmdGXQfbISKJiavLspuBV - qsoiYlyRbXLDAMoIdQhgMriODYWCTEYepmjaldWLLjkulDGuQN - GQXUlqNkVjPKodMebPIeoZZlxfhbQJOjHSRjUTrcgBFPeDZIxn - MEeRkkLceDsqKLEJFEjJxHtYrYxQMxYcuRHEDGYSPbELDQLSsj - tWUIACjQWeiUhbboGuuEQIhUJCRSBzVImpYwOlFbsjCRmxboZW - ZDaAUZEgFKbMJoKIMpymTreeZGWLXNCfVzaEyWNdbkaZOcsfst - uwlsZBMlEknIBsALpXRaplZWVtXTKzsWglRVBpmfsQfqraiEYA - AXszbzsOzYPYeXHXRwoPmoPoBxopdFFvWMBTPCxESTepRpjlnB - kTuOPHlUrSzUQRmZMYplWbyoBbWzQYCiydyzurOduhjuyiGrCE - FZbeLMbelIeCMnixknIARZRbwALObGXADQqianJbkiEAqqpdnK - TiQrZbnkvxEciyKXlliUDOGVdpMoAsHSalFbLcYyXxNFLAhqjy - KlbjbloxkWKSqvUfJQPpFLoddWgeABfYUoaAnylKmEHwxgNsnO - ] - SString:VVcaPSFrOPYlEkpVyTRbSZneB - MSString:map[ - ueFBFTTmqDwrXDoXAYTRhQRmLXhudA:AhQewvZfrlytbAROzGjpXUmNQzIoGl - fZwrCsFfZwqMsDJXOUYIacflFIeyFU:VMufFCRRHTtuFthOrRAMbzbKVJHnvJ - rHDQTyZqZVSPLwTtZfNSwKWrgmRghL:lRSXNHkhUyjDuBgoAfrQwOcHYilqRB - BvCpQJMHzKXKbOoAnTXkLCNxKshwWr:tiNFrXAXUtdywkyygWBrEVrmAcAepD - uWWKgHKTkUgAZiopAIUmgVWrkrceVy:GuuDNTUiaBtOKwWrMoZDiyaOPxywnq - HohMjOdMDkAqimKPTgdjUorydpKkly:whAjmraukcZczskqycoJELlMJTghca - umEgMBGUvBptdKImKsoWXMGJJoRbgT:tPpgHgLEyHmDOocOiSgTbXQHVduLxP - SRQLHjBXCXKvbLIktdKeLwMnIFOmbi:IJBpLyTcraOxOUtwSKTisjElpulkTL - dbnDeJZLqMXQGjbTSNxPSlfDHGCghU:JWrymovFwNWbIQBxPpQmlgJsgpXcui - roraKNGnBXnrJlsxTnFgxHyZeTXdAC:XIcLWqUAQAbfkRrgfjrTVxZCvRJXyl - TrvxqVVjXAboYDPvUglSJQrltPjzLx:nBhWdfNPybnNnCyQlSshWKOnwUMQzL - dTHhWJWMwfVvKpIKTFCaoBJgKmnfbD:ixjNHsvSkRkFiNLpgUzIKPsheqhCeY - lWyBrtfcGWiNbSTJZJXwOPvVngZZMk:kvlYeGgwguVtiafGKjHWsYWewbaXte - bigsYNfVcNMGtnzgaqEjeRRlIcUdbR:hYOnJupEOvblTTEYzZYPuTVmvTmiit - ] - MIint:map[7:7 5:7 8:8 9:5 6:5] -} -``` - -## Unique values - -```go -package main - -import ( - "fmt" - - "github.com/bxcodec/faker/v3" -) - -// SomeStruct ... -type SomeStruct struct { - Word string `faker:"word,unique"` -} - -func main() { - for i := 0; i < 5; i++ { // Generate 5 structs having a unique word - a := SomeStruct{} - err := faker.FakeData(&a) - if err != nil { - fmt.Println(err) - } - fmt.Printf("%+v", a) - } - faker.ResetUnique() // Forget all generated unique values. Allows to start generating another unrelated dataset. -} -``` - -Result: - -``` -{Word:nobis}{Word:recusandae}{Word:praesentium}{Word:doloremque}{Word:non} -``` \ No newline at end of file diff --git a/CustomFaker.md b/example_custom_faker_test.go similarity index 55% rename from CustomFaker.md rename to example_custom_faker_test.go index 91145c8..ee56e72 100644 --- a/CustomFaker.md +++ b/example_custom_faker_test.go @@ -1,9 +1,4 @@ -## Custom Generator Provider - -You can also add your own generator function to your own defined tags. See example below - -```go -package main +package faker_test import ( "fmt" @@ -27,14 +22,14 @@ type Sample struct { // CustomGenerator ... func CustomGenerator() { - faker.AddProvider("customIdFaker", func(v reflect.Value) (interface{}, error) { + _ = faker.AddProvider("customIdFaker", func(v reflect.Value) (interface{}, error) { return int64(43), nil }) - faker.AddProvider("danger", func(v reflect.Value) (interface{}, error) { + _ = faker.AddProvider("danger", func(v reflect.Value) (interface{}, error) { return "danger-ranger", nil }) - faker.AddProvider("gondoruwo", func(v reflect.Value) (interface{}, error) { + _ = faker.AddProvider("gondoruwo", func(v reflect.Value) (interface{}, error) { obj := Gondoruwo{ Name: "Power", Locatadata: 324, @@ -43,15 +38,12 @@ func CustomGenerator() { }) } -func main() { +// You can also add your own generator function to your own defined tags. +func Example_customFaker() { CustomGenerator() var sample Sample - faker.FakeData(&sample) + _ = faker.FakeData(&sample) fmt.Printf("%+v", sample) + // Output: + // {ID:43 Gondoruwo:{Name:Power Locatadata:324} Danger:danger-ranger} } -``` - -Results: -``` -{ID:43 Gondoruwo:{Name:Power Locatadata:324} Danger:danger-ranger} -``` diff --git a/example_single_fake_data_test.go b/example_single_fake_data_test.go new file mode 100644 index 0000000..3e3b22b --- /dev/null +++ b/example_single_fake_data_test.go @@ -0,0 +1,71 @@ +package faker_test + +import "github.com/bxcodec/faker/v3" + +// Single fake function can be used for retrieving particular values. +func Example_singleFakeData() { + + // Address + faker.Latitude() // => 81.12195 + faker.Longitude() // => -84.38158 + + // Datetime + faker.UnixTime() // => 1197930901 + faker.Date() // => 1982-02-27 + faker.TimeString() // => 03:10:25 + faker.MonthName() // => February + faker.YearString() // => 1994 + faker.DayOfWeek() // => Sunday + faker.DayOfMonth() // => 20 + faker.Timestamp() // => 1973-06-21 14:50:46 + faker.Century() // => IV + faker.Timezone() // => Asia/Jakarta + faker.Timeperiod() // => PM + + // Internet + faker.Email() // => mJBJtbv@OSAaT.com + faker.MacAddress() // => cd:65:e1:d4:76:c6 + faker.DomainName() // => FWZcaRE.org + faker.URL() // => https://www.oEuqqAY.org/QgqfOhd + faker.Username() // => lVxELHS + faker.IPv4() // => 99.23.42.63 + faker.IPv6() // => 975c:fb2c:2133:fbdd:beda:282e:1e0a:ec7d + faker.Password() // => dfJdyHGuVkHBgnHLQQgpINApynzexnRpgIKBpiIjpTP + + // Words and Sentences + faker.Word() // => nesciunt + faker.Sentence() // => Consequatur perferendis voluptatem accusantium. + faker.Paragraph() // => Aut consequatur sit perferendis accusantium voluptatem. Accusantium perferendis consequatur voluptatem sit aut. Aut sit accusantium consequatur voluptatem perferendis. Perferendis voluptatem aut accusantium consequatur sit. + + // Payment + faker.CCType() // => American Express + faker.CCNumber() // => 373641309057568 + faker.Currency() // => USD + faker.AmountWithCurrency() // => USD 49257.100 + + // Person + faker.TitleMale() // => Mr. + faker.TitleFemale() // => Mrs. + faker.FirstName() // => Whitney + faker.FirstNameMale() // => Kenny + faker.FirstNameFemale() // => Jana + faker.LastName() // => Rohan + faker.Name() // => Mrs. Casandra Kiehn + + // Phone + faker.Phonenumber() // -> 201-886-0269 + faker.TollFreePhoneNumber() // => (777) 831-964572 + faker.E164PhoneNumber() // => +724891571063 + + // UUID + faker.UUIDHyphenated() // => 8f8e4463-9560-4a38-9b0c-ef24481e4e27 + faker.UUIDDigit() // => 90ea6479fd0e4940af741f0a87596b73 + + // Unique values + faker.SetGenerateUniqueValues(true) // Enable unique data generation on single fake data functions + faker.Word() + // ... + faker.SetGenerateUniqueValues(false) // Disable unique data generation on single fake data functions + faker.ResetUnique() // Forget all generated unique values + +} diff --git a/example_with_tags_lenbounds_test.go b/example_with_tags_lenbounds_test.go new file mode 100644 index 0000000..8ada652 --- /dev/null +++ b/example_with_tags_lenbounds_test.go @@ -0,0 +1,84 @@ +package faker_test + +import ( + "fmt" + + "github.com/bxcodec/faker/v3" +) + +// You can set length for your random strings also set boundary for your integers. +func Example_withTagsLengthAndBoundary() { + // SomeStruct ... + type SomeStruct struct { + Inta int `faker:"boundary_start=5, boundary_end=10"` + Int8 int8 `faker:"boundary_start=100, boundary_end=1000"` + Int16 int16 `faker:"boundary_start=123, boundary_end=1123"` + Int32 int32 `faker:"boundary_start=-10, boundary_end=8123"` + Int64 int64 `faker:"boundary_start=31, boundary_end=88"` + + UInta uint `faker:"boundary_start=35, boundary_end=152"` + UInt8 uint8 `faker:"boundary_start=5, boundary_end=1425"` + UInt16 uint16 `faker:"boundary_start=245, boundary_end=2125"` + UInt32 uint32 `faker:"boundary_start=0, boundary_end=40"` + UInt64 uint64 `faker:"boundary_start=14, boundary_end=50"` + + ASString []string `faker:"len=50"` + SString string `faker:"len=25"` + MSString map[string]string `faker:"len=30"` + MIint map[int]int `faker:"boundary_start=5, boundary_end=10"` + } + + _ = faker.SetRandomMapAndSliceSize(20) // Random generated map or array size wont exceed 20... + a := SomeStruct{} + _ = faker.FakeData(&a) + fmt.Printf("%+v", a) + // Result: + /* + { + Inta:7 + Int8:-102 + Int16:556 + Int32:113 + Int64:70 + UInta:78 + UInt8:54 + UInt16:1797 + UInt32:8 + UInt64:34 + ASString:[ + geHYIpEoQhQdijFooVEAOyvtTwJOofbQPJdbHvEEdjueZaKIgI + WVJBBtmrrVccyIydAiLSkMwWbFzFMEotEXsyUXqcmBTVORlkJK + xYiRTRSZRuGDcMWYoPALVMZgIXoTQtmdGXQfbISKJiavLspuBV + qsoiYlyRbXLDAMoIdQhgMriODYWCTEYepmjaldWLLjkulDGuQN + GQXUlqNkVjPKodMebPIeoZZlxfhbQJOjHSRjUTrcgBFPeDZIxn + MEeRkkLceDsqKLEJFEjJxHtYrYxQMxYcuRHEDGYSPbELDQLSsj + tWUIACjQWeiUhbboGuuEQIhUJCRSBzVImpYwOlFbsjCRmxboZW + ZDaAUZEgFKbMJoKIMpymTreeZGWLXNCfVzaEyWNdbkaZOcsfst + uwlsZBMlEknIBsALpXRaplZWVtXTKzsWglRVBpmfsQfqraiEYA + AXszbzsOzYPYeXHXRwoPmoPoBxopdFFvWMBTPCxESTepRpjlnB + kTuOPHlUrSzUQRmZMYplWbyoBbWzQYCiydyzurOduhjuyiGrCE + FZbeLMbelIeCMnixknIARZRbwALObGXADQqianJbkiEAqqpdnK + TiQrZbnkvxEciyKXlliUDOGVdpMoAsHSalFbLcYyXxNFLAhqjy + KlbjbloxkWKSqvUfJQPpFLoddWgeABfYUoaAnylKmEHwxgNsnO + ] + SString:VVcaPSFrOPYlEkpVyTRbSZneB + MSString:map[ + ueFBFTTmqDwrXDoXAYTRhQRmLXhudA:AhQewvZfrlytbAROzGjpXUmNQzIoGl + fZwrCsFfZwqMsDJXOUYIacflFIeyFU:VMufFCRRHTtuFthOrRAMbzbKVJHnvJ + rHDQTyZqZVSPLwTtZfNSwKWrgmRghL:lRSXNHkhUyjDuBgoAfrQwOcHYilqRB + BvCpQJMHzKXKbOoAnTXkLCNxKshwWr:tiNFrXAXUtdywkyygWBrEVrmAcAepD + uWWKgHKTkUgAZiopAIUmgVWrkrceVy:GuuDNTUiaBtOKwWrMoZDiyaOPxywnq + HohMjOdMDkAqimKPTgdjUorydpKkly:whAjmraukcZczskqycoJELlMJTghca + umEgMBGUvBptdKImKsoWXMGJJoRbgT:tPpgHgLEyHmDOocOiSgTbXQHVduLxP + SRQLHjBXCXKvbLIktdKeLwMnIFOmbi:IJBpLyTcraOxOUtwSKTisjElpulkTL + dbnDeJZLqMXQGjbTSNxPSlfDHGCghU:JWrymovFwNWbIQBxPpQmlgJsgpXcui + roraKNGnBXnrJlsxTnFgxHyZeTXdAC:XIcLWqUAQAbfkRrgfjrTVxZCvRJXyl + TrvxqVVjXAboYDPvUglSJQrltPjzLx:nBhWdfNPybnNnCyQlSshWKOnwUMQzL + dTHhWJWMwfVvKpIKTFCaoBJgKmnfbD:ixjNHsvSkRkFiNLpgUzIKPsheqhCeY + lWyBrtfcGWiNbSTJZJXwOPvVngZZMk:kvlYeGgwguVtiafGKjHWsYWewbaXte + bigsYNfVcNMGtnzgaqEjeRRlIcUdbR:hYOnJupEOvblTTEYzZYPuTVmvTmiit + ] + MIint:map[7:7 5:7 8:8 9:5 6:5] + } + */ +} diff --git a/example_with_tags_test.go b/example_with_tags_test.go new file mode 100644 index 0000000..4dd08a0 --- /dev/null +++ b/example_with_tags_test.go @@ -0,0 +1,111 @@ +package faker_test + +import ( + "fmt" + + "github.com/bxcodec/faker/v3" +) + +// SomeStructWithTags ... +type SomeStructWithTags struct { + Latitude float32 `faker:"lat"` + Longitude float32 `faker:"long"` + CreditCardNumber string `faker:"cc_number"` + CreditCardType string `faker:"cc_type"` + Email string `faker:"email"` + DomainName string `faker:"domain_name"` + IPV4 string `faker:"ipv4"` + IPV6 string `faker:"ipv6"` + Password string `faker:"password"` + PhoneNumber string `faker:"phone_number"` + MacAddress string `faker:"mac_address"` + URL string `faker:"url"` + UserName string `faker:"username"` + TollFreeNumber string `faker:"toll_free_number"` + E164PhoneNumber string `faker:"e_164_phone_number"` + TitleMale string `faker:"title_male"` + TitleFemale string `faker:"title_female"` + FirstName string `faker:"first_name"` + FirstNameMale string `faker:"first_name_male"` + FirstNameFemale string `faker:"first_name_female"` + LastName string `faker:"last_name"` + Name string `faker:"name"` + UnixTime int64 `faker:"unix_time"` + Date string `faker:"date"` + Time string `faker:"time"` + MonthName string `faker:"month_name"` + Year string `faker:"year"` + DayOfWeek string `faker:"day_of_week"` + DayOfMonth string `faker:"day_of_month"` + Timestamp string `faker:"timestamp"` + Century string `faker:"century"` + TimeZone string `faker:"timezone"` + TimePeriod string `faker:"time_period"` + Word string `faker:"word"` + Sentence string `faker:"sentence"` + Paragraph string `faker:"paragraph"` + Currency string `faker:"currency"` + Amount float64 `faker:"amount"` + AmountWithCurrency string `faker:"amount_with_currency"` + UUIDHypenated string `faker:"uuid_hyphenated"` + UUID string `faker:"uuid_digit"` + Skip string `faker:"-"` +} + +func Example_withTags() { + + a := SomeStructWithTags{} + err := faker.FakeData(&a) + if err != nil { + fmt.Println(err) + } + fmt.Printf("%+v", a) + /* + Result: + { + Latitude: 81.12195 + Longitude: -84.38158 + CreditCardType: American Express + CreditCardNumber: 373641309057568 + Email: mJBJtbv@OSAaT.ru + DomainName: FWZcaRE.ru, + IPV4: 99.23.42.63 + IPV6: 975c:fb2c:2133:fbdd:beda:282e:1e0a:ec7d + Password: dfJdyHGuVkHBgnHLQQgpINApynzexnRpgIKBpiIjpTPOmNyMFb + PhoneNumber: 792-153-4861 + MacAddress: cd:65:e1:d4:76:c6 + URL: https://www.oEuqqAY.org/QgqfOhd + UserName: lVxELHS + TollFreeNumber: (777) 831-964572 + E164PhoneNumber: +724891571063 + TitleMale: Mr. + TitleFemale: Queen + FirstName: Whitney + FirstNameMale: Kenny + FirstNameFemale: Jana + LastName: Rohan + Name: Miss Casandra Kiehn + UnixTime: 1197930901 + Date: 1982-02-27 + Time: 03:10:25 + MonthName: February + Year: 1996 + DayOfWeek: Sunday + DayOfMonth: 20 + Timestamp: 1973-06-21 14:50:46 + Century: IV + TimeZone: Canada/Eastern + TimePeriod: AM + Word: nesciunt + Sentence: Consequatur perferendis aut sit voluptatem accusantium. + Paragraph: Aut consequatur sit perferendis accusantium voluptatem. Accusantium perferendis consequatur voluptatem sit aut. Aut sit accusantium consequatur voluptatem perferendis. Perferendis voluptatem aut accusantium consequatur sit. + Currency: IRR, + Amount: 88.990000, + AmountWithCurrency: XBB 49257.100000, + UUIDHypenated: 8f8e4463-9560-4a38-9b0c-ef24481e4e27, + UUID: 90ea6479fd0e4940af741f0a87596b73, + Skip: + } + */ + +} diff --git a/example_with_tags_unique_test.go b/example_with_tags_unique_test.go new file mode 100644 index 0000000..200f951 --- /dev/null +++ b/example_with_tags_unique_test.go @@ -0,0 +1,27 @@ +package faker_test + +import ( + "fmt" + + "github.com/bxcodec/faker/v3" +) + +// SomeStructWithUnique ... +type SomeStructWithUnique struct { + Word string `faker:"word,unique"` +} + +func Example_withTagsAndUnique() { + for i := 0; i < 5; i++ { // Generate 5 structs having a unique word + a := SomeStructWithUnique{} + err := faker.FakeData(&a) + if err != nil { + fmt.Println(err) + } + fmt.Printf("%+v", a) + } + faker.ResetUnique() // Forget all generated unique values. Allows to start generating another unrelated dataset. + + // Result: + // {Word:nobis}{Word:recusandae}{Word:praesentium}{Word:doloremque}{Word:non} +} diff --git a/WithoutTag.md b/example_without_tag_test.go similarity index 89% rename from WithoutTag.md rename to example_without_tag_test.go index 14f9fa5..2d7f0f8 100644 --- a/WithoutTag.md +++ b/example_without_tag_test.go @@ -1,10 +1,4 @@ -## Without Tag - - -You also can use faker to generate your structs data randomly without any tag. And it will fill the data based on its data-type. - -```go -package main +package faker_test import ( "fmt" @@ -45,7 +39,9 @@ type BStruct struct { Image string } -func main() { +// You also can use faker to generate your structs data randomly without any tag. +// And it will fill the data based on its data-type. +func Example_withoutTag() { a := SomeStruct{} err := faker.FakeData(&a) if err != nil { @@ -53,7 +49,7 @@ func main() { } fmt.Printf("%+v", a) /* - Output: + Result: { Int:5231564546548329 Int8:52 @@ -81,5 +77,3 @@ func main() { } */ } - -```