-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
86 lines (69 loc) · 1.66 KB
/
service.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
package tally
import (
"errors"
"github.com/gyokuro/tally/proto"
)
var (
ErrorNotFound = errors.New("Not found")
ErrorBadParam = errors.New("Bad parameter")
)
// Location by lat, lng
type Location struct {
Latitude float64
Longitude float64
}
type EventService interface {
Put(events []Tally.Event) error
}
// Typedef of Id, using 64 bit unsigned int.
type Id uint64
// Enumeration of units for distance
type DistanceUnit int
// Distance units
const (
Meters DistanceUnit = iota
Kilometers
Feet
Miles
)
// Cab strcture for minimum of id and location
type Cab struct {
Id Id `json:"id"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
// Structure for capturing the query parameters for within or proximity computation
type GeoWithin struct {
Center Location
Radius float64
Unit DistanceUnit
Limit int
}
// Service interface implemented by various backend datastores.
// The http server requires an implementation of this interface.
type CabService interface {
// Load a Cab by id. If not found, a ErrorNotFound must be returned.
Read(id Id) (Cab, error)
// Insert or update a cab
Upsert(cab Cab) error
// Deletes the cab by Id
Delete(id Id) error
// Queries for list of cabs by location and radius. If none, return empty list.
Query(query GeoWithin) ([]Cab, error)
// Delete all cabs
DeleteAll() error
// Performs any necessary clean up
Close()
}
// Sanitizes the input based on the spec. For example,
// the default value of limit is 8, and default unit of
// distance is meters.
func Sanitize(q *GeoWithin) *GeoWithin {
if q.Limit == 0 {
q.Limit = 8
}
if q.Unit == 0 {
q.Unit = Meters
}
return q
}