From 2e67874e047539777cc81e78a318ce6bcde05e4f Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Thu, 22 Dec 2022 18:07:01 +0100 Subject: [PATCH] Add NominatimLocation --- internal/geo/ip/ip.go | 4 ++-- internal/geo/location/location.go | 11 +++++------ internal/geo/location/nominatim.go | 18 ++++++++++++++++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/internal/geo/ip/ip.go b/internal/geo/ip/ip.go index 91f3450..c494bd4 100644 --- a/internal/geo/ip/ip.go +++ b/internal/geo/ip/ip.go @@ -33,12 +33,12 @@ func (l *Address) String() string { if l.Latitude == -1000 { return fmt.Sprintf( "%s;%s;%s;%s", - l.CountryCode, l.CountryCode, l.Region, l.City) + l.CountryCode, l.Country, l.Region, l.City) } return fmt.Sprintf( "%s;%s;%s;%s;%v;%v", - l.CountryCode, l.CountryCode, l.Region, l.City, l.Latitude, l.Longitude) + l.CountryCode, l.Country, l.Region, l.City, l.Latitude, l.Longitude) } // Cache provides access to the IP Geodata cache. diff --git a/internal/geo/location/location.go b/internal/geo/location/location.go index 0338b40..7cffd00 100644 --- a/internal/geo/location/location.go +++ b/internal/geo/location/location.go @@ -6,12 +6,11 @@ import ( ) type Location struct { - Name string `db:"name,key"` - Lat string `db:"lat"` - Lon string `db:"lon"` - Timezone string `db:"timezone"` - //nolint:tagliatelle - Fullname string `db:"displayName" json:"display_name"` + Name string `db:"name,key" json:"name"` + Lat string `db:"lat" json:"latitude"` + Lon string `db:"lon" json:"longitude"` + Timezone string `db:"timezone" json:"timezone"` + Fullname string `db:"displayName" json:"address"` } // String returns string representation of location. diff --git a/internal/geo/location/nominatim.go b/internal/geo/location/nominatim.go index 6b3e752..2927761 100644 --- a/internal/geo/location/nominatim.go +++ b/internal/geo/location/nominatim.go @@ -17,6 +17,14 @@ type Nominatim struct { token string } +type NominatimLocation struct { + Name string `db:"name,key"` + Lat string `db:"lat"` + Lon string `db:"lon"` + //nolint:tagliatelle + Fullname string `db:"displayName" json:"display_name"` +} + func NewNominatim(name, url, token string) *Nominatim { return &Nominatim{ name: name, @@ -27,7 +35,7 @@ func NewNominatim(name, url, token string) *Nominatim { func (n *Nominatim) Query(location string) (*Location, error) { var ( - result []Location + result []NominatimLocation errResponse struct { Error string @@ -65,5 +73,11 @@ func (n *Nominatim) Query(location string) (*Location, error) { return nil, fmt.Errorf("%w: %s: invalid response", types.ErrUpstream, n.name) } - return &result[0], nil + nl := &result[0] + + return &Location{ + Lat: nl.Lat, + Lon: nl.Lon, + Fullname: nl.Fullname, + }, nil }