From 8e8796993334dd8f3ee3ff4eee4d2e8a5fdca9a7 Mon Sep 17 00:00:00 2001 From: Michael Baird Date: Thu, 29 Jun 2017 08:52:18 -0500 Subject: [PATCH] Input definition GET and DELETE handle the case when the definition does not exist --- handler.go | 21 +++++++++++++-------- handler_test.go | 13 +++++++++++-- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/handler.go b/handler.go index ed34f19d0..63ecffac0 100644 --- a/handler.go +++ b/handler.go @@ -1566,8 +1566,13 @@ func (h *Handler) handleGetInputDefinition(w http.ResponseWriter, r *http.Reques return } - inputDef, _ := index.inputDefinitions[inputDefName] - if err := json.NewEncoder(w).Encode(InputDefinitionInfo{ + inputDef, err := index.InputDefinition(inputDefName) + if err != nil { + http.Error(w, err.Error(), http.StatusNotFound) + return + } + + if err = json.NewEncoder(w).Encode(InputDefinitionInfo{ Frames: inputDef.frames, Fields: inputDef.fields, }); err != nil { @@ -1629,7 +1634,7 @@ func (h *Handler) handlePostInput(w http.ResponseWriter, r *http.Request) { return } for _, req := range reqs { - bits, err := h.InputJsonDataParser(req.(map[string]interface{}), index, inputDefName) + bits, err := h.InputJSONDataParser(req.(map[string]interface{}), index, inputDefName) if err == ErrInputDefinitionNotFound { http.Error(w, err.Error(), http.StatusNotFound) return @@ -1650,11 +1655,11 @@ func (h *Handler) handlePostInput(w http.ResponseWriter, r *http.Request) { } } -// InputJsonDataParser validate input json file and execute SetBit -func (h *Handler) InputJsonDataParser(req map[string]interface{}, index *Index, name string) (map[string][]*Bit, error) { - inputDef := index.inputDefinition(name) - if inputDef == nil { - return nil, ErrInputDefinitionNotFound +// InputJSONDataParser validate input json file and execute SetBit +func (h *Handler) InputJSONDataParser(req map[string]interface{}, index *Index, name string) (map[string][]*Bit, error) { + inputDef, err := index.InputDefinition(name) + if err != nil { + return nil, err } // if field in input data is not in defined definition, return error var columnLabel string diff --git a/handler_test.go b/handler_test.go index e47e04cc0..a88ab1f64 100644 --- a/handler_test.go +++ b/handler_test.go @@ -1253,8 +1253,10 @@ func TestHandler_DeleteInputDefinition(t *testing.T) { t.Fatalf("unexpected status code: %d", w.Code) } else if body := w.Body.String(); body != `{}`+"\n" { t.Fatalf("unexpected body: %s", body) - } else if index.InputDefinition("test") != nil { - t.Fatalf("unexpected result: %v", index.InputDefinition("test")) + } + _, err = index.InputDefinition("test") + if err != pilosa.ErrInputDefinitionNotFound { + t.Fatal(err) } } @@ -1299,6 +1301,13 @@ func TestHandler_GetInputDefinition(t *testing.T) { } else if body := w.Body.String(); body != string(expect)+"\n" { t.Fatalf("unexpected body: %s, expect: %s", body, string(expect)) } + + // Check non existant definition + w = httptest.NewRecorder() + h.ServeHTTP(w, MustNewHTTPRequest("GET", "/index/i0/input-definition/foo", strings.NewReader(""))) + if w.Code != http.StatusNotFound { + t.Fatalf("unexpected status code: %d", w.Code) + } } var defaultBody = `