Skip to content

Commit

Permalink
Nicer YAML syntax for multi-SQL backends
Browse files Browse the repository at this point in the history
  • Loading branch information
tdammers committed Jun 26, 2017
1 parent 1c2fe37 commit e10402a
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/Web/Sprinkles/Backends/Spec.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-#LANGUAGE NoImplicitPrelude #-}
{-#LANGUAGE OverloadedStrings #-}
{-#LANGUAGE OverloadedLists #-}
{-#LANGUAGE TypeFamilies #-}
{-#LANGUAGE MultiParamTypeClasses #-}
{-#LANGUAGE FlexibleInstances #-}
Expand Down Expand Up @@ -232,18 +233,38 @@ backendSpecFromJSON (Object obj) = do
query <- obj .: "query"
params <- obj .:? "params" .!= []
return (SqlBackend dsn query params, FetchAll)
Just queries' -> do
queries <- parseJSON queries'
Just (Array queries') -> do
queries <- forM (toList queries') $ \case
String queryStr -> do
return (queryStr, [])
Object queriesObj -> do
query <- queriesObj .: "query"
params <- queriesObj .:? "params" .!= []
return (query, params)
Array queriesArr -> do
case toList queriesArr of
[] ->
fail "Invalid query object, empty array is not allowed"
[String queryStr] ->
return (queryStr, [])
[String queryStr, Array params] ->
(queryStr,) <$> mapM parseJSON (toList params)
(String queryStr:params) ->
(queryStr,) <$> mapM parseJSON params
x ->
fail "Invalid query object, first array element must be string"
x -> fail "Invalid query object, must be array, string, or object"
mode <- obj .:? "results" .!= ResultsMerge
return (SqlMultiBackend dsn mode queries, FetchAll)
Just x -> fail "Invalid queries object, must be array"
parseSubprocessSpec = do
rawCmd <- obj .: "cmd"
t <- fromString <$> (obj .:? "mime-type" .!= "text/plain")
case rawCmd of
String cmd -> return (SubprocessBackend cmd [] t, FetchOne)
Array v -> parseJSON rawCmd >>= \case
cmd:args -> return (SubprocessBackend cmd args t, FetchOne)
[] -> fail "Expected a command and a list of arguments"
_ -> fail "Expected a command and a list of arguments"
x -> fail $ "Expected string or array, but found " ++ show x
parseLiteralBackendSpec = do
b <- obj .:? "body" .!= JSON.Null
Expand Down

0 comments on commit e10402a

Please sign in to comment.