1

Serialization cleanup

This commit is contained in:
Alex Yatskov 2015-03-24 20:52:40 +09:00
parent 59accf2e53
commit 944f64f5d8
2 changed files with 43 additions and 29 deletions

View File

@ -36,24 +36,38 @@ import (
var db *sql.DB var db *sql.DB
func executeQuery(rw http.ResponseWriter, req *http.Request) { func executeQuery(rw http.ResponseWriter, req *http.Request) {
type Request struct { type JsonFeatureMap map[string]float64
features featureMap
bounds queryBounds
geo geoContext
walkingDist float64
minScore float64
hintSteps int
maxResults int
// features: _ctx.query.features || {}, type JsonRange struct {
// range: {min: -1.0, max: 1.0}, Max float64 `json:"max"`
// profile: getProfile(), Min float64 `json:"min"`
// walkingDist: parseFloat($('#walkingDist').val()),
// minScore: parseFloat($('#minScore').val()),
// hintSteps: parseInt($('#hintSteps').val()),
// maxResults: parseInt($('#maxResults').val())
} }
type JsonGeo struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Valid bool `json:"valid"`
}
type JsonRequest struct {
Features JsonFeatureMap `json:"features"`
Geo *JsonGeo `json:"geo"`
HintSteps int `json:"hintSteps"`
MaxResults int `json:"maxResults"`
MinScore float64 `json:"minScore"`
Profile JsonFeatureMap `json:"profile"`
Range JsonRange `json:"range"`
WalkingDist float64 `json:"walkingDist"`
}
var request JsonRequest
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
log.Print("Ready")
// function runQuery(query, callback) { // function runQuery(query, callback) {
// query.profile = fixupProfile(query.profile); // query.profile = fixupProfile(query.profile);
// query.features = fixupFeatures(query.features); // query.features = fixupFeatures(query.features);
@ -99,7 +113,7 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) {
} }
func getCategories(rw http.ResponseWriter, req *http.Request) { func getCategories(rw http.ResponseWriter, req *http.Request) {
type Category struct { type JsonCategory struct {
Description string `json:"description"` Description string `json:"description"`
Id int `json:"id"` Id int `json:"id"`
} }
@ -110,7 +124,7 @@ func getCategories(rw http.ResponseWriter, req *http.Request) {
} }
defer rows.Close() defer rows.Close()
var categories []Category var categories []JsonCategory
for rows.Next() { for rows.Next() {
var ( var (
description string description string
@ -121,7 +135,7 @@ func getCategories(rw http.ResponseWriter, req *http.Request) {
log.Fatal(err) log.Fatal(err)
} }
categories = append(categories, Category{description, id}) categories = append(categories, JsonCategory{description, id})
} }
if err := rows.Err(); err != nil { if err := rows.Err(); err != nil {
@ -138,23 +152,23 @@ func getCategories(rw http.ResponseWriter, req *http.Request) {
} }
func addCategory(rw http.ResponseWriter, req *http.Request) { func addCategory(rw http.ResponseWriter, req *http.Request) {
type Request struct { type JsonRequest struct {
Description string `json:"description"` Description string `json:"description"`
} }
type Response struct { type JsonResponse struct {
Description string `json:"description"` Description string `json:"description"`
Id int `json:"id"` Id int `json:"id"`
Success bool `json:"success"` Success bool `json:"success"`
} }
var request Request var request JsonRequest
if err := json.NewDecoder(req.Body).Decode(&request); err != nil { if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }
response := Response{Description: strings.TrimSpace(request.Description)} response := JsonResponse{Description: strings.TrimSpace(request.Description)}
if len(request.Description) > 0 { if len(request.Description) > 0 {
result, err := db.Exec("INSERT INTO categories(description) VALUES(?)", request.Description) result, err := db.Exec("INSERT INTO categories(description) VALUES(?)", request.Description)
@ -186,15 +200,15 @@ func addCategory(rw http.ResponseWriter, req *http.Request) {
} }
func removeCategory(rw http.ResponseWriter, req *http.Request) { func removeCategory(rw http.ResponseWriter, req *http.Request) {
type Request struct { type JsonRequest struct {
Id int `json:"id"` Id int `json:"id"`
} }
type Response struct { type JsonResponse struct {
Success bool `json:"success"` Success bool `json:"success"`
} }
var request Request var request JsonRequest
if err := json.NewDecoder(req.Body).Decode(&request); err != nil { if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
@ -210,7 +224,7 @@ func removeCategory(rw http.ResponseWriter, req *http.Request) {
log.Fatal(err) log.Fatal(err)
} }
js, err := json.Marshal(Response{affectedRows > 0}) js, err := json.Marshal(JsonResponse{affectedRows > 0})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -258,7 +272,7 @@ func accessReview(rw http.ResponseWriter, req *http.Request) {
// } // }
} }
func getStaticPath() (string, error) { func staticPath() (string, error) {
if len(os.Args) > 1 { if len(os.Args) > 1 {
return os.Args[1], nil return os.Args[1], nil
} }
@ -267,7 +281,7 @@ func getStaticPath() (string, error) {
} }
func main() { func main() {
dir, err := getStaticPath() dir, err := staticPath()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -155,7 +155,7 @@
} }
function getProfile() { function getProfile() {
return localStorage.profile || '{}'; return JSON.parse(localStorage.profile || '{}');
} }
window.onpopstate = function(state) { window.onpopstate = function(state) {