forked from jaspervdj/digestive-functors
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add another abstraction layer for HTTP input
- Loading branch information
Showing
6 changed files
with
104 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
module Text.Digestive.Http | ||
( HttpInput (..) | ||
, inputString | ||
, inputRead | ||
, inputBool | ||
, inputChoice | ||
) where | ||
|
||
import Control.Applicative ((<$>)) | ||
import Control.Monad (mplus) | ||
import Data.Monoid (Monoid, mconcat) | ||
import Data.Maybe (fromMaybe) | ||
|
||
import Text.Digestive.Common | ||
import Text.Digestive.Types | ||
import Text.Digestive.Result | ||
import Text.Digestive.Transform | ||
|
||
class HttpInput a where | ||
getInputString :: a -> String | ||
|
||
inputString :: (Monad m, Functor m, HttpInput i) | ||
=> (FormId -> Maybe String -> v) -- ^ View constructor | ||
-> Maybe String -- ^ Default value | ||
-> Form m i e v String -- ^ Resulting form | ||
inputString = input toView toResult | ||
where | ||
toView _ inp defaultInput = fmap getInputString inp `mplus` defaultInput | ||
toResult = const . Ok . fromMaybe "" . fmap getInputString | ||
|
||
inputRead :: (Monad m, Functor m, HttpInput i, Read a, Show a) | ||
=> (FormId -> Maybe String -> v) -- ^ View constructor | ||
-> e -- ^ Error when no read | ||
-> Maybe a -- ^ Default input | ||
-> Form m i e v a -- ^ Resulting form | ||
inputRead cons' error' def = inputString cons' (fmap show def) | ||
`transform` transformRead error' | ||
|
||
inputBool :: (Monad m, Functor m, HttpInput i) | ||
=> (FormId -> Bool -> v) -- ^ View constructor | ||
-> Bool -- ^ Default input | ||
-> Form m i e v Bool -- ^ Resulting form | ||
inputBool = input toView toResult | ||
where | ||
toView isInput inp def = if isInput then readBool inp else def | ||
toResult inp _ = Ok $ readBool inp | ||
readBool (Just x) = not (null $ getInputString x) | ||
readBool Nothing = False | ||
|
||
inputChoice :: (Monad m, Functor m, HttpInput i, Monoid v, Eq a) | ||
=> (FormId -> String -> Bool -> a -> v) -- ^ Choice constructor | ||
-> a -- ^ Default option | ||
-> [a] -- ^ Choices | ||
-> Form m i e v a -- ^ Resulting form | ||
inputChoice toView defaultInput choices = Form $ do | ||
inputKey <- fromMaybe "" . fmap getInputString <$> getFormInput | ||
id' <- getFormId | ||
let -- Find the actual input, based on the key, or use the default input | ||
inp = fromMaybe defaultInput $ lookup inputKey $ zip (ids id') choices | ||
-- Apply the toView' function to all choices | ||
view' = mconcat $ zipWith (toView' id' inp) (ids id') choices | ||
return (View (const view'), Ok inp) | ||
where | ||
ids id' = map (((show id' ++ "-") ++) . show) [1 .. length choices] | ||
toView' id' inp key x = toView id' key (inp == x) x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters