From 20395f3257c96f5ff1ce66e2b9b6996ed21a8585 Mon Sep 17 00:00:00 2001 From: Rubem Mota Date: Tue, 12 Dec 2023 20:44:28 +0000 Subject: [PATCH 1/3] feat(blood): Added faker for blood types Added faker that will generate blood type, rh factor and group Signed-off-by: Rubem Mota --- blood.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ blood_test.go | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 blood.go create mode 100644 blood_test.go diff --git a/blood.go b/blood.go new file mode 100644 index 0000000..feb80ec --- /dev/null +++ b/blood.go @@ -0,0 +1,54 @@ +package faker + +import ( + "fmt" + "reflect" + + "github.com/go-faker/faker/v4/pkg/options" +) + +var bloodTypes = []string{"O", "A", "B", "AB"} +var bloodRhFactors = []string{"+", "-"} + +func GetBlood(opts ...options.OptionFunc) Blooder { + opt := options.BuildOptions(opts) + db := &Blood{ + fakerOption: *opt, + } + return db +} + +type Blooder interface { + BloodType(v reflect.Value) (interface{}, error) + BloodRHFactor(v reflect.Value) (interface{}, error) + BloodGroup(v reflect.Value) (interface{}, error) +} + +// Internet struct +type Blood struct { + fakerOption options.Options +} + +func (b Blood) bloodType() string { + return randomElementFromSliceString(bloodTypes) +} + +func (b Blood) BloodType(v reflect.Value) (interface{}, error) { + return b.bloodType(), nil +} + +func (b Blood) bloodRhFactor() string { + return randomElementFromSliceString(bloodRhFactors) +} + +func (b Blood) BloodRHFactor(v reflect.Value) (interface{}, error) { + return b.bloodRhFactor(), nil +} + +func (b Blood) bloodGroup() string { + return fmt.Sprintf("%s%s", b.bloodType(), b.bloodRhFactor()) +} + +func (b Blood) BloodGroup(v reflect.Value) (interface{}, error) { + return b.bloodGroup(), nil +} diff --git a/blood_test.go b/blood_test.go new file mode 100644 index 0000000..be5c4bf --- /dev/null +++ b/blood_test.go @@ -0,0 +1,41 @@ +package faker + +import ( + "reflect" + "strings" + "testing" + + "github.com/go-faker/faker/v4/pkg/slice" +) + +func TestBloodType(t *testing.T) { + bloodType, err := GetBlood().BloodType(reflect.Value{}) + if err != nil { + t.Error("Expected not error, got err", err) + } + if !slice.Contains(bloodTypes, bloodType.(string)) { + t.Error("Expected value from variable bloodType in function BloodType") + } +} + +func TestBloodRhFactor(t *testing.T) { + bloodRhFactor, err := GetBlood().BloodRHFactor(reflect.Value{}) + if err != nil { + t.Error("Expected not error, got err", err) + } + if !slice.Contains(bloodRhFactors, bloodRhFactor.(string)) { + t.Error("Expected value from variable bloodRhFactor in function BloodType") + } +} + +func TestBloodGroup(t *testing.T) { + bloodTypes = []string{"O"} + bloodRhFactors = []string{"+"} + bloodGroup, err := GetBlood().BloodGroup(reflect.Value{}) + if err != nil { + t.Error("Expected not error, got err", err) + } + if !strings.Contains(bloodGroup.(string), "O+") { + t.Error("Expected get url") + } +} From 51279e52f168bb75d8bbeee9ca67cda3869d463d Mon Sep 17 00:00:00 2001 From: Rubem Date: Wed, 7 Feb 2024 18:38:37 +0000 Subject: [PATCH 2/3] feat(blood): Added tag into main tag Signed-off-by: Rubem --- faker.go | 5 +++- faker_test.go | 66 +++++++++++++++++++++++++-------------------------- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/faker.go b/faker.go index d7a2a18..56fe655 100644 --- a/faker.go +++ b/faker.go @@ -94,6 +94,7 @@ const ( RussianLastNameMaleTag = "russian_last_name_male" RussianFirstNameFemaleTag = "russian_first_name_female" RussianLastNameFemaleTag = "russian_last_name_female" + BloodTypeTag = "blood_type" ) // PriorityTags define the priority order of the tag @@ -102,7 +103,7 @@ var PriorityTags = []string{ID, HyphenatedID, EmailTag, MacAddressTag, DomainNam E164PhoneNumberTag, TitleMaleTag, TitleFemaleTag, FirstNameTag, FirstNameMaleTag, FirstNameFemaleTag, LastNameTag, NAME, ChineseFirstNameTag, ChineseLastNameTag, ChineseNameTag, GENDER, UnixTimeTag, DATE, TIME, MonthNameTag, YEAR, DayOfWeekTag, DayOfMonthTag, TIMESTAMP, CENTURY, TIMEZONE, TimePeriodTag, WORD, SENTENCE, PARAGRAPH, - CurrencyTag, AmountTag, AmountWithCurrencyTag, SKIP, Length, SliceLength, Language, BoundaryStart, BoundaryEnd, ONEOF, + CurrencyTag, AmountTag, AmountWithCurrencyTag, SKIP, Length, SliceLength, Language, BoundaryStart, BoundaryEnd, ONEOF, BloodTypeTag, } type mapperTagCustom struct { @@ -132,6 +133,7 @@ func (m *mapperTagCustom) Store(key string, taggedFunc interfaces.TaggedFunction var defaultTag = sync.Map{} func initDefaultTag() { + defaultTag.Store(BloodTypeTag, BloodTypeTag) defaultTag.Store(EmailTag, EmailTag) defaultTag.Store(MacAddressTag, MacAddressTag) defaultTag.Store(DomainNameTag, DomainNameTag) @@ -284,6 +286,7 @@ func initMapperTagWithOption(opts ...options.OptionFunc) { mapperTag.Store(IPV6Tag, GetNetworker(opts...).IPv6) mapperTag.Store(PASSWORD, GetNetworker(opts...).Password) mapperTag.Store(JWT, GetNetworker(opts...).Jwt) + mapperTag.Store(BloodTypeTag, GetBlood(opts...).BloodGroup) } func initOption(opt ...options.OptionFunc) *options.Options { diff --git a/faker_test.go b/faker_test.go index 97011bb..5ea1f28 100644 --- a/faker_test.go +++ b/faker_test.go @@ -93,37 +93,37 @@ type SomeStruct struct { UInt32 uint32 UInt64 uint64 - Latitude float32 `faker:"lat"` - LATITUDE float64 `faker:"lat"` - RealAddress RealAddress `faker:"real_address"` - Long float32 `faker:"long"` - LONG float64 `faker:"long"` - StringValue string - CreditCardType string `faker:"cc_type"` - CreditCardNumber string `faker:"cc_number"` - Email string `faker:"email"` - IPV4 string `faker:"ipv4"` - IPV6 string `faker:"ipv6"` - Bool bool - SString []string - SInt []int - SInt8 []int8 - SInt16 []int16 - SInt32 []int32 - SInt64 []int64 - SFloat32 []float32 - SFloat64 []float64 - SBool []bool - Struct AStruct - TArray TArray - Time time.Time - Stime []time.Time - Currency string `faker:"currency"` - Amount float64 `faker:"amount"` - AmountWithCurrency string `faker:"amount_with_currency"` - ID string `faker:"uuid_digit"` - HyphenatedID string `faker:"uuid_hyphenated"` - + Latitude float32 `faker:"lat"` + LATITUDE float64 `faker:"lat"` + RealAddress RealAddress `faker:"real_address"` + Long float32 `faker:"long"` + LONG float64 `faker:"long"` + StringValue string + CreditCardType string `faker:"cc_type"` + CreditCardNumber string `faker:"cc_number"` + Email string `faker:"email"` + IPV4 string `faker:"ipv4"` + IPV6 string `faker:"ipv6"` + Bool bool + SString []string + SInt []int + SInt8 []int8 + SInt16 []int16 + SInt32 []int32 + SInt64 []int64 + SFloat32 []float32 + SFloat64 []float64 + SBool []bool + Struct AStruct + TArray TArray + Time time.Time + Stime []time.Time + Currency string `faker:"currency"` + Amount float64 `faker:"amount"` + AmountWithCurrency string `faker:"amount_with_currency"` + ID string `faker:"uuid_digit"` + HyphenatedID string `faker:"uuid_hyphenated"` + BloodType string `faker:"blood_type"` MapStringString map[string]string MapStringStruct map[string]AStruct MapCustomStringStruct map[SomeString]AStruct @@ -220,7 +220,7 @@ func (s SomeStruct) String() string { AmountWithCurrency: %v ID: %v HyphenatedID: %v - + BloodType: %v MapStringString: %v MapStringStruct: %v MapStringStructPointer: %v @@ -232,7 +232,7 @@ func (s SomeStruct) String() string { s.Email, s.IPV4, s.IPV6, s.Bool, s.SString, s.SInt, s.SInt8, s.SInt16, s.SInt32, s.SInt64, s.SFloat32, s.SFloat64, s.SBool, s.Struct, s.Time, s.Stime, s.Currency, s.Amount, - s.AmountWithCurrency, s.ID, s.HyphenatedID, s.MapStringString, + s.AmountWithCurrency, s.ID, s.HyphenatedID, s.BloodType, s.MapStringString, s.MapStringStruct, s.MapStringStructPointer) } From ac48bd0f9663506fe98f1915db24fb98228c798e Mon Sep 17 00:00:00 2001 From: Rubem Date: Wed, 7 Feb 2024 18:40:09 +0000 Subject: [PATCH 3/3] fix(blood): Changed tag location to be used by default without options Signed-off-by: Rubem --- faker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/faker.go b/faker.go index 56fe655..2439c1f 100644 --- a/faker.go +++ b/faker.go @@ -232,6 +232,7 @@ func initMappertTagDefault() { mapperTag.Store(RussianFirstNameFemaleTag, GetPerson().RussianFirstNameFemale) mapperTag.Store(RussianLastNameMaleTag, GetPerson().RussianLastNameMale) mapperTag.Store(RussianLastNameFemaleTag, GetPerson().RussianLastNameFemale) + mapperTag.Store(BloodTypeTag, GetBlood().BloodGroup) } // Compiled regexp @@ -286,7 +287,6 @@ func initMapperTagWithOption(opts ...options.OptionFunc) { mapperTag.Store(IPV6Tag, GetNetworker(opts...).IPv6) mapperTag.Store(PASSWORD, GetNetworker(opts...).Password) mapperTag.Store(JWT, GetNetworker(opts...).Jwt) - mapperTag.Store(BloodTypeTag, GetBlood(opts...).BloodGroup) } func initOption(opt ...options.OptionFunc) *options.Options {