on master help sign in
Merge pull request #117 from sophie-h/feature-drop-null
316ccf0Michael Snoyman
ChangeLog.md+4
@@ -1,3 +1,7 @@
1+## 0.8.24
2+
3+* New encodePretty option `setConfDropNull` to drop null values from objects [#116](https://github.com/snoyberg/yaml/issues/116)
4+
1## 0.8.23.3
5## 0.8.23.3
2
6
3* Avoid over-escaping `*` [#113](https://github.com/snoyberg/yaml/issues/113)
7* Avoid over-escaping `*` [#113](https://github.com/snoyberg/yaml/issues/113)
Data/Yaml/Pretty.hs+29−10
@@ -1,12 +1,14 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2-- | Prettier YAML encoding.
2-- | Prettier YAML encoding.
3--
3--
4−-- Since 0.8.13
4+-- @since 0.8.13
5module Data.Yaml.Pretty
5module Data.Yaml.Pretty
6 ( encodePretty
6 ( encodePretty
7 , Config
7 , Config
8 , getConfCompare
8 , getConfCompare
9 , setConfCompare
9 , setConfCompare
10+ , getConfDropNull
11+ , setConfDropNull
10 , defConfig
12 , defConfig
11 ) where
13 ) where
12
14
@@ -29,30 +31,47 @@ import qualified Data.Vector as V
29import Data.Yaml.Builder
31import Data.Yaml.Builder
30
32
31-- |
33-- |
32−-- Since 0.8.13
34+-- @since 0.8.13
33data Config = Config
35data Config = Config
34 { confCompare :: Text -> Text -> Ordering -- ^ Function used to sort keys in objects
36 { confCompare :: Text -> Text -> Ordering -- ^ Function used to sort keys in objects
37+ , confDropNull :: Bool
35 }
38 }
36
39
37−-- | The default configuration: do not sort objects.
40+-- | The default configuration: do not sort objects or drop keys
38--
41--
39−-- Since 0.8.13
42+-- @since 0.8.13
40defConfig :: Config
43defConfig :: Config
41−defConfig = Config mempty
44+defConfig = Config mempty False
42
45
43-- |
46-- |
44−-- Since 0.8.13
47+-- @since 0.8.13
45getConfCompare :: Config -> Text -> Text -> Ordering
48getConfCompare :: Config -> Text -> Text -> Ordering
46getConfCompare = confCompare
49getConfCompare = confCompare
47
50
48−-- |
51+-- | Sets ordering for object keys
52+--
49−-- Since 0.8.13
53+-- @since 0.8.13
50setConfCompare :: (Text -> Text -> Ordering) -> Config -> Config
54setConfCompare :: (Text -> Text -> Ordering) -> Config -> Config
51setConfCompare cmp c = c { confCompare = cmp }
55setConfCompare cmp c = c { confCompare = cmp }
52
56
57+-- |
58+-- @since 0.8.24
59+getConfDropNull :: Config -> Bool
60+getConfDropNull = confDropNull
61+
62+-- | Drop entries with `Null` value from objects, if set to `True`
63+--
64+-- @since 0.8.24
65+setConfDropNull :: Bool -> Config -> Config
66+setConfDropNull m c = c { confDropNull = m }
67+
53pretty :: Config -> Value -> YamlBuilder
68pretty :: Config -> Value -> YamlBuilder
54pretty cfg = go
69pretty cfg = go
55− where go (Object o) = mapping (sortBy (confCompare cfg `on` fst) $ HM.toList $ HM.map go o)
70+ where go (Object o) = let sort = sortBy (confCompare cfg `on` fst)
71+ select
72+ | confDropNull cfg = HM.filter (/= Null)
73+ | otherwise = id
74+ in mapping (sort $ HM.toList $ HM.map go $ select o)
56 go (Array a) = array (go <$> V.toList a)
75 go (Array a) = array (go <$> V.toList a)
57 go Null = null
76 go Null = null
58 go (String s) = string s
77 go (String s) = string s
@@ -61,6 +80,6 @@ pretty cfg = go
61
80
62-- | Configurable 'encode'.
81-- | Configurable 'encode'.
63--
82--
64−-- Since 0.8.13
83+-- @since 0.8.13
65encodePretty :: ToJSON a => Config -> a -> ByteString
84encodePretty :: ToJSON a => Config -> a -> ByteString
66encodePretty cfg = toByteString . pretty cfg . toJSON
85encodePretty cfg = toByteString . pretty cfg . toJSON
yaml.cabal+1−1
@@ -1,5 +1,5 @@
1name: yaml
1name: yaml
2−version: 0.8.23.3
2+version: 0.8.24
3license: BSD3
3license: BSD3
4license-file: LICENSE
4license-file: LICENSE
5author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov
5author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov