Skip to content

Commit

Permalink
remove switch in sendJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Jun 3, 2022
1 parent e02e277 commit 8bab889
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
7 changes: 0 additions & 7 deletions api/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ func getJSON(req *http.Request, v interface{}) error {
func sendJSON(ctx context.Context, rw http.ResponseWriter, v interface{}, code int) {
rw.Header().Set("Content-Type", "application/json")

switch val := v.(type) {
case map[string]interface{}:
v = dataResponse{
Data: val,
}
}

b, err := json.Marshal(v)
if err != nil {
log.Error(ctx, fmt.Sprintf("Error while encoding JSON: %v", err))
Expand Down
32 changes: 24 additions & 8 deletions api/http/handlerfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,22 @@ func rootHandler(rw http.ResponseWriter, req *http.Request) {
sendJSON(
req.Context(),
rw,
map[string]interface{}{"response": "Welcome to the DefraDB HTTP API. Use /graphql to send queries to the database"},
dataResponse{
Data: map[string]interface{}{
"response": "Welcome to the DefraDB HTTP API. Use /graphql to send queries to the database",
},
},
http.StatusOK,
)
}

func pingHandler(rw http.ResponseWriter, req *http.Request) {
sendJSON(req.Context(), rw, map[string]interface{}{"response": "pong"}, http.StatusOK)
sendJSON(
req.Context(),
rw,
dataResponse{map[string]interface{}{"response": "pong"}},
http.StatusOK,
)
}

func dumpHandler(rw http.ResponseWriter, req *http.Request) {
Expand All @@ -51,7 +60,12 @@ func dumpHandler(rw http.ResponseWriter, req *http.Request) {
}
db.PrintDump(req.Context())

sendJSON(req.Context(), rw, map[string]interface{}{"response": "ok"}, http.StatusOK)
sendJSON(
req.Context(),
rw,
dataResponse{map[string]interface{}{"response": "ok"}},
http.StatusOK,
)
}

type gqlRequest struct {
Expand Down Expand Up @@ -137,7 +151,7 @@ func loadSchemaHandler(rw http.ResponseWriter, req *http.Request) {
sendJSON(
req.Context(),
rw,
map[string]interface{}{"result": "success"},
dataResponse{map[string]interface{}{"result": "success"}},
http.StatusBadRequest,
)
}
Expand Down Expand Up @@ -200,10 +214,12 @@ func getBlockHandler(rw http.ResponseWriter, req *http.Request) {
sendJSON(
req.Context(),
rw,
map[string]interface{}{
"block": string(buf),
"delta": string(data),
"val": delta.Value(),
dataResponse{
Data: map[string]interface{}{
"block": string(buf),
"delta": string(data),
"val": delta.Value(),
},
},
http.StatusOK,
)
Expand Down

0 comments on commit 8bab889

Please sign in to comment.