From d699ec55ad5757773354a723e969d6c8c1c94400 Mon Sep 17 00:00:00 2001 From: tom twinkle Date: Wed, 1 Nov 2023 13:18:57 +0900 Subject: [PATCH 1/2] Add an accuracy option to the latitude and longitude of an address --- address.go | 23 +++++++++++++++++------ pkg/options/options.go | 2 ++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/address.go b/address.go index 64446f4..5a3d99a 100644 --- a/address.go +++ b/address.go @@ -3,6 +3,7 @@ package faker import ( _ "embed" "encoding/json" + "math" "reflect" "github.com/go-faker/faker/v4/pkg/options" @@ -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 @@ -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 @@ -81,7 +92,7 @@ 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) } @@ -89,7 +100,7 @@ func Longitude(opts ...options.OptionFunc) float64 { func Latitude(opts ...options.OptionFunc) float64 { return singleFakeData(LATITUDE, func() interface{} { address := Address{} - return float64(address.latitude()) + return float64(address.latitude(opts...)) }, opts...).(float64) } diff --git a/pkg/options/options.go b/pkg/options/options.go index c029177..b242305 100644 --- a/pkg/options/options.go +++ b/pkg/options/options.go @@ -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 decimal point of latitude and longitude of address + SetAddressMaxPosition int } // MaxDepthOption used for configuring the max depth of nested struct for faker From c95f5351a08031adff1c735c4f0bef450fb458fc Mon Sep 17 00:00:00 2001 From: tom twinkle Date: Wed, 1 Nov 2023 13:30:18 +0900 Subject: [PATCH 2/2] fix typo --- pkg/options/options.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/options/options.go b/pkg/options/options.go index b242305..dccfef8 100644 --- a/pkg/options/options.go +++ b/pkg/options/options.go @@ -61,7 +61,7 @@ type Options struct { RandomFloatBoundary *interfaces.RandomFloatBoundary // SetTagName sets the tag name that should be used TagName string - // SetAddressMaxPosition to set maximum precision to the decimal point of latitude and longitude of address + // SetAddressMaxPosition to set maximum precision to the float point of latitude and longitude of address SetAddressMaxPosition int }