Skip to content

Commit

Permalink
feat(blood): Added faker for blood types (#40)
Browse files Browse the repository at this point in the history
* feat(blood): Added faker for blood types

Added faker that will generate blood type, rh factor and group

Signed-off-by: Rubem Mota <rubemmota89@gmail.com>

* feat(blood): Added tag into main tag

Signed-off-by: Rubem <rubemmota89@gmail.com>

* fix(blood): Changed tag location to be used by default without options

Signed-off-by: Rubem <rubemmota89@gmail.com>

---------

Signed-off-by: Rubem Mota <rubemmota89@gmail.com>
Signed-off-by: Rubem <rubemmota89@gmail.com>
  • Loading branch information
rubemlrm authored Feb 12, 2024
1 parent 3032a45 commit eb0a115
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 34 deletions.
54 changes: 54 additions & 0 deletions blood.go
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 41 additions & 0 deletions blood_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
5 changes: 4 additions & 1 deletion faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -230,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
Expand Down
66 changes: 33 additions & 33 deletions faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -220,7 +220,7 @@ func (s SomeStruct) String() string {
AmountWithCurrency: %v
ID: %v
HyphenatedID: %v
BloodType: %v
MapStringString: %v
MapStringStruct: %v
MapStringStructPointer: %v
Expand All @@ -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)
}

Expand Down

0 comments on commit eb0a115

Please sign in to comment.