1
restaurant-search/types.go

199 lines
5.0 KiB
Go
Raw Normal View History

2015-03-24 06:17:37 +00:00
/*
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import "sort"
2015-03-25 03:33:41 +00:00
type featureMap map[string]float64
2015-03-24 13:36:53 +00:00
2015-03-25 10:10:34 +00:00
type jsonAccessRequest struct {
Id int `json:"id"`
Profile featureMap `json:"profile"`
}
2015-03-25 11:17:12 +00:00
type jsonGeoData struct {
2015-03-24 13:36:53 +00:00
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
2015-03-25 03:00:54 +00:00
type jsonQueryRequest struct {
2015-06-28 04:30:22 +00:00
Bracket *jsonNamedBracket `json:"bracket"`
Features featureMap `json:"features"`
Geo *jsonGeoData `json:"geo"`
MaxResults int `json:"maxResults"`
MinScore float64 `json:"minScore"`
Profile featureMap `json:"profile"`
Resolution int `json:"resolution"`
SortAsc bool `json:"sortAsc"`
SortKey string `json:"sortKey"`
WalkingDist float64 `json:"walkingDist"`
2015-03-24 13:36:53 +00:00
}
2015-03-25 03:00:54 +00:00
type jsonColumn struct {
2015-06-27 04:53:26 +00:00
Bracket jsonBracket `json:"bracket"`
Hints []jsonProjection `json:"hints"`
Steps int `json:"steps"`
Value float64 `json:"value"`
2015-03-25 03:00:54 +00:00
}
type jsonProjection struct {
Compatibility float64 `json:"compatibility"`
Count int `json:"count"`
2015-03-25 11:17:12 +00:00
Sample float64 `json:"sample"`
2015-03-25 03:00:54 +00:00
}
type jsonRecord struct {
AccessCount int `json:"accessCount"`
2015-03-25 11:17:12 +00:00
ClosestStn string `json:"closestStn"`
Compatibility float64 `json:"compatibility"`
2015-03-25 11:17:12 +00:00
DistanceToStn float64 `json:"distanceToStn"`
DistanceToUser float64 `json:"distanceToUser"`
2015-03-25 03:00:54 +00:00
Id int `json:"id"`
2015-03-25 11:17:12 +00:00
Name string `json:"name"`
Score float64 `json:"score"`
2015-03-26 03:18:43 +00:00
Url string `json:"url"`
2015-03-25 03:00:54 +00:00
}
2015-06-27 04:53:26 +00:00
type jsonBracket struct {
2015-06-27 03:48:44 +00:00
Min float64 `json:"min"`
Max float64 `json:"max"`
}
2015-06-28 04:30:22 +00:00
type jsonNamedBracket struct {
2015-06-28 04:48:53 +00:00
jsonBracket
Name string `json:"name"`
2015-06-28 04:30:22 +00:00
}
2015-03-25 03:00:54 +00:00
type jsonQueryResponse struct {
2015-06-28 04:30:22 +00:00
Columns map[string]jsonColumn `json:"columns"`
Count int `json:"count"`
MinScore float64 `json:"minScore"`
Records []jsonRecord `json:"records"`
2015-03-25 03:00:54 +00:00
}
2015-03-24 13:36:53 +00:00
type jsonCategory struct {
Description string `json:"description"`
Id int `json:"id"`
}
type jsonAddCategoryRequest struct {
Description string `json:"description"`
}
type jsonAddCategoryResponse struct {
Description string `json:"description"`
Id int `json:"id"`
Success bool `json:"success"`
}
type jsonRemoveCategoryRequest struct {
Id int `json:"id"`
}
type jsonRemoveCategoryResponse struct {
Success bool `json:"success"`
}
2015-03-24 11:04:52 +00:00
type queryContext struct {
2015-03-25 11:17:12 +00:00
geo *geoData
2015-03-24 11:04:52 +00:00
profile featureMap
walkingDist float64
}
2015-03-24 11:04:52 +00:00
type queryProjection struct {
compatibility float64
2015-03-24 06:17:37 +00:00
count int
2015-03-25 11:17:12 +00:00
sample float64
2015-03-24 06:17:37 +00:00
}
2015-03-25 11:17:12 +00:00
type geoData struct {
2015-03-24 08:58:35 +00:00
latitude float64
longitude float64
}
2015-06-28 04:39:10 +00:00
type namedBracket struct {
name string
min float64
max float64
}
2015-03-24 11:04:52 +00:00
type record struct {
2015-03-24 06:17:37 +00:00
accessCount int
2015-03-25 03:00:54 +00:00
closestStn string
compatibility float64
distanceToStn float64
distanceToUser float64
2015-03-24 11:04:52 +00:00
features featureMap
2015-03-25 11:17:12 +00:00
geo geoData
2015-03-24 06:17:37 +00:00
id int
name string
score float64
2015-03-25 03:00:54 +00:00
url string
2015-03-24 06:17:37 +00:00
}
2015-03-24 11:04:52 +00:00
type records []record
2015-03-24 06:17:37 +00:00
type recordSorter struct {
ascending bool
entries records
key string
}
2015-06-24 10:11:43 +00:00
func (s recordSorter) sort() {
if s.ascending {
sort.Sort(s)
} else {
2015-06-24 10:11:43 +00:00
sort.Sort(sort.Reverse(s))
}
2015-03-24 06:17:37 +00:00
}
2015-06-24 10:11:43 +00:00
func (s recordSorter) Len() int {
return len(s.entries)
}
2015-06-24 10:11:43 +00:00
func (s recordSorter) Less(i, j int) bool {
entry1 := s.entries[i]
entry2 := s.entries[j]
2015-06-24 10:11:43 +00:00
switch s.key {
case "accessCount":
return entry1.accessCount < entry2.accessCount
case "closestStn":
return entry1.closestStn < entry2.closestStn
case "compatibility":
return entry1.compatibility < entry2.compatibility
case "distanceToStn":
return entry1.distanceToStn < entry2.distanceToStn
case "distanceToUser":
return entry1.distanceToUser < entry2.distanceToUser
case "name":
return entry1.name < entry2.name
default:
return entry1.score < entry2.score
}
2015-03-24 06:17:37 +00:00
}
2015-06-24 10:11:43 +00:00
func (s recordSorter) Swap(i, j int) {
s.entries[i], s.entries[j] = s.entries[j], s.entries[i]
2015-03-24 06:17:37 +00:00
}