Skip to content

Commit

Permalink
feat: Add feature: Country Info(#47) (#50)
Browse files Browse the repository at this point in the history
* Add feature: Country Names(#47)

* fix indentation issues
  • Loading branch information
vitaliy-paliy authored Mar 29, 2024
1 parent 6d0a05f commit 181bd1b
Show file tree
Hide file tree
Showing 7 changed files with 1,841 additions and 23 deletions.
34 changes: 34 additions & 0 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,26 @@ var (
//go:embed misc/addresses-us-1000.min.json
addressesUSBytes []byte
addressesUS []RealAddress

//go:embed misc/country_info.json
countriesBytes []byte
countries []CountryInfo
)

func init() {
data := struct {
Addresses []RealAddress `json:"addresses"`
Countries []CountryInfo `json:"countries"`
}{}
if err := json.Unmarshal(addressesUSBytes, &data); err != nil {
panic(err)
}
addressesUS = data.Addresses

if err := json.Unmarshal(countriesBytes, &data); err != nil {
panic(err)
}
countries = data.Countries
}

// GetAddress returns a new Addresser interface of Address
Expand All @@ -35,6 +45,7 @@ type Addresser interface {
Latitude(v reflect.Value) (interface{}, error)
Longitude(v reflect.Value) (interface{}, error)
RealWorld(v reflect.Value) (interface{}, error)
CountryInfo(v reflect.Value) (interface{}, error)
}

// Address struct
Expand Down Expand Up @@ -72,6 +83,14 @@ func (i Address) realWorld() RealAddress {
return addressesUS[rand.Intn(len(addressesUS))]
}

func (i Address) countryInfo() CountryInfo {
return countries[rand.Intn(len(countries))]
}

func (i Address) CountryInfo(_ reflect.Value) (interface{}, error) {
return i.countryInfo(), nil
}

// RealWorld sets real world address
func (i Address) RealWorld(_ reflect.Value) (interface{}, error) {
return i.realWorld(), nil
Expand Down Expand Up @@ -113,3 +132,18 @@ func GetRealAddress(opts ...options.OptionFunc) RealAddress {
return address.realWorld()
}, opts...).(RealAddress)
}

type CountryInfo struct {
Abbr string `json:"abbr"`
Name string `json:"name"`
Capital string `json:"capital"`
Population string `json:"population"`
Continent string `json:"continent"`
}

func GetCountryInfo(opts ...options.OptionFunc) CountryInfo {
return singleFakeData(CountryInfoTag, func() interface{} {
address := Address{}
return address.countryInfo()
}, opts...).(CountryInfo)
}
24 changes: 24 additions & 0 deletions address_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package faker

import (
"regexp"
"testing"
)

Expand All @@ -25,3 +26,26 @@ func TestGetRealAddress(t *testing.T) {
}
t.Log(addr)
}

func TestGetCountryInfo(t *testing.T) {
rand.Seed(31)
countryInfo := GetCountryInfo()

expectedCountryName := "Morocco"
if countryInfo.Name != expectedCountryName {
t.Errorf("Test failed, expected: %s, got: %s", expectedCountryName, countryInfo.Name)
}

if len(countryInfo.Abbr) != 2 {
t.Error("Invalid ISO-3166 abbreviation")
}

if len(countryInfo.Continent) != 2 {
t.Error("Invalid continent abbreviation")
}

re := regexp.MustCompile(`^\d{1,3}(,\d{3})*$`)
if !re.MatchString(countryInfo.Population) {
t.Error("Invalid population number")
}
}
2 changes: 1 addition & 1 deletion example_with_custom_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ func Example_withTagsAndCustomTagName() {
}
*/

}
}
5 changes: 4 additions & 1 deletion faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ const (
RussianFirstNameFemaleTag = "russian_first_name_female"
RussianLastNameFemaleTag = "russian_last_name_female"
BloodTypeTag = "blood_type"
CountryInfoTag = "country_info"
)

// PriorityTags define the priority order of the tag
var PriorityTags = []string{ID, HyphenatedID, EmailTag, MacAddressTag, DomainNameTag, UserNameTag, URLTag, IPV4Tag,
IPV6Tag, PASSWORD, JWT, LATITUDE, LONGITUDE, CreditCardNumber, CreditCardType, PhoneNumber, TollFreeNumber,
IPV6Tag, PASSWORD, JWT, CountryInfoTag, LATITUDE, LONGITUDE, CreditCardNumber, CreditCardType, PhoneNumber, TollFreeNumber,
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,
Expand Down Expand Up @@ -145,6 +146,7 @@ func initDefaultTag() {
defaultTag.Store(JWT, JWT)
defaultTag.Store(CreditCardType, CreditCardType)
defaultTag.Store(CreditCardNumber, CreditCardNumber)
defaultTag.Store(CountryInfoTag, CountryInfoTag)
defaultTag.Store(LATITUDE, LATITUDE)
defaultTag.Store(LONGITUDE, LONGITUDE)
defaultTag.Store(RealAddressTag, RealAddressTag)
Expand Down Expand Up @@ -192,6 +194,7 @@ var mapperTag = mapperTagCustom{}
func initMappertTagDefault() {
mapperTag.Store(CreditCardType, GetPayment().CreditCardType)
mapperTag.Store(CreditCardNumber, GetPayment().CreditCardNumber)
mapperTag.Store(CountryInfoTag, GetAddress().CountryInfo)
mapperTag.Store(LATITUDE, GetAddress().Latitude)
mapperTag.Store(LONGITUDE, GetAddress().Longitude)
mapperTag.Store(RealAddressTag, GetAddress().RealWorld)
Expand Down
4 changes: 3 additions & 1 deletion faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type SomeStruct struct {
UInt32 uint32
UInt64 uint64

CountryInfo CountryInfo `faker:"country_info"`
Latitude float32 `faker:"lat"`
LATITUDE float64 `faker:"lat"`
RealAddress RealAddress `faker:"real_address"`
Expand Down Expand Up @@ -191,6 +192,7 @@ func (s SomeStruct) String() string {
UInt32: %v
UInt64: %v
CountryInfo: %v
Latitude: %v
LATITUDE: %v
RealAddress: %v
Expand Down Expand Up @@ -227,7 +229,7 @@ func (s SomeStruct) String() string {
}`, s.Inta, s.Int8, s.Int16, s.Int32,
s.Int64, s.Float32, s.Float64, s.UInta,
s.UInt8, s.UInt16, s.UInt32, s.UInt64,
s.Latitude, s.LATITUDE, s.RealAddress, s.Long, s.LONG,
s.CountryInfo, s.Latitude, s.LATITUDE, s.RealAddress, s.Long, s.LONG,
s.StringValue, s.CreditCardType, s.CreditCardNumber,
s.Email, s.IPV4, s.IPV6, s.Bool, s.SString, s.SInt,
s.SInt8, s.SInt16, s.SInt32, s.SInt64, s.SFloat32, s.SFloat64,
Expand Down
Loading

0 comments on commit 181bd1b

Please sign in to comment.