on master help sign in
Add decodeThrow, decodeFileThrow helper functions
commitc3d043b176eb435f1b88d42c18af7e90acf0635b authorDaniel Firth <locallycompact@gmail.com> dateMon 25 Jun 2018, 14:01 (8 years ago) parentb981f3d
3 files changed+21−1
ChangeLog.md+4 Data/Yaml.hs+16 yaml.cabal+1−1
ChangeLog.md+4
@@ -1,3 +1,7 @@
1+## 0.8.31
2+
3+* Add `decodeThrow` and `decodeFileThrow` convenience functions.
4+
15## 0.8.30
26
37* Removed `AppSettings` mentioned in `loadYamlSettings` error message.
Data/Yaml.hs+16
@@ -64,6 +64,8 @@ module Data.Yaml
6464 , decodeEither
6565 , decodeEither'
6666 , decodeFileEither
67+ , decodeThrow
68+ , decodeFileThrow
6769 -- ** More control over decoding
6870 , decodeHelper
6971 ) where
@@ -71,6 +73,8 @@ module Data.Yaml
7173import Control.Applicative((<$>))
7274#endif
7375import Control.Exception
76+import Control.Monad.IO.Class (MonadIO, liftIO)
77+import Control.Monad.Trans.Resource (MonadThrow, throwM)
7478import Data.Aeson
7579 ( Value (..), ToJSON (..), FromJSON (..), object
7680 , (.=) , (.:) , (.:?) , (.!=)
@@ -190,6 +194,18 @@ decodeEither' = either Left (either (Left . AesonException) Right)
190194 . decodeHelper
191195 . Y.decode
192196
197+-- | A version of 'decodeEither'' lifted to MonadThrow
198+--
199+-- Since 0.8.31
200+decodeThrow :: (MonadThrow m, FromJSON a) => ByteString -> m a
201+decodeThrow = either throwM return . decodeEither'
202+
203+-- | A version of 'decodeFileEither' lifted to MonadIO
204+--
205+-- Since 0.8.31
206+decodeFileThrow :: (MonadIO m, FromJSON a) => FilePath -> m a
207+decodeFileThrow f = liftIO $ decodeFileEither f >>= either throwIO return
208+
193209array :: [Value] -> Value
194210array = Array . V.fromList
195211
yaml.cabal+1−1
@@ -1,5 +1,5 @@
11name: yaml
2−version: 0.8.30
2+version: 0.8.31
33license: BSD3
44license-file: LICENSE
55author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov