From d8b7332acc83216c7f2f5f4363789be8ccbb1fc7 Mon Sep 17 00:00:00 2001 From: Ruslan Talpa Date: Mon, 26 Oct 2015 15:27:28 +0200 Subject: [PATCH] Code cleanup (lint suggestions) --- src/PostgREST/App.hs | 24 ++++++++++++++---------- test/Feature/InsertSpec.hs | 2 +- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 4b1358747..6a49cd06f 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -78,10 +78,9 @@ app dbstructure conf reqBody req = wrapQuery qs [ (if hasPrefer "count=none" then countNoneF else countAllF), countF, - (case contentType of + case contentType of "text/csv" -> asCsvF -- TODO check when in csv mode if the header is correct when requesting nested data _ -> asJsonF - ) ] range ) V.empty True @@ -120,7 +119,7 @@ app dbstructure conf reqBody req = q = B.Stmt ( wrapQuery qs [ - (if isSingle then locationF pKeys else "null"), + if isSingle then locationF pKeys else "null", "null", -- countF, ( if echoRequested @@ -368,6 +367,7 @@ parsePostRequest rootTableName httpRequest reqBody = --rootTableName = cs $ head $ pathInfo httpRequest -- TODO unsafe head isCsv = lookupHeader "Content-Type" == Just csvMT + parseRequestBody :: Bool -> BL.ByteString -> Either Text ([Text],[[Value]]) parseRequestBody isCsv reqBody = first cs $ checkStructure =<< @@ -379,13 +379,17 @@ parseRequestBody isCsv reqBody = first cs $ else eitherDecode reqBody >>= convertJson where checkStructure :: ([Text], [[Value]]) -> Either String ([Text], [[Value]]) - checkStructure v = - if headerMatchesContent v - then Right v - else - if isCsv - then Left "CSV header does not match rows length" - else Left "The number of keys in objects do not match" + checkStructure v + | headerMatchesContent v = Right v + | isCsv = Left "CSV header does not match rows length" + | otherwise = Left "The number of keys in objects do not match" + -- checkStructure v = + -- if headerMatchesContent v + -- then Right v + -- else + -- if isCsv + -- then Left "CSV header does not match rows length" + -- else Left "The number of keys in objects do not match" headerMatchesContent :: ([Text], [[Value]]) -> Bool headerMatchesContent (header, vals) = all ( (headerLength ==) . length) vals diff --git a/test/Feature/InsertSpec.hs b/test/Feature/InsertSpec.hs index 51db4fb56..c0775150b 100644 --- a/test/Feature/InsertSpec.hs +++ b/test/Feature/InsertSpec.hs @@ -172,7 +172,7 @@ spec = afterAll_ resetDb $ around withApp $ do -- , matchHeaders = ["Content-Type" <:> "application/json", -- "Location" <:> "/no_pk?a=is.null&b=eq.foo"] -- } - it "can post nulls" $ do + it "can post nulls" $ request methodPost "/no_pk" [("Content-Type", "text/csv"), ("Accept", "text/csv"), ("Prefer", "return=representation")] "a,b\nNULL,foo"