forked from nspcc-dev/neofs-node
-
Notifications
You must be signed in to change notification settings - Fork 6
/
record.go
140 lines (109 loc) · 2.92 KB
/
record.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package locodedb
import (
"errors"
"fmt"
"github.com/TrueCloudLab/frostfs-node/pkg/util/locode"
locodecolumn "github.com/TrueCloudLab/frostfs-node/pkg/util/locode/column"
)
// Key represents the key in FrostFS location database.
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 {
return nil, fmt.Errorf("could not parse country: %w", err)
}
location, err := LocationCodeFromString(lc.LocationCode())
if err != nil {
return nil, fmt.Errorf("could not parse location: %w", err)
}
return &Key{
cc: country,
lc: location,
}, nil
}
// CountryCode returns the location's country code.
func (k *Key) CountryCode() *CountryCode {
return k.cc
}
// LocationCode returns the location code.
func (k *Key) LocationCode() *LocationCode {
return k.lc
}
// Record represents the entry in FrostFS location database.
type Record struct {
countryName string
locationName string
subDivName string
subDivCode string
p *Point
cont *Continent
}
var errParseCoordinates = errors.New("invalid coordinates")
// NewRecord calculates the Record from the UN/LOCODE table record.
func NewRecord(r locode.Record) (*Record, error) {
crd, err := locodecolumn.CoordinatesFromString(r.Coordinates)
if err != nil {
return nil, fmt.Errorf("%w: %v", errParseCoordinates, err)
}
point, err := PointFromCoordinates(crd)
if err != nil {
return nil, fmt.Errorf("could not parse geo point: %w", err)
}
return &Record{
locationName: r.NameWoDiacritics,
subDivCode: r.SubDiv,
p: point,
}, nil
}
// CountryName returns the country name.
func (r *Record) CountryName() string {
return r.countryName
}
// SetCountryName sets the country name.
func (r *Record) SetCountryName(name string) {
r.countryName = name
}
// LocationName returns the location name.
func (r *Record) LocationName() string {
return r.locationName
}
// SetLocationName sets the location name.
func (r *Record) SetLocationName(name string) {
r.locationName = name
}
// SubDivCode returns the subdivision code.
func (r *Record) SubDivCode() string {
return r.subDivCode
}
// SetSubDivCode sets the subdivision code.
func (r *Record) SetSubDivCode(name string) {
r.subDivCode = name
}
// SubDivName returns the subdivision name.
func (r *Record) SubDivName() string {
return r.subDivName
}
// SetSubDivName sets the subdivision name.
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
}
// Continent returns the location continent.
func (r *Record) Continent() *Continent {
return r.cont
}
// SetContinent sets the location continent.
func (r *Record) SetContinent(c *Continent) {
r.cont = c
}