This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathobjects.go
192 lines (170 loc) · 5.56 KB
/
objects.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package t38c
import (
"strconv"
"time"
geojson "github.com/paulmach/go.geojson"
"github.com/tidwall/gjson"
)
type field struct {
Name string
Value float64
}
// KeyStats struct
type KeyStats struct {
InMemorySize int `json:"in_memory_size"`
NumObjects int `json:"num_objects"`
NumPoints int `json:"num_points"`
}
// Point struct
type Point struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
}
// Bounds struct
type Bounds struct {
SW Point `json:"sw"`
NE Point `json:"ne"`
}
// Object struct
type Object struct {
FeatureCollection *geojson.FeatureCollection `json:"featureCollection,omitempty"`
Feature *geojson.Feature `json:"feature,omitempty"`
Geometry *geojson.Geometry `json:"geometry,omitempty"`
String *string `json:"string,omitempty"`
}
// UnmarshalJSON ...
func (ob *Object) UnmarshalJSON(data []byte) error {
res := gjson.ParseBytes(data)
objectType := res.Get("type")
if !objectType.Exists() {
str := res.String()
ob.String = &str
return nil
}
var err error
switch objectType.String() {
case "FeatureCollection":
ob.FeatureCollection, err = geojson.UnmarshalFeatureCollection(data)
case "Feature":
ob.Feature, err = geojson.UnmarshalFeature(data)
default:
ob.Geometry, err = geojson.UnmarshalGeometry(data)
}
return err
}
// SearchResponse struct
type SearchResponse struct {
Cursor int `json:"cursor"`
Count int `json:"count"`
Fields []string `json:"fields,omitempty"`
Objects []struct {
ID string `json:"ID"`
Object Object `json:"object"`
Fields []float64 `json:"fields,omitempty"`
Distance *float64 `json:"distance,omitempty"`
} `json:"objects,omitempty"`
Points []struct {
ID string `json:"ID"`
Point Point `json:"point"`
Fields []float64 `json:"fields,omitempty"`
Distance *float64 `json:"distance,omitempty"`
} `json:"points,omitempty"`
Bounds []struct {
ID string `json:"ID"`
Bounds Bounds `json:"bounds"`
Fields []float64 `json:"fields,omitempty"`
Distance *float64 `json:"distance,omitempty"`
} `json:"bounds,omitempty"`
Hashes []struct {
ID string `json:"id"`
Hash string `json:"hash"`
Fields []float64 `json:"fields,omitempty"`
Distance *float64 `json:"distance,omitempty"`
} `json:"hashes,omitempty"`
IDs []string `json:"ids,omitempty"`
}
// OutputFormat ...
type OutputFormat cmd
var (
// FormatCount - Total object count sent in the response.
// When LIMIT or CURSOR are provided, COUNT returns the number of results that would otherwise be sent as objects.
// When LIMIT is not specified, COUNT totals up all items starting from provided CURSOR position
// (or zero if a cursor is omitted). LIMIT and CURSOR options are ignored.
FormatCount = OutputFormat(newCmd("COUNT"))
// FormatIDs - A list of IDs belonging to the key. Will not return the objects.
FormatIDs = OutputFormat(newCmd("IDS"))
// FormatPoints - A list of standard latitude, longitude points.
FormatPoints = OutputFormat(newCmd("POINTS"))
// FormatBounds - A list of minimum bounding rectangle.
FormatBounds = OutputFormat(newCmd("BOUNDS"))
// FormatHashes - A list of Geohash. Requires a precision of 1 to 22.
FormatHashes = func(precision int) OutputFormat {
return OutputFormat(newCmd("HASHES", strconv.Itoa(precision)))
}
)
// Meta struct
type Meta struct {
Name string
Value string
}
// Hook struct
type Hook struct {
Endpoints []string `json:"endpoints"`
Chan
}
// Chan struct
type Chan struct {
Name string `json:"name"`
Key string `json:"key"`
Command []string `json:"command"`
Meta map[string]string `json:"meta"`
}
// GeofenceEvent struct
type GeofenceEvent struct {
Command string `json:"command"`
Hook string `json:"hook,omitempty"`
Group string `json:"group"`
Detect string `json:"detect"`
Key string `json:"key"`
Time time.Time `json:"time"`
ID string `json:"id"`
Object *Object `json:"object,omitempty"`
Point *Point `json:"point,omitempty"`
Bounds *Bounds `json:"bounds,omitempty"`
Hash *string `json:"hash,omitempty"`
Nearby *RoamObject `json:"nearby,omitempty"`
Faraway *RoamObject `json:"faraway,omitempty"`
Fields map[string]float64 `json:"fields,omitempty"`
}
// RoamObject struct
type RoamObject struct {
Key string `json:"key"`
ID string `json:"id"`
Object Object `json:"object"`
Meters float64 `json:"meters"`
}
// NotifyCommand ...
type NotifyCommand string
const (
// Del notifies the client that an object has been deleted from the collection that is being fenced.
Del NotifyCommand = "del"
// Drop notifies the client that the entire collection is dropped.
Drop NotifyCommand = "drop"
// Set notifies the client that an object has been added or updated,
// and when it’s position is detected by the fence.
Set NotifyCommand = "set"
)
// DetectAction ...
type DetectAction string
const (
// Inside is when an object is inside the specified area.
Inside DetectAction = "inside"
// Outside is when an object is outside the specified area.
Outside DetectAction = "outside"
// Enter is when an object that was not previously in the fence has entered the area.
Enter DetectAction = "enter"
// Exit is when an object that was previously in the fence has exited the area.
Exit DetectAction = "exit"
// Cross is when an object that was not previously in the fence has entered and exited the area.
Cross DetectAction = "cross"
)