From 2ed7817d37c7b6ba780ee5724953a19be44c2724 Mon Sep 17 00:00:00 2001 From: Shintaro Ikeda Date: Mon, 17 Feb 2020 21:15:28 +0900 Subject: [PATCH] chore: update examples (#88) --- CustomFaker.md | 22 ++++++++++--- SingleFakeData.md | 80 +++++++++++++++++++++++------------------------ WithStructTag.md | 40 +++++++++++++++++------- WithoutTag.md | 8 +++-- 4 files changed, 90 insertions(+), 60 deletions(-) diff --git a/CustomFaker.md b/CustomFaker.md index 8ea5267..91145c8 100644 --- a/CustomFaker.md +++ b/CustomFaker.md @@ -3,20 +3,32 @@ You can also add your own generator function to your own defined tags. See example below ```go +package main + +import ( + "fmt" + "reflect" + + "github.com/bxcodec/faker/v3" +) + +// Gondoruwo ... type Gondoruwo struct { Name string Locatadata int } +// Sample ... type Sample struct { - ID int64 `faker:"customIdFaker"` - Gondoruwo Gondoruwo `faker:"gondoruwo"` - Danger string `faker:"danger"` + ID int64 `faker:"customIdFaker"` + Gondoruwo Gondoruwo `faker:"gondoruwo"` + Danger string `faker:"danger"` } +// CustomGenerator ... func CustomGenerator() { faker.AddProvider("customIdFaker", func(v reflect.Value) (interface{}, error) { - return int64(43), nil + return int64(43), nil }) faker.AddProvider("danger", func(v reflect.Value) (interface{}, error) { return "danger-ranger", nil @@ -31,7 +43,7 @@ func CustomGenerator() { }) } -func main() { +func main() { CustomGenerator() var sample Sample faker.FakeData(&sample) diff --git a/SingleFakeData.md b/SingleFakeData.md index 90457de..5e1df70 100644 --- a/SingleFakeData.md +++ b/SingleFakeData.md @@ -5,8 +5,8 @@ ------ ```go -faker.Latitude() // => 81.12195 -faker.Longitude() // => -84.38158 +faker.Latitude() // => 81.12195 +faker.Longitude() // => -84.38158 ``` ### Datetime @@ -14,17 +14,17 @@ faker.Longitude() // => -84.38158 --- ```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 +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 @@ -32,14 +32,14 @@ faker.Timeperiod() // => PM --- ```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 +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 @@ -47,9 +47,9 @@ faker.Password() // => dfJdyHGuVkHBgnHLQQgpINApynzexnRpgIKBpiIjpTP --- ```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. +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 @@ -57,10 +57,10 @@ faker.Paragraph() // => Aut consequatur sit perferendis accusantium voluptatem --- ```go -faker.CCType() // => American Express -faker.CCNumber() // => 373641309057568 -faker.Currency() // => USD -faker.AmountWithCurrency() // => USD 49257.100 +faker.CCType() // => American Express +faker.CCNumber() // => 373641309057568 +faker.Currency() // => USD +faker.AmountWithCurrency() // => USD 49257.100 ``` ### Person @@ -68,13 +68,13 @@ faker.AmountWithCurrency() // => USD 49257.100 --- ```go -faker.TitleMale() // => Mr. -faker.TitleFemale() // => Mrs. -faker.FirstName() // => Whitney -faker.FirstNameMale() // => Kenny -faker.FirstNameFemale() // => Jana -faker.LastName() // => Rohan -faker.Name() // => Mrs. Casandra Kiehn +faker.TitleMale() // => Mr. +faker.TitleFemale() // => Mrs. +faker.FirstName() // => Whitney +faker.FirstNameMale() // => Kenny +faker.FirstNameFemale() // => Jana +faker.LastName() // => Rohan +faker.Name() // => Mrs. Casandra Kiehn ``` @@ -84,9 +84,9 @@ faker.Name() // => Mrs. Casandra Kiehn --- ```go -faker.Phonenumber() // -> 201-886-0269 +faker.Phonenumber() // -> 201-886-0269 faker.TollFreePhoneNumber() // => (777) 831-964572 -faker.E164PhoneNumber() // => +724891571063 +faker.E164PhoneNumber() // => +724891571063 ``` ### UUID @@ -94,8 +94,8 @@ faker.E164PhoneNumber() // => +724891571063 --- ```go -faker.UUIDHyphenated() // => 8f8e4463-9560-4a38-9b0c-ef24481e4e27 -faker.UUIDDigit() // => 90ea6479fd0e4940af741f0a87596b73 +faker.UUIDHyphenated() // => 8f8e4463-9560-4a38-9b0c-ef24481e4e27 +faker.UUIDDigit() // => 90ea6479fd0e4940af741f0a87596b73 ``` ### Unique values @@ -107,5 +107,5 @@ faker.SetGenerateUniqueValues(true) // Enable unique data generation on single f faker.Word() //... faker.SetGenerateUniqueValues(false) // Disable unique data generation on single fake data functions -faker.ResetUnique() // Forget all generated unique values +faker.ResetUnique() // Forget all generated unique values ``` diff --git a/WithStructTag.md b/WithStructTag.md index a89d661..283348a 100644 --- a/WithStructTag.md +++ b/WithStructTag.md @@ -68,13 +68,12 @@ Supported tag: * unique ```go - package main import ( "fmt" - "github.com/bxcodec/faker" + "github.com/bxcodec/faker/v3" ) // SomeStruct ... @@ -84,7 +83,7 @@ type SomeStruct struct { CreditCardNumber string `faker:"cc_number"` CreditCardType string `faker:"cc_type"` Email string `faker:"email"` - DomainName string `faker:"domain_name"` + DomainName string `faker:"domain_name"` IPV4 string `faker:"ipv4"` IPV6 string `faker:"ipv6"` Password string `faker:"password"` @@ -118,9 +117,9 @@ type SomeStruct struct { 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:"-"` + UUIDHypenated string `faker:"uuid_hyphenated"` + UUID string `faker:"uuid_digit"` + Skip string `faker:"-"` } func main() { @@ -179,7 +178,6 @@ func main() { } */ } - ``` ## Length And Boundary @@ -187,15 +185,17 @@ func main() { --- You can set length for your random strings also set boundary for your integers. + ```go package main import ( "fmt" - "github.com/bxcodec/faker" + "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"` @@ -215,8 +215,8 @@ type SomeStruct struct { 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... +func main() { + faker.SetRandomMapAndSliceSize(20) //Random generated map or array size wont exceed 20... a := SomeStruct{} err := faker.FakeData(&a) if err != nil { @@ -226,7 +226,9 @@ func main(){ } ``` + Result: + ``` { Inta:7 @@ -275,17 +277,25 @@ Result: 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 + for i := 0; i < 5; i++ { // Generate 5 structs having a unique word a := SomeStruct{} err := faker.FakeData(&a) if err != nil { @@ -295,4 +305,10 @@ func main() { } 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/WithoutTag.md b/WithoutTag.md index 9bc9b2e..14f9fa5 100644 --- a/WithoutTag.md +++ b/WithoutTag.md @@ -4,15 +4,15 @@ 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 import ( "fmt" - "github.com/bxcodec/faker" + "github.com/bxcodec/faker/v3" ) +// SomeStruct ... type SomeStruct struct { Int int Int8 int8 @@ -32,18 +32,20 @@ type SomeStruct struct { SBool []bool Struct AStruct } + +// AStruct ... type AStruct struct { Number int64 Height int64 AnotherStruct BStruct } +// BStruct ... type BStruct struct { Image string } func main() { - a := SomeStruct{} err := faker.FakeData(&a) if err != nil {