Skip to content

Commit 42780a6

Browse files
committed
Use to translate attrs to types
1 parent a943e83 commit 42780a6

File tree

3 files changed

+7
-14
lines changed

3 files changed

+7
-14
lines changed

spec/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ queryTests =
341341
["/wp/v2/posts"]
342342
"<wpPosts offset=1 limit=1></wpPosts>" `shouldQueryTo`
343343
["/wp/v2/posts?offset=1"]
344-
"<wpPosts offset=0& limit=1></wpPosts>" `shouldQueryTo`
344+
"<wpPosts offset=0 limit=1></wpPosts>" `shouldQueryTo`
345345
["/wp/v2/posts?offset=0"]
346346
"<wpPosts limit=10 page=1></wpPosts>" `shouldQueryTo`
347347
["/wp/v2/posts?page=1"]

src/Web/Offset/Splices.hs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -346,30 +346,23 @@ parseQueryNode attrs =
346346
, qnum = perpage
347347
, qoffset = readLookup "offset" attrs
348348
, qpage = readLookup "page" attrs
349-
, qorder = toWPOrdering $ lookup "order" attrs
349+
, qorder = readLookup "order" attrs
350350
, qorderby = lookup "orderby" attrs
351351
, qsearch = lookup "search" attrs
352352
, qbefore = readLookup "before" attrs
353353
, qafter = readLookup "after" attrs
354-
, qstatus = toWPStatus $ lookup "status" attrs
354+
, qstatus = readLookup "status" attrs
355355
, qsticky = readLookup "sticky" attrs
356356
, quser = lookup "user" attrs
357357
, qtaxes = filterTaxonomies attrs }
358-
where readLookup n attrs = readSafe =<< lookup n attrs
358+
where -- `toTitle` allows us to use the standard Read instance to, e.g.,
359+
-- translate the text "asc" to the type constructor `Asc`
360+
readLookup n attrs = (readSafe . T.toTitle) =<< lookup n attrs
359361
perpage =
360362
case readLookup "per-page" attrs of
361363
Just n -> Just n
362364
Nothing -> readLookup "num" attrs
363365

364-
toWPOrdering :: Maybe Text -> Maybe WPOrdering
365-
toWPOrdering (Just "asc") = Just Asc
366-
toWPOrdering (Just "desc") = Just Desc
367-
toWPOrdering _ = Nothing
368-
369-
toWPStatus :: Maybe Text -> Maybe WPPostStatus
370-
toWPStatus (Just status) = readSafe (T.toTitle status)
371-
toWPStatus _ = Nothing
372-
373366
listOfFilters = ["limit"
374367
, "num"
375368
, "offset"

src/Web/Offset/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ data WPQuery = WPPostsQuery{ qlimit :: Maybe Int
254254
, qtaxes :: [TaxSpecList]
255255
} deriving (Show)
256256

257-
data WPOrdering = Asc | Desc deriving (Eq, Show, Ord)
257+
data WPOrdering = Asc | Desc deriving (Eq, Show, Read, Ord)
258258

259259
data WPPostStatus = Publish | Future | Draft | Pending | Private deriving (Eq, Show, Read, Ord)
260260

0 commit comments

Comments
 (0)