Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an accuracy option to the latitude and longitude of an address #38

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package faker
import (
_ "embed"
"encoding/json"
"math"
"reflect"

"github.com/go-faker/faker/v4/pkg/options"
Expand Down Expand Up @@ -40,8 +41,13 @@ type Addresser interface {
// Address struct
type Address struct{}

func (i Address) latitude() float32 {
return (rand.Float32() * 180) - 90
func (i Address) latitude(opts ...options.OptionFunc) float32 {
lat := (rand.Float32() * 180) - 90
ops := initOption(opts...)
if ops.SetAddressMaxPosition > 0 {
return float32(math.Round(math.Pow10(ops.SetAddressMaxPosition)*float64(lat)) / math.Pow10(ops.SetAddressMaxPosition))
}
return lat
}

// Latitude sets latitude of the address
Expand All @@ -54,8 +60,13 @@ func (i Address) Latitude(v reflect.Value) (interface{}, error) {
return float64(val), nil
}

func (i Address) longitude() float32 {
return (rand.Float32() * 360) - 180
func (i Address) longitude(opts ...options.OptionFunc) float32 {
lng := (rand.Float32() * 360) - 180
ops := initOption(opts...)
if ops.SetAddressMaxPosition > 0 {
return float32(math.Round(math.Pow10(ops.SetAddressMaxPosition)*float64(lng)) / math.Pow10(ops.SetAddressMaxPosition))
}
return lng
}

// Longitude sets longitude of the address
Expand All @@ -81,15 +92,15 @@ func (i Address) RealWorld(_ reflect.Value) (interface{}, error) {
func Longitude(opts ...options.OptionFunc) float64 {
return singleFakeData(LONGITUDE, func() interface{} {
address := Address{}
return float64(address.longitude())
return float64(address.longitude(opts...))
}, opts...).(float64)
}

// Latitude get fake latitude randomly
func Latitude(opts ...options.OptionFunc) float64 {
return singleFakeData(LATITUDE, func() interface{} {
address := Address{}
return float64(address.latitude())
return float64(address.latitude(opts...))
}, opts...).(float64)
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type Options struct {
RandomFloatBoundary *interfaces.RandomFloatBoundary
// SetTagName sets the tag name that should be used
TagName string
// SetAddressMaxPosition to set maximum precision to the float point of latitude and longitude of address
SetAddressMaxPosition int
}

// MaxDepthOption used for configuring the max depth of nested struct for faker
Expand Down