-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplace.go
80 lines (62 loc) · 1.54 KB
/
place.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
package main
import (
"encoding/binary"
"encoding/hex"
"errors"
"github.com/google/uuid"
"math"
)
const PlaceIdSize = 32
type PlaceId struct {
UserId uuid.UUID
Lat float64
Long float64
}
func (placeId PlaceId) putBytes(dst []byte) error {
if len(dst) < PlaceIdSize {
return errors.New("placeId: failed to serialize")
}
copy(dst, placeId.UserId[:])
binary.LittleEndian.PutUint64(dst[16:], math.Float64bits(placeId.Lat))
binary.LittleEndian.PutUint64(dst[24:], math.Float64bits(placeId.Long))
return nil
}
func placeIdFromBytes(src []byte) (PlaceId, error) {
if src == nil || len(src) != PlaceIdSize {
return PlaceId{}, errors.New("placeId: failed to parse")
}
userId, err := uuid.FromBytes(src[0:16])
if err != nil {
return PlaceId{}, err
}
lat := math.Float64frombits(binary.LittleEndian.Uint64(src[16:]))
long := math.Float64frombits(binary.LittleEndian.Uint64(src[24:]))
return PlaceId{UserId: userId, Lat: lat, Long: long}, nil
}
func (placeId PlaceId) ToString(key []byte) (string, error) {
data := make([]byte, PlaceIdSize)
err := placeId.putBytes(data)
if err != nil {
return "", err
}
err = Encrypt(data, key)
if err != nil {
return "", err
}
return hex.EncodeToString(data), nil
}
func PlaceIdFromString(value string, key []byte) (PlaceId, error) {
data, err := hex.DecodeString(value)
if err != nil {
return PlaceId{}, err
}
err = Decrypt(data, key)
if err != nil {
return PlaceId{}, err
}
placeId, err := placeIdFromBytes(data)
if err != nil {
return PlaceId{}, err
}
return placeId, nil
}