Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
feat: added gender tag (#104)
Browse files Browse the repository at this point in the history
Co-authored-by: o.musin <[email protected]>
Co-authored-by: Iman Tumorang <[email protected]>
  • Loading branch information
3 people authored Jun 7, 2020
1 parent a67194d commit a395211
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const (
FirstNameFemaleTag = "first_name_female"
LastNameTag = "last_name"
NAME = "name"
GENDER = "gender"
UnixTimeTag = "unix_time"
DATE = "date"
TIME = "time"
Expand Down Expand Up @@ -137,6 +138,7 @@ var defaultTag = map[string]string{
FirstNameFemaleTag: FirstNameFemaleTag,
LastNameTag: LastNameTag,
NAME: NAME,
GENDER: GENDER,
UnixTimeTag: UnixTimeTag,
DATE: DATE,
TIME: TimeFormat,
Expand Down Expand Up @@ -185,6 +187,7 @@ var mapperTag = map[string]TaggedFunction{
FirstNameFemaleTag: GetPerson().FirstNameFemale,
LastNameTag: GetPerson().LastName,
NAME: GetPerson().Name,
GENDER: GetPerson().Gender,
UnixTimeTag: GetDateTimer().UnixTime,
DATE: GetDateTimer().Date,
TIME: GetDateTimer().Time,
Expand Down
20 changes: 20 additions & 0 deletions person.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Dowser interface {
FirstNameFemale(v reflect.Value) (interface{}, error)
LastName(v reflect.Value) (interface{}, error)
Name(v reflect.Value) (interface{}, error)
Gender(v reflect.Value) (interface{}, error)
}

var person Dowser
Expand Down Expand Up @@ -108,6 +109,8 @@ var lastNames = []string{
}
var randNameFlag = rand.Intn(100)

var genders = []string{"Male", "Female", "Prefer to skip"}

// GetPerson returns a new Dowser interface of Person struct
func GetPerson() Dowser {
mu.Lock()
Expand Down Expand Up @@ -249,3 +252,20 @@ func Name() string {
return p.name()
}).(string)
}

// Gender returns a random gender
func (p Person) Gender(v reflect.Value) (interface{}, error) {
return p.gender(), nil
}

func (p Person) gender() string {
return randomElementFromSliceString(genders)
}

// Gender get fake gender
func Gender() string {
return singleFakeData(GENDER, func() interface{} {
p := Person{}
return p.gender()
}).(string)
}
10 changes: 10 additions & 0 deletions person_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,13 @@ func TestFakeNameFemale(t *testing.T) {
t.Error("Expected from function name string get empty string")
}
}

func TestFakeGender(t *testing.T) {
gender, err := GetPerson().Gender(reflect.Value{})
if err != nil {
t.Error("Expected not error, got err", err)
}
if !slice.Contains(genders, gender.(string)) {
t.Error("Expected value from variable genders in function Gender")
}
}

0 comments on commit a395211

Please sign in to comment.