2021-02-08 17:27:19 +00:00
|
|
|
package locodedb
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/locode"
|
|
|
|
locodecolumn "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/locode/column"
|
2021-02-08 17:27:19 +00:00
|
|
|
)
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// Key represents the key in FrostFS location database.
|
2021-02-08 17:27:19 +00:00
|
|
|
type Key struct {
|
|
|
|
cc *CountryCode
|
|
|
|
|
|
|
|
lc *LocationCode
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewKey calculates Key from LOCODE.
|
|
|
|
func NewKey(lc locode.LOCODE) (*Key, error) {
|
|
|
|
country, err := CountryCodeFromString(lc.CountryCode())
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not parse country: %w", err)
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
location, err := LocationCodeFromString(lc.LocationCode())
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not parse location: %w", err)
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Key{
|
|
|
|
cc: country,
|
|
|
|
lc: location,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// CountryCode returns the location's country code.
|
2021-02-08 17:27:19 +00:00
|
|
|
func (k *Key) CountryCode() *CountryCode {
|
|
|
|
return k.cc
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// LocationCode returns the location code.
|
2021-02-08 17:27:19 +00:00
|
|
|
func (k *Key) LocationCode() *LocationCode {
|
|
|
|
return k.lc
|
|
|
|
}
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// Record represents the entry in FrostFS location database.
|
2021-02-08 17:27:19 +00:00
|
|
|
type Record struct {
|
|
|
|
countryName string
|
|
|
|
|
2021-02-10 10:20:39 +00:00
|
|
|
locationName string
|
2021-02-08 17:27:19 +00:00
|
|
|
|
|
|
|
subDivName string
|
|
|
|
|
|
|
|
subDivCode string
|
|
|
|
|
|
|
|
p *Point
|
|
|
|
|
|
|
|
cont *Continent
|
|
|
|
}
|
|
|
|
|
|
|
|
var errParseCoordinates = errors.New("invalid coordinates")
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// NewRecord calculates the Record from the UN/LOCODE table record.
|
2021-02-08 17:27:19 +00:00
|
|
|
func NewRecord(r locode.Record) (*Record, error) {
|
|
|
|
crd, err := locodecolumn.CoordinatesFromString(r.Coordinates)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("%w: %v", errParseCoordinates, err)
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
point, err := PointFromCoordinates(crd)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not parse geo point: %w", err)
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Record{
|
2021-02-10 10:20:39 +00:00
|
|
|
locationName: r.NameWoDiacritics,
|
|
|
|
subDivCode: r.SubDiv,
|
|
|
|
p: point,
|
2021-02-08 17:27:19 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// CountryName returns the country name.
|
2021-02-08 17:27:19 +00:00
|
|
|
func (r *Record) CountryName() string {
|
|
|
|
return r.countryName
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SetCountryName sets the country name.
|
2021-02-08 17:27:19 +00:00
|
|
|
func (r *Record) SetCountryName(name string) {
|
|
|
|
r.countryName = name
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// LocationName returns the location name.
|
2021-02-10 10:20:39 +00:00
|
|
|
func (r *Record) LocationName() string {
|
|
|
|
return r.locationName
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SetLocationName sets the location name.
|
2021-02-10 10:20:39 +00:00
|
|
|
func (r *Record) SetLocationName(name string) {
|
|
|
|
r.locationName = name
|
2021-02-08 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SubDivCode returns the subdivision code.
|
2021-02-08 17:27:19 +00:00
|
|
|
func (r *Record) SubDivCode() string {
|
|
|
|
return r.subDivCode
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SetSubDivCode sets the subdivision code.
|
2021-02-08 17:27:19 +00:00
|
|
|
func (r *Record) SetSubDivCode(name string) {
|
|
|
|
r.subDivCode = name
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SubDivName returns the subdivision name.
|
2021-02-08 17:27:19 +00:00
|
|
|
func (r *Record) SubDivName() string {
|
|
|
|
return r.subDivName
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SetSubDivName sets the subdivision name.
|
2021-02-08 17:27:19 +00:00
|
|
|
func (r *Record) SetSubDivName(name string) {
|
|
|
|
r.subDivName = name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GeoPoint returns geo point of the location.
|
|
|
|
func (r *Record) GeoPoint() *Point {
|
|
|
|
return r.p
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGeoPoint sets geo point of the location.
|
|
|
|
func (r *Record) SetGeoPoint(p *Point) {
|
|
|
|
r.p = p
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Continent returns the location continent.
|
2021-02-08 17:27:19 +00:00
|
|
|
func (r *Record) Continent() *Continent {
|
|
|
|
return r.cont
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SetContinent sets the location continent.
|
2021-02-08 17:27:19 +00:00
|
|
|
func (r *Record) SetContinent(c *Continent) {
|
|
|
|
r.cont = c
|
|
|
|
}
|