on master help sign in
Merge pull request #73 from xkollar/minor-changes
a440479Michael Snoyman
Data/Yaml.hs+17−24
@@ -1,5 +1,4 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2−{-# LANGUAGE PatternGuards #-}
3{-# LANGUAGE ScopedTypeVariables #-}
2{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE OverloadedStrings #-}
5-- | Provides a high-level interface for processing YAML files.
4-- | Provides a high-level interface for processing YAML files.
@@ -53,35 +52,33 @@ module Data.Yaml
53 -- ** More control over decoding
52 -- ** More control over decoding
54 , decodeHelper
53 , decodeHelper
55 ) where
54 ) where
56−
55+#if !MIN_VERSION_base(4,8,0)
57−import qualified Text.Libyaml as Y
56+import Control.Applicative((<$>))
57+#endif
58+import Control.Exception
59+import Control.Monad.Trans.Resource (runResourceT)
58import Data.Aeson
60import Data.Aeson
59 ( Value (..), ToJSON (..), FromJSON (..), object
61 ( Value (..), ToJSON (..), FromJSON (..), object
60 , (.=) , (.:) , (.:?) , (.!=)
62 , (.=) , (.:) , (.:?) , (.!=)
61 , Object, Array
63 , Object, Array
62 )
64 )
65+import Data.Aeson.Encode (encodeToTextBuilder)
63import Data.Aeson.Types (Pair, parseMaybe, parseEither, Parser)
66import Data.Aeson.Types (Pair, parseMaybe, parseEither, Parser)
64−import Text.Libyaml hiding (encode, decode, encodeFile, decodeFile)
65import Data.ByteString (ByteString)
67import Data.ByteString (ByteString)
66−import System.IO.Unsafe (unsafePerformIO)
67−import Control.Exception
68import qualified Data.Conduit as C
68import qualified Data.Conduit as C
69import qualified Data.Conduit.List as CL
69import qualified Data.Conduit.List as CL
70−import qualified Data.Vector as V
71−import Data.Text.Encoding (encodeUtf8)
72import qualified Data.HashMap.Strict as M
70import qualified Data.HashMap.Strict as M
73import qualified Data.HashSet as HashSet
71import qualified Data.HashSet as HashSet
74−#if MIN_VERSION_aeson(0, 7, 0)
72+import Data.Text.Encoding (encodeUtf8)
75import qualified Data.Text.Encoding as TE
73import qualified Data.Text.Encoding as TE
76import qualified Data.Text.Lazy as TL
74import qualified Data.Text.Lazy as TL
77import Data.Text.Lazy.Builder (toLazyText)
75import Data.Text.Lazy.Builder (toLazyText)
78−import Data.Aeson.Encode (encodeToTextBuilder)
79−#else
80−import qualified Data.ByteString.Char8 as S8
76+import qualified Data.Vector as V
81−#endif
77+import System.IO.Unsafe (unsafePerformIO)
82−import Control.Monad.Trans.Resource (runResourceT)
83
78
84import Data.Yaml.Internal
79import Data.Yaml.Internal
80+import Text.Libyaml hiding (encode, decode, encodeFile, decodeFile)
81+import qualified Text.Libyaml as Y
85
82
86encode :: ToJSON a => a -> ByteString
83encode :: ToJSON a => a -> ByteString
87encode obj = unsafePerformIO $
84encode obj = unsafePerformIO $
@@ -110,10 +107,10 @@ objToEvents' :: Value -> [Y.Event] -> [Y.Event]
110--objToEvents' (Scalar s) rest = scalarToEvent s : rest
107--objToEvents' (Scalar s) rest = scalarToEvent s : rest
111objToEvents' (Array list) rest =
108objToEvents' (Array list) rest =
112 EventSequenceStart Nothing
109 EventSequenceStart Nothing
113− : foldr ($) (EventSequenceEnd : rest) (map objToEvents' $ V.toList list)
110+ : foldr objToEvents' (EventSequenceEnd : rest) (V.toList list)
114objToEvents' (Object pairs) rest =
111objToEvents' (Object pairs) rest =
115 EventMappingStart Nothing
112 EventMappingStart Nothing
116− : foldr ($) (EventMappingEnd : rest) (map pairToEvents $ M.toList pairs)
113+ : foldr pairToEvents (EventMappingEnd : rest) (M.toList pairs)
117
114
118-- Empty strings need special handling to ensure they get quoted. This avoids:
115-- Empty strings need special handling to ensure they get quoted. This avoids:
119-- https://github.com/snoyberg/yaml/issues/24
116-- https://github.com/snoyberg/yaml/issues/24
@@ -130,12 +127,8 @@ objToEvents' (String s) rest =
130objToEvents' Null rest = EventScalar "null" NullTag PlainNoTag Nothing : rest
127objToEvents' Null rest = EventScalar "null" NullTag PlainNoTag Nothing : rest
131objToEvents' (Bool True) rest = EventScalar "true" BoolTag PlainNoTag Nothing : rest
128objToEvents' (Bool True) rest = EventScalar "true" BoolTag PlainNoTag Nothing : rest
132objToEvents' (Bool False) rest = EventScalar "false" BoolTag PlainNoTag Nothing : rest
129objToEvents' (Bool False) rest = EventScalar "false" BoolTag PlainNoTag Nothing : rest
133−#if MIN_VERSION_aeson(0,7,0)
134-- Use aeson's implementation which gets rid of annoying decimal points
130-- Use aeson's implementation which gets rid of annoying decimal points
135objToEvents' n@Number{} rest = EventScalar (TE.encodeUtf8 $ TL.toStrict $ toLazyText $ encodeToTextBuilder n) IntTag PlainNoTag Nothing : rest
131objToEvents' n@Number{} rest = EventScalar (TE.encodeUtf8 $ TL.toStrict $ toLazyText $ encodeToTextBuilder n) IntTag PlainNoTag Nothing : rest
136−#else
137−objToEvents' (Number n) rest = EventScalar (S8.pack $ show n) IntTag PlainNoTag Nothing : rest
138−#endif
139
132
140pairToEvents :: Pair -> [Y.Event] -> [Y.Event]
133pairToEvents :: Pair -> [Y.Event] -> [Y.Event]
141pairToEvents (k, v) rest =
134pairToEvents (k, v) rest =
@@ -146,8 +139,8 @@ decode :: FromJSON a
146 => ByteString
139 => ByteString
147 -> Maybe a
140 -> Maybe a
148decode bs = unsafePerformIO
141decode bs = unsafePerformIO
149− $ fmap (either (const Nothing) id)
142+ $ either (const Nothing) id
150− $ decodeHelper_ (Y.decode bs)
143+ <$> decodeHelper_ (Y.decode bs)
151
144
152decodeFile :: FromJSON a
145decodeFile :: FromJSON a
153 => FilePath
146 => FilePath
@@ -165,8 +158,8 @@ decodeFileEither = decodeHelper_ . Y.decodeFile
165
158
166decodeEither :: FromJSON a => ByteString -> Either String a
159decodeEither :: FromJSON a => ByteString -> Either String a
167decodeEither bs = unsafePerformIO
160decodeEither bs = unsafePerformIO
168− $ fmap (either (Left . prettyPrintParseException) id)
161+ $ either (Left . prettyPrintParseException) id
169− $ decodeHelper (Y.decode bs)
162+ <$> decodeHelper (Y.decode bs)
170
163
171-- | More helpful version of 'decodeEither' which returns the 'YamlException'.
164-- | More helpful version of 'decodeEither' which returns the 'YamlException'.
172--
165--
Data/Yaml/Builder.hs+15−29
@@ -18,30 +18,26 @@ module Data.Yaml.Builder
18 , (.=)
18 , (.=)
19 ) where
19 ) where
20
20
21−import Data.Conduit
21+import Prelude hiding (null)
22−import Data.ByteString (ByteString)
22+
23−import Text.Libyaml
23+import Control.Arrow (second)
24−import Data.Yaml.Internal
24+import Control.Monad.Trans.Resource (runResourceT)
25−import Data.Text (Text)
25+import Data.Aeson.Encode (encodeToTextBuilder)
26−#if MIN_VERSION_aeson(0, 7, 0)
27−import Data.Scientific (Scientific)
28import Data.Aeson.Types (Value(..))
26import Data.Aeson.Types (Value(..))
29−#endif
27+import Data.ByteString (ByteString)
28+import qualified Data.ByteString.Char8 as S8
29+import Data.Conduit
30import qualified Data.HashSet as HashSet
30import qualified Data.HashSet as HashSet
31+import Data.Scientific (Scientific)
32+import Data.Text (Text)
31import Data.Text.Encoding (encodeUtf8)
33import Data.Text.Encoding (encodeUtf8)
32−import System.IO.Unsafe (unsafePerformIO)
33−import Control.Arrow (second)
34−import qualified Data.ByteString.Char8 as S8
35−import Control.Monad.Trans.Resource (runResourceT)
36−#if MIN_VERSION_aeson(0, 7, 0)
37import qualified Data.Text.Encoding as TE
34import qualified Data.Text.Encoding as TE
38import qualified Data.Text.Lazy as TL
35import qualified Data.Text.Lazy as TL
39import Data.Text.Lazy.Builder (toLazyText)
36import Data.Text.Lazy.Builder (toLazyText)
40−import Data.Aeson.Encode (encodeToTextBuilder)
37+import System.IO.Unsafe (unsafePerformIO)
41−#else
38+
42−import Data.Attoparsec.Number
39+import Data.Yaml.Internal
43−#endif
40+import Text.Libyaml
44−import Prelude hiding (null)
45
41
46(.=) :: ToYaml a => Text -> a -> (Text, YamlBuilder)
42(.=) :: ToYaml a => Text -> a -> (Text, YamlBuilder)
47k .= v = (k, toYaml v)
43k .= v = (k, toYaml v)
@@ -73,7 +69,7 @@ array :: [YamlBuilder] -> YamlBuilder
73array bs =
69array bs =
74 YamlBuilder $ (EventSequenceStart Nothing:) . flip (foldr go) bs . (EventSequenceEnd:)
70 YamlBuilder $ (EventSequenceStart Nothing:) . flip (foldr go) bs . (EventSequenceEnd:)
75 where
71 where
76− go (YamlBuilder b) rest = b rest
72+ go (YamlBuilder b) = b
77
73
78string :: Text -> YamlBuilder
74string :: Text -> YamlBuilder
79-- Empty strings need special handling to ensure they get quoted. This avoids:
75-- Empty strings need special handling to ensure they get quoted. This avoids:
@@ -89,22 +85,12 @@ string s =
89 | otherwise = EventScalar (encodeUtf8 s) StrTag PlainNoTag Nothing
85 | otherwise = EventScalar (encodeUtf8 s) StrTag PlainNoTag Nothing
90
86
91-- Use aeson's implementation which gets rid of annoying decimal points
87-- Use aeson's implementation which gets rid of annoying decimal points
92−#if MIN_VERSION_aeson(0, 7, 0)
93scientific :: Scientific -> YamlBuilder
88scientific :: Scientific -> YamlBuilder
94scientific n = YamlBuilder (EventScalar (TE.encodeUtf8 $ TL.toStrict $ toLazyText $ encodeToTextBuilder (Number n)) IntTag PlainNoTag Nothing :)
89scientific n = YamlBuilder (EventScalar (TE.encodeUtf8 $ TL.toStrict $ toLazyText $ encodeToTextBuilder (Number n)) IntTag PlainNoTag Nothing :)
95−#else
96−scientific :: Number -> YamlBuilder
97−scientific n = YamlBuilder (EventScalar (S8.pack $ show n) IntTag PlainNoTag Nothing :)
98−#endif
99
90
100{-# DEPRECATED number "Use scientific" #-}
91{-# DEPRECATED number "Use scientific" #-}
101−#if MIN_VERSION_aeson(0,7,0)
102number :: Scientific -> YamlBuilder
92number :: Scientific -> YamlBuilder
103number = scientific
93number = scientific
104−#else
105−number :: Number -> YamlBuilder
106−number n = YamlBuilder (EventScalar (S8.pack $ show n) IntTag PlainNoTag Nothing :)
107−#endif
108
94
109bool :: Bool -> YamlBuilder
95bool :: Bool -> YamlBuilder
110bool True = YamlBuilder (EventScalar "true" BoolTag PlainNoTag Nothing :)
96bool True = YamlBuilder (EventScalar "true" BoolTag PlainNoTag Nothing :)
Data/Yaml/Include.hs+6−5
@@ -1,17 +1,18 @@
1{-# LANGUAGE RankNTypes #-}
1{-# LANGUAGE RankNTypes #-}
2module Data.Yaml.Include (decodeFile, decodeFileEither) where
2module Data.Yaml.Include (decodeFile, decodeFileEither) where
3
3
4−import Control.Monad (when)
5import Control.Exception (throwIO)
4import Control.Exception (throwIO)
5+import Control.Monad (when)
6import Control.Monad.IO.Class (liftIO)
6import Control.Monad.IO.Class (liftIO)
7import Control.Monad.Trans.Resource (MonadResource)
7import Control.Monad.Trans.Resource (MonadResource)
8−import System.Directory
9−import System.FilePath
10−import Data.Text (unpack)
11−import Data.Text.Encoding (decodeUtf8)
12import Data.Aeson (FromJSON)
8import Data.Aeson (FromJSON)
13import Data.Conduit
9import Data.Conduit
14import qualified Data.Conduit.List as CL
10import qualified Data.Conduit.List as CL
11+import Data.Text (unpack)
12+import Data.Text.Encoding (decodeUtf8)
13+import System.Directory
14+import System.FilePath
15+
15import Data.Yaml.Internal (ParseException(..), decodeHelper_, decodeHelper)
16import Data.Yaml.Internal (ParseException(..), decodeHelper_, decodeHelper)
16import Text.Libyaml hiding (decodeFile)
17import Text.Libyaml hiding (decodeFile)
17import qualified Text.Libyaml as Y
18import qualified Text.Libyaml as Y
Data/Yaml/Internal.hs+22−35
@@ -13,40 +13,36 @@ module Data.Yaml.Internal
13 , isNumeric
13 , isNumeric
14 ) where
14 ) where
15
15
16−import qualified Text.Libyaml as Y
16+#if !MIN_VERSION_base(4,8,0)
17−import Data.Aeson
17+import Control.Applicative ((<$>), Applicative(..))
18−import Data.Aeson.Types hiding (parse)
18+#endif
19−import Text.Libyaml hiding (encode, decode, encodeFile, decodeFile)
20−import Data.ByteString (ByteString)
21−import qualified Data.Map as Map
22import Control.Exception
19import Control.Exception
23import Control.Exception.Enclosed
20import Control.Exception.Enclosed
21+import Control.Monad (liftM, ap, unless)
22+import Control.Monad.IO.Class (MonadIO, liftIO)
23+import Control.Monad.Trans.Class (MonadTrans, lift)
24+import Control.Monad.Trans.Resource (ResourceT, runResourceT)
24import Control.Monad.Trans.State
25import Control.Monad.Trans.State
26+import Data.Aeson
27+import Data.Aeson.Types hiding (parse)
28+import qualified Data.Attoparsec.Text as Atto
29+import Data.ByteString (ByteString)
30+import Data.Char (toUpper)
25import qualified Data.Conduit as C
31import qualified Data.Conduit as C
26import qualified Data.Conduit.List as CL
32import qualified Data.Conduit.List as CL
27−import Control.Monad.Trans.Class (MonadTrans, lift)
33+import qualified Data.HashMap.Strict as M
28−import Control.Monad.IO.Class (MonadIO, liftIO)
34+import qualified Data.HashSet as HashSet
29−import Control.Monad (liftM, ap)
35+import qualified Data.Map as Map
30−#if !MIN_VERSION_base(4,8,0)
36+import Data.Scientific (Scientific)
31−import Control.Applicative (Applicative(..))
32−#endif
33−import Data.Char (toUpper)
34−import qualified Data.Vector as V
35import Data.Text (Text, pack)
37import Data.Text (Text, pack)
36import qualified Data.Text as T
38import qualified Data.Text as T
37import Data.Text.Encoding (decodeUtf8With)
39import Data.Text.Encoding (decodeUtf8With)
38import Data.Text.Encoding.Error (lenientDecode)
40import Data.Text.Encoding.Error (lenientDecode)
39−import qualified Data.HashMap.Strict as M
40import Data.Typeable
41import Data.Typeable
41−#if MIN_VERSION_aeson(0, 7, 0)
42−import qualified Data.Attoparsec.Text as Atto
42+import qualified Data.Vector as V
43−import Data.Scientific (Scientific)
43+
44−#else
45−import Data.Text.Read
44+import qualified Text.Libyaml as Y
46−import Data.Attoparsec.Number
45+import Text.Libyaml hiding (encode, decode, encodeFile, decodeFile)
47−#endif
48−import Control.Monad.Trans.Resource (ResourceT, runResourceT)
49−import qualified Data.HashSet as HashSet
50
46
51data ParseException = NonScalarKey
47data ParseException = NonScalarKey
52 | UnknownAlias { _anchorName :: Y.AnchorName }
48 | UnknownAlias { _anchorName :: Y.AnchorName }
@@ -131,9 +127,7 @@ type Parse = StateT (Map.Map String Value) (ResourceT IO)
131requireEvent :: Event -> C.Sink Event Parse ()
127requireEvent :: Event -> C.Sink Event Parse ()
132requireEvent e = do
128requireEvent e = do
133 f <- CL.head
129 f <- CL.head
134− if f == Just e
135− then return ()
136− else liftIO $ throwIO $ UnexpectedEvent f $ Just e
130+ unless (f == Just e) $ liftIO $ throwIO $ UnexpectedEvent f $ Just e
137
131
138parse :: C.Sink Event Parse Value
132parse :: C.Sink Event Parse Value
139parse = do
133parse = do
@@ -163,26 +157,19 @@ textToValue _ _ t
163 | t `elem` ["null", "Null", "NULL", "~", ""] = Null
157 | t `elem` ["null", "Null", "NULL", "~", ""] = Null
164 | any (t `isLike`) ["y", "yes", "on", "true"] = Bool True
158 | any (t `isLike`) ["y", "yes", "on", "true"] = Bool True
165 | any (t `isLike`) ["n", "no", "off", "false"] = Bool False
159 | any (t `isLike`) ["n", "no", "off", "false"] = Bool False
166−#if MIN_VERSION_aeson(0, 7, 0)
167 | Right x <- textToScientific t = Number x
160 | Right x <- textToScientific t = Number x
168−#else
169− | Right (x, "") <- signed decimal t = Number $ I x
170− | Right (x, "") <- double t = Number $ D x
171−#endif
172 | otherwise = String t
161 | otherwise = String t
173 where x `isLike` ref = x `elem` [ref, T.toUpper ref, titleCased]
162 where x `isLike` ref = x `elem` [ref, T.toUpper ref, titleCased]
174 where titleCased = toUpper (T.head ref) `T.cons` T.tail ref
163 where titleCased = toUpper (T.head ref) `T.cons` T.tail ref
175
164
176−#if MIN_VERSION_aeson(0, 7, 0)
177textToScientific :: Text -> Either String Scientific
165textToScientific :: Text -> Either String Scientific
178textToScientific = Atto.parseOnly (Atto.scientific <* Atto.endOfInput)
166textToScientific = Atto.parseOnly (Atto.scientific <* Atto.endOfInput)
179−#endif
180
167
181parseO :: C.Sink Event Parse Value
168parseO :: C.Sink Event Parse Value
182parseO = do
169parseO = do
183 me <- CL.head
170 me <- CL.head
184 case me of
171 case me of
185− Just (EventScalar v tag style a) -> fmap (textToValue style tag) $ parseScalar v a style tag
172+ Just (EventScalar v tag style a) -> textToValue style tag <$> parseScalar v a style tag
186 Just (EventSequenceStart a) -> parseS a id
173 Just (EventSequenceStart a) -> parseS a id
187 Just (EventMappingStart a) -> parseM a M.empty
174 Just (EventMappingStart a) -> parseM a M.empty
188 Just (EventAlias an) -> do
175 Just (EventAlias an) -> do
Data/Yaml/Parser.hs+10−9
@@ -5,16 +5,13 @@
5-- drastically, or be entirely removed, in a future release.
5-- drastically, or be entirely removed, in a future release.
6module Data.Yaml.Parser where
6module Data.Yaml.Parser where
7
7
8−import Text.Libyaml
9import Control.Applicative
8import Control.Applicative
10−#if !MIN_VERSION_base(4,8,0)
9+import Control.Exception (Exception)
11−import Data.Monoid (Monoid (..))
12−#endif
13import Control.Monad (MonadPlus (..), liftM, ap)
10import Control.Monad (MonadPlus (..), liftM, ap)
14−import Control.Monad.Trans.Writer.Strict (tell, WriterT)
15import Control.Monad.Trans.Class (lift)
11import Control.Monad.Trans.Class (lift)
16import Control.Monad.Trans.Resource (MonadThrow, monadThrow, runResourceT)
12import Control.Monad.Trans.Resource (MonadThrow, monadThrow, runResourceT)
17−import qualified Data.Map as Map
13+import Control.Monad.Trans.Writer.Strict (tell, WriterT)
14+import Data.ByteString (ByteString)
18import Data.Conduit
15import Data.Conduit
19#if MIN_VERSION_conduit(1,1,0)
16#if MIN_VERSION_conduit(1,1,0)
20import Data.Conduit.Lift (runWriterC)
17import Data.Conduit.Lift (runWriterC)
@@ -22,12 +19,16 @@ import Data.Conduit.Lift (runWriterC)
22#else
19#else
23import Data.Conduit.Lift (runWriterSC)
20import Data.Conduit.Lift (runWriterSC)
24#endif
21#endif
22+import qualified Data.Map as Map
23+#if !MIN_VERSION_base(4,8,0)
24+import Data.Monoid (Monoid (..))
25+#endif
25import Data.Text (Text, pack, unpack)
26import Data.Text (Text, pack, unpack)
26import Data.Text.Encoding (decodeUtf8)
27import Data.Text.Encoding (decodeUtf8)
27−import Data.ByteString (ByteString)
28−import Control.Exception (Exception)
29−import Data.Typeable (Typeable)
30import Data.Text.Read (signed, decimal)
28import Data.Text.Read (signed, decimal)
29+import Data.Typeable (Typeable)
30+
31+import Text.Libyaml
31
32
32newtype YamlParser a = YamlParser
33newtype YamlParser a = YamlParser
33 { unYamlParser :: AnchorMap -> Either Text a
34 { unYamlParser :: AnchorMap -> Either Text a
Data/Yaml/Pretty.hs+16−8
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
1-- | Prettier YAML encoding.
2-- | Prettier YAML encoding.
2--
3--
3-- Since 0.8.13
4-- Since 0.8.13
@@ -9,16 +10,23 @@ module Data.Yaml.Pretty
9 , defConfig
10 , defConfig
10 ) where
11 ) where
11
12
12−import Data.Yaml.Builder
13+import Prelude hiding (null)
14+
15+#if !MIN_VERSION_base(4,8,0)
16+import Control.Applicative ((<$>))
17+#endif
13import Data.Aeson.Types
18import Data.Aeson.Types
14−import Data.Text (Text)
19+import Data.ByteString (ByteString)
15−import Data.Monoid
16−import qualified Data.Vector as V
17−import qualified Data.HashMap.Strict as HM
18import Data.Function (on)
20import Data.Function (on)
21+import qualified Data.HashMap.Strict as HM
19import Data.List (sortBy)
22import Data.List (sortBy)
23+#if !MIN_VERSION_base(4,8,0)
24+import Data.Monoid
25+#endif
20−import Data.ByteString (ByteString)
26+import Data.Text (Text)
21−import Prelude hiding (null)
27+import qualified Data.Vector as V
28+
29+import Data.Yaml.Builder
22
30
23-- |
31-- |
24-- Since 0.8.13
32-- Since 0.8.13
@@ -45,7 +53,7 @@ setConfCompare cmp c = c { confCompare = cmp }
45pretty :: Config -> Value -> YamlBuilder
53pretty :: Config -> Value -> YamlBuilder
46pretty cfg = go
54pretty cfg = go
47 where go (Object o) = mapping (sortBy (confCompare cfg `on` fst) $ HM.toList $ HM.map go o)
55 where go (Object o) = mapping (sortBy (confCompare cfg `on` fst) $ HM.toList $ HM.map go o)
48− go (Array a) = array (fmap go $ V.toList a)
56+ go (Array a) = array (go <$> V.toList a)
49 go Null = null
57 go Null = null
50 go (String s) = string s
58 go (String s) = string s
51 go (Number n) = scientific n
59 go (Number n) = scientific n
Text/Libyaml.hs+14−17
@@ -4,8 +4,6 @@
4{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleContexts #-}
5{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE FlexibleInstances #-}
6{-# LANGUAGE MultiParamTypeClasses #-}
6{-# LANGUAGE MultiParamTypeClasses #-}
7−{-# LANGUAGE PackageImports #-}
8−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
9{-# LANGUAGE RankNTypes #-}
7{-# LANGUAGE RankNTypes #-}
10{-# LANGUAGE CPP #-}
8{-# LANGUAGE CPP #-}
11{-# LANGUAGE TypeFamilies #-}
9{-# LANGUAGE TypeFamilies #-}
@@ -31,12 +29,6 @@ module Text.Libyaml
31
29
32import Prelude hiding (pi)
30import Prelude hiding (pi)
33
31
34−import qualified Data.ByteString.Internal as B
35−import qualified Data.ByteString.Char8 as B8
36−import qualified Data.ByteString
37−import qualified Data.ByteString.Unsafe as BU
38−import Data.ByteString (ByteString, packCStringLen)
39−import Control.Monad
40import Foreign.C
32import Foreign.C
41import Foreign.Ptr
33import Foreign.Ptr
42import Foreign.ForeignPtr
34import Foreign.ForeignPtr
@@ -44,17 +36,22 @@ import Foreign.ForeignPtr
44import Foreign.ForeignPtr.Unsafe
36import Foreign.ForeignPtr.Unsafe
45#endif
37#endif
46import Foreign.Marshal.Alloc
38import Foreign.Marshal.Alloc
47−import Data.Data
48−
49−import Control.Monad.IO.Class
50
39
51−import Control.Exception (throwIO, Exception, finally)
52#if !MIN_VERSION_base(4,8,0)
40#if !MIN_VERSION_base(4,8,0)
53import Control.Applicative
41import Control.Applicative
54#endif
42#endif
43+import Control.Exception (mask_, throwIO, Exception, finally)
44+import Control.Monad
45+import Control.Monad.IO.Class
55import Control.Monad.Trans.Resource
46import Control.Monad.Trans.Resource
56import Data.Conduit hiding (Source, Sink, Conduit)
47import Data.Conduit hiding (Source, Sink, Conduit)
57−import Control.Exception (mask_)
48+import Data.Data
49+
50+import Data.ByteString (ByteString, packCStringLen)
51+import qualified Data.ByteString
52+import qualified Data.ByteString.Char8 as B8
53+import qualified Data.ByteString.Internal as B
54+import qualified Data.ByteString.Unsafe as BU
58
55
59data Event =
56data Event =
60 EventStreamStart
57 EventStreamStart
@@ -267,20 +264,20 @@ getEvent er = do
267 yanchor <- c_get_scalar_anchor er
264 yanchor <- c_get_scalar_anchor er
268 anchor <- if yanchor == nullPtr
265 anchor <- if yanchor == nullPtr
269 then return Nothing
266 then return Nothing
270− else fmap Just $ peekCString yanchor
267+ else Just <$> peekCString yanchor
271 return $ Just $ EventScalar bs (bsToTag tagbs) style anchor
268 return $ Just $ EventScalar bs (bsToTag tagbs) style anchor
272 YamlSequenceStartEvent -> do
269 YamlSequenceStartEvent -> do
273 yanchor <- c_get_sequence_start_anchor er
270 yanchor <- c_get_sequence_start_anchor er
274 anchor <- if yanchor == nullPtr
271 anchor <- if yanchor == nullPtr
275 then return Nothing
272 then return Nothing
276− else fmap Just $ peekCString yanchor
273+ else Just <$> peekCString yanchor
277 return $ Just $ EventSequenceStart anchor
274 return $ Just $ EventSequenceStart anchor
278 YamlSequenceEndEvent -> return $ Just EventSequenceEnd
275 YamlSequenceEndEvent -> return $ Just EventSequenceEnd
279 YamlMappingStartEvent -> do
276 YamlMappingStartEvent -> do
280 yanchor <- c_get_mapping_start_anchor er
277 yanchor <- c_get_mapping_start_anchor er
281 anchor <- if yanchor == nullPtr
278 anchor <- if yanchor == nullPtr
282 then return Nothing
279 then return Nothing
283− else fmap Just $ peekCString yanchor
280+ else Just <$> peekCString yanchor
284 return $ Just $ EventMappingStart anchor
281 return $ Just $ EventMappingStart anchor
285 YamlMappingEndEvent -> return $ Just EventMappingEnd
282 YamlMappingEndEvent -> return $ Just EventMappingEnd
286
283
@@ -605,7 +602,7 @@ encodeFile filePath =
605 file <- withCString filePath $
602 file <- withCString filePath $
606 \filePath' -> withCString "w" $
603 \filePath' -> withCString "w" $
607 \w' -> c_fopen filePath' w'
604 \w' -> c_fopen filePath' w'
608− if (file == nullPtr)
605+ if file == nullPtr
609 then throwIO $ YamlException $ "could not open file for write: " ++ filePath
606 then throwIO $ YamlException $ "could not open file for write: " ++ filePath
610 else return file
607 else return file
611
608
exe/json2yaml.hs+7−9
@@ -1,11 +1,10 @@
1−{-# LANGUAGE MultiParamTypeClasses #-}
2−{-# LANGUAGE FlexibleInstances #-}
3−import System.Environment (getArgs)
4−import qualified Data.Aeson as J
5−import qualified Data.Yaml as Y
6import Control.Monad (when)
1import Control.Monad (when)
2+import qualified Data.Aeson as J
7import qualified Data.ByteString as S
3import qualified Data.ByteString as S
8import qualified Data.ByteString.Lazy as L
4import qualified Data.ByteString.Lazy as L
5+import System.Environment (getArgs)
6+
7+import qualified Data.Yaml as Y
9
8
10main :: IO ()
9main :: IO ()
11main = do
10main = do
@@ -18,7 +17,6 @@ main = do
18 _ -> L.readFile input
17 _ -> L.readFile input
19 case mval of
18 case mval of
20 Nothing -> error "Invalid input JSON"
19 Nothing -> error "Invalid input JSON"
21− Just val -> do
22− case output of
20+ Just val -> case output of
23− "-" -> S.putStr $ Y.encode (val :: Y.Value)
21+ "-" -> S.putStr $ Y.encode (val :: Y.Value)
24− _ -> Y.encodeFile output val
22+ _ -> Y.encodeFile output val
exe/yaml2json.hs+7−5
@@ -1,10 +1,12 @@
1−import Data.Yaml (decodeFileEither, decodeEither')
2−import System.Environment (getArgs)
3−import System.Exit
4−import Data.Aeson (encode, Value)
5import Prelude hiding (putStr, getContents)
1import Prelude hiding (putStr, getContents)
6−import Data.ByteString.Lazy (putStr)
2+
3+import Data.Aeson (encode, Value)
7import Data.ByteString (getContents)
4import Data.ByteString (getContents)
5+import Data.ByteString.Lazy (putStr)
6+import System.Environment (getArgs)
7+import System.Exit
8+
9+import Data.Yaml (decodeFileEither, decodeEither')
8
10
9helpMessage :: IO ()
11helpMessage :: IO ()
10helpMessage = putStrLn "yaml2json FILE\n use - as FILE to indicate stdin" >> exitFailure
12helpMessage = putStrLn "yaml2json FILE\n use - as FILE to indicate stdin" >> exitFailure
test/Data/YamlSpec.hs−4
@@ -148,9 +148,7 @@ spec = do
148 it "serialization of +123 #64" $ do
148 it "serialization of +123 #64" $ do
149 D.decode (D.encode ("+123" :: String)) `shouldBe` Just ("+123" :: String)
149 D.decode (D.encode ("+123" :: String)) `shouldBe` Just ("+123" :: String)
150
150
151−#if MIN_VERSION_aeson(0, 7, 0)
152 it "preserves Scientific precision" casePreservesScientificPrecision
151 it "preserves Scientific precision" casePreservesScientificPrecision
153−#endif
154
152
155
153
156specialStrings :: [T.Text]
154specialStrings :: [T.Text]
@@ -470,7 +468,6 @@ caseIssue49 =
470 where
468 where
471 src = "---\na:\n &id5 value: 1.0\nb:\n *id5: 1.2"
469 src = "---\na:\n &id5 value: 1.0\nb:\n *id5: 1.2"
472
470
473−#if MIN_VERSION_aeson(0, 7, 0)
474-- | We cannot guarantee this before aeson started using 'Scientific'.
471-- | We cannot guarantee this before aeson started using 'Scientific'.
475casePreservesScientificPrecision :: Assertion
472casePreservesScientificPrecision :: Assertion
476casePreservesScientificPrecision = do
473casePreservesScientificPrecision = do
@@ -487,4 +484,3 @@ casePreservesScientificPrecision = do
487 -- results, because of <https://github.com/bos/text/issues/34>.
484 -- results, because of <https://github.com/bos/text/issues/34>.
488 D.decodeEither "x: 9.78159610558926e-5" @?= Right (object
485 D.decodeEither "x: 9.78159610558926e-5" @?= Right (object
489 [ "x" .= D.Number (read "9.78159610558926e-5") ])
486 [ "x" .= D.Number (read "9.78159610558926e-5") ])
490−#endif
yaml.cabal+4−4
@@ -43,7 +43,7 @@ library
43 , bytestring >= 0.9.1.4
43 , bytestring >= 0.9.1.4
44 , conduit >= 1.1.0 && < 1.3
44 , conduit >= 1.1.0 && < 1.3
45 , resourcet >= 0.3 && < 1.2
45 , resourcet >= 0.3 && < 1.2
46− , aeson >= 0.5
46+ , aeson >= 0.7
47 , containers
47 , containers
48 , unordered-containers
48 , unordered-containers
49 , vector
49 , vector
@@ -91,7 +91,7 @@ executable yaml2json
91 build-depends: base >= 4 && < 5
91 build-depends: base >= 4 && < 5
92 , yaml
92 , yaml
93 , bytestring >= 0.9.1.4
93 , bytestring >= 0.9.1.4
94− , aeson >= 0.5
94+ , aeson >= 0.7
95
95
96executable json2yaml
96executable json2yaml
97 if flag(no-exe)
97 if flag(no-exe)
@@ -104,7 +104,7 @@ executable json2yaml
104 build-depends: base >= 4 && < 5
104 build-depends: base >= 4 && < 5
105 , yaml
105 , yaml
106 , bytestring >= 0.9.1.4
106 , bytestring >= 0.9.1.4
107− , aeson >= 0.5
107+ , aeson >= 0.7
108
108
109
109
110test-suite spec
110test-suite spec
@@ -122,7 +122,7 @@ test-suite spec
122 , conduit
122 , conduit
123 , yaml
123 , yaml
124 , text
124 , text
125− , aeson
125+ , aeson >= 0.7
126 , unordered-containers
126 , unordered-containers
127 , vector
127 , vector
128 , resourcet
128 , resourcet