-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Aeson Value extractor functions into a separate module
- Loading branch information
1 parent
8d78843
commit 976ca7e
Showing
3 changed files
with
84 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
module Aeson ( | ||
asInt, | ||
asText, | ||
asDouble, | ||
asArray | ||
) where | ||
|
||
|
||
import Data.Aeson (Value (..)) | ||
import Data.Maybe (fromJust) | ||
import qualified Data.Scientific as S | ||
import qualified Data.Text as T | ||
import qualified Data.Text.Read as T | ||
import qualified Data.Vector as V | ||
|
||
-- | Try to extract an Int from Value | ||
-- | ||
-- >>> asInt (Number 10) | ||
-- 10 | ||
-- | ||
-- >>> asInt (String "10") | ||
-- 10 | ||
-- | ||
-- >>> asInt (String "foo") | ||
-- *** Exception: Expected an integer, but received: foo | ||
-- ... | ||
asInt :: Value -> Int | ||
asInt (Number n) = fromJust $ S.toBoundedInteger n | ||
asInt (String s) = | ||
case T.decimal s of | ||
(Right (n, _)) -> n | ||
(Left _) -> error $ "Expected an integer, but received: " <> T.unpack s | ||
asInt o = error $ "Expected an integer but received: " <> show o | ||
|
||
|
||
-- | Try to extract a Double from Value | ||
-- | ||
-- >>> asDouble (Number 10.3) | ||
-- 10.3 | ||
-- | ||
-- >>> asDouble (String "10.5") | ||
-- 10.5 | ||
-- | ||
-- >>> asDouble (String "foo") | ||
-- *** Exception: Expected a double, but received: foo | ||
-- ... | ||
asDouble :: Value -> Double | ||
asDouble (Number n) = S.toRealFloat n | ||
asDouble (String s) = | ||
case T.double s of | ||
(Right (n, _)) -> n | ||
(Left _) -> error $ "Expected a double, but received: " <> T.unpack s | ||
asDouble o = error $ "Expected a double, but received: " <> show o | ||
|
||
|
||
asArray :: Value -> V.Vector Value | ||
asArray (Array x) = x | ||
asArray o = error $ "Expected an array, but received: " <> show o | ||
|
||
|
||
-- | Try to extract a Text from Value | ||
-- | ||
-- >>> asText (String "foo") | ||
-- "foo" | ||
-- | ||
-- >>> asText (Number 10.3) | ||
-- "10.3" | ||
asText :: Value -> T.Text | ||
asText (String t) = t | ||
asText (Number n) = T.pack $ show n | ||
asText o = error $ "Expected a string, but received: " <> show o |
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 |
---|---|---|
|
@@ -5,5 +5,6 @@ main :: IO () | |
main = doctest | ||
[ "-isrc" | ||
, "src/Expr.hs" | ||
, "src/Aeson.hs" | ||
, "app/Main.hs" | ||
] |