Skip to content

Commit

Permalink
add GetRealAddress & coverage badge (#14)
Browse files Browse the repository at this point in the history
* feat: support boundary for custom int type

* chore: upload test coverage to codecov

* feat: add GetRealAddress

* chore: add coverage badge

* chore: re org import

* feat: remove confused field in GetRealAddress
  • Loading branch information
wolf-joe authored Jan 17, 2023
1 parent 21cea9c commit 04f3781
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Struct Data Fake Generator
Faker will generate you a fake data based on your Struct.

[![Go Test](https://github.com/go-faker/faker/actions/workflows/go.yml/badge.svg)](https://github.com/go-faker/faker/actions/workflows/go.yml)
[![codecov](https://codecov.io/gh/go-faker/faker/branch/main/graph/badge.svg)](https://codecov.io/gh/go-faker/faker)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-faker/faker/v4)](https://goreportcard.com/report/github.com/go-faker/faker/v4)
[![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/go-faker/faker/blob/master/LICENSE)
[![Go.Dev](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white)](https://pkg.go.dev/github.com/go-faker/faker/v4?tab=doc)
Expand Down
38 changes: 38 additions & 0 deletions address.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
package faker

import (
_ "embed"
"encoding/json"
"reflect"

"github.com/go-faker/faker/v4/pkg/options"
)

var (
//go:embed misc/addresses-us-1000.min.json
addressesUSBytes []byte
addressesUS []RealAddress
)

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

// GetAddress returns a new Addresser interface of Address
func GetAddress() Addresser {
address := &Address{}
Expand Down Expand Up @@ -64,3 +82,23 @@ func Latitude(opts ...options.OptionFunc) float64 {
return float64(address.latitude())
}, opts...).(float64)
}

type RealCoordinates struct {
Latitude float64 `json:"lat"`
Longitude float64 `json:"lng"`
}

type RealAddress struct {
Address string `json:"address1"`
City string `json:"city"`
State string `json:"state"`
PostalCode string `json:"postalCode"`
Coordinates RealCoordinates `json:"coordinates"`
}

// GetRealAddress get real world address randomly
func GetRealAddress(opts ...options.OptionFunc) RealAddress {
return singleFakeData(RealAddressTag, func() interface{} {
return addressesUS[rand.Intn(len(addressesUS))]
}, opts...).(RealAddress)
}
8 changes: 8 additions & 0 deletions address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ func TestGetLatitude(t *testing.T) {
t.Error("function Latitude need return a valid longitude")
}
}

func TestGetRealAddress(t *testing.T) {
addr := GetRealAddress()
if addr.Address == "" || addr.City == "" {
t.Error("empty address")
}
t.Log(addr)
}
5 changes: 3 additions & 2 deletions example_single_fake_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
func Example_singleFakeData() {

// Address
fmt.Println(faker.Latitude()) // => 81.12195
fmt.Println(faker.Longitude()) // => -84.38158
fmt.Println(faker.Latitude()) // => 81.12195
fmt.Println(faker.Longitude()) // => -84.38158
fmt.Println(faker.GetRealAddress()) // => {2755 Country Drive Fremont CA 94536 {37.557882 -121.986823}}

// Datetime
fmt.Println(faker.UnixTime()) // => 1197930901
Expand Down
1 change: 1 addition & 0 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
JWT = "jwt"
LATITUDE = "lat"
LONGITUDE = "long"
RealAddressTag = "real_address"
CreditCardNumber = "cc_number"
CreditCardType = "cc_type"
PhoneNumber = "phone_number"
Expand Down
1 change: 1 addition & 0 deletions misc/addresses-us-1000.min.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions misc/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* `addresses-us-1000.min.json` is copy from https://github.com/EthanRBrown/rrad

0 comments on commit 04f3781

Please sign in to comment.