/
place.go
101 lines (84 loc) · 1.68 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package docs
import (
"bytes"
"strings"
"encoding/hex"
"encoding/json"
"strconv"
)
type PlaceInput struct {
Continent string
Ocean string
Union string
Country string
CountyOrState string
District string
TownOrCity string
Borough string
Road string
Building string
Apartment string
}
func ExamplePlace() PlaceInput {
return PlaceInput{
Continent: "EUROPE",
Union: "UNITED KINGDOM",
Country: "ENGLAND",
CountyOrState: "KENT",
District: "THANET",
TownOrCity: "MARGATE",
Borough: "CLIFTONVILLE",
Road: "DALBY SQUARE",
Building: "14",
}
}
type Place struct {
ID string
Details []string
}
func (place *Place) ToID() string {
b := Hash(place.ToJSON())
return hex.EncodeToString(b)
}
func (place *Place) ToJSON() []byte {
b, err := json.Marshal(place.Details)
if err != nil {
panic("JSON place")
}
return b
}
func (place *Place) ParentHashes() []string {
hashes := []string{}
var currentHash string
for n, v := range place.Details {
currentHash = hex.EncodeToString(
Hash(
bytes.Join(
[][]byte{
[]byte(currentHash),
[]byte(Hash([]byte(strconv.Itoa(n) + " " + v))),
},
nil,
),
),
)
hashes = append(hashes, currentHash)
}
return hashes
}
func NewPlace(input string) Place {
ss := strings.Split(input, ", ")
last := len(ss) - 1
for i := 0; i < len(ss)/2; i++ {
ss[i], ss[last-i] = ss[last-i], ss[i]
}
place := Place{
Details: ss,
}
place.ID = place.ToID()
return place
}
func (self Place) URI() string {
hashes := self.ParentHashes()
return "where/" + strings.Join(hashes, "/")
}