-
Notifications
You must be signed in to change notification settings - Fork 205
/
geolocation.go
65 lines (55 loc) · 1.62 KB
/
geolocation.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
package types
import (
"fmt"
"strconv"
"strings"
planstypes "github.com/lavanet/lava/x/plans/types"
)
// for convenience (calculate once only)
var (
allGeoEnumRegionsList []int32
allGeoEnumRegions int32
)
// initialize convenience vars at start-up
func init() {
for _, geoloc := range planstypes.Geolocation_value {
if geoloc != int32(planstypes.Geolocation_GLS) && geoloc != int32(planstypes.Geolocation_GL) {
allGeoEnumRegionsList = append(allGeoEnumRegionsList, geoloc)
allGeoEnumRegions |= geoloc
}
}
}
// IsValidGeoEnum tests the validity of a given geolocation
func IsValidGeoEnum(geoloc int32) bool {
return geoloc != int32(planstypes.Geolocation_GLS) && (geoloc & ^allGeoEnumRegions) == 0
}
// IsGeoEnumSingleBit returns true if at most one bit is set
func IsGeoEnumSingleBit(geoloc int32) bool {
return (geoloc & (geoloc - 1)) == 0
}
// ParseGeoEnum parses a string into GeoEnum bitmask.
// The string may be a number or a comma-separated geolocations codes.
func ParseGeoEnum(arg string) (geoloc int32, err error) {
geoloc64, err := strconv.ParseUint(arg, 10, 32)
geoloc = int32(geoloc64)
if err == nil {
if geoloc != int32(planstypes.Geolocation_GL) {
if !IsValidGeoEnum(geoloc) {
return 0, fmt.Errorf("invalid geolocation value: %s", arg)
}
}
return geoloc, nil
}
split := strings.Split(arg, ",")
for _, s := range split {
val, ok := planstypes.Geolocation_value[s]
if !ok || val == int32(planstypes.Geolocation_GLS) {
return 0, fmt.Errorf("invalid geolocation code: %s", s)
}
geoloc |= val
}
return geoloc, nil
}
func GetGeolocations() []int32 {
return allGeoEnumRegionsList
}