forked from Merkie/uule-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding.go
More file actions
60 lines (47 loc) · 1.44 KB
/
encoding.go
File metadata and controls
60 lines (47 loc) · 1.44 KB
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
package uule
import (
"encoding/base64"
"fmt"
"time"
)
type uuleVersion int
const (
// uuleVersion1 represents a UULE which is a location string.
uuleVersion1 uuleVersion = 1
// uuleVersion2 represents a UULE which is a latitude and longitude string.
uuleVersion2 uuleVersion = 2
)
var lengthPrefixKey = "EFGHIjKLMN0PQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789- ABCDEFGHIJKLMNOPQRSTUVWXYZL"
func getLengthPrefix(length int) (string, bool) {
if length < 4 || length > 89 {
return "", false
}
return string(lengthPrefixKey[length-4]), true
}
func encodeUULELatLong(latitude, longitude float64, radius int) (string, error) {
timestamp := time.Now().UnixMicro()
latitudeE7 := int(latitude * 1e7)
longitudeE7 := int(longitude * 1e7)
payload := fmt.Sprintf(`role:1
producer:12
provenance:6
timestamp:%d
latlng{
latitude_e7:%d
longitude_e7:%d
}
radius:%d`, timestamp, latitudeE7, longitudeE7, radius*620)
return encodeUULEString(payload, uuleVersion2)
}
func encodeUULEString(uuleString string, version uuleVersion) (string, error) {
encodedLocation := base64.StdEncoding.EncodeToString([]byte(uuleString))
if version == uuleVersion1 {
lengthPrefix, exists := getLengthPrefix(len(uuleString))
if !exists {
return "", fmt.Errorf("no length prefix found for length %d", len(uuleString))
}
// "w+CAIQICI" is the standard prefix for all UULEs
return "w+CAIQICI" + lengthPrefix + encodedLocation, nil
}
return "a+" + encodedLocation, nil
}