@@ -7,7 +7,6 @@ module Data.Yaml.Pretty
8 , getConfCompare
8 , getConfCompare
9 , setConfCompare
9 , setConfCompare
11 , getConfDropNull
10 , getConfDropNull
12 , setConfDropNull
11 , setConfDropNull
13 , defConfig
12 , defConfig
@@ -51,7 +50,7 @@ type Key = Text
52-- @since 0.8.13
51-- @since 0.8.13
53data Config = Config
52data Config = Config
54− { confCompare :: JSONPath -> Text -> Text -> Ordering -- ^ Function used to sort keys in objects
53+ { confCompare :: Text -> Text -> Ordering -- ^ Function used to sort keys in objects
55 , confDropNull :: Bool -- ^ Drop null values from objects
54 , confDropNull :: Bool -- ^ Drop null values from objects
@@ -63,20 +62,14 @@ defConfig = Config mempty False
65-- @since 0.8.13
64-- @since 0.8.13
66−getConfCompare :: Config -> JSONPath -> Text -> Text -> Ordering
65+getConfCompare :: Config -> Text -> Text -> Ordering
67getConfCompare = confCompare
66getConfCompare = confCompare
69-- | Sets ordering for object keys
68-- | Sets ordering for object keys
71-- @since 0.8.13
70-- @since 0.8.13
72setConfCompare :: (Text -> Text -> Ordering) -> Config -> Config
71setConfCompare :: (Text -> Text -> Ordering) -> Config -> Config
73−setConfCompare cmp c = c { confCompare = \_ps -> cmp }
72+setConfCompare cmp c = c { confCompare = cmp }
75−-- | Set ordering for object keys that depends on location of object in the document.
78−setConfComparePos :: (JSONPath -> Text -> Text -> Ordering) -> Config -> Config
79−setConfComparePos cmp c = c { confCompare = cmp }
82-- @since 0.8.24
75-- @since 0.8.24
@@ -90,17 +83,17 @@ setConfDropNull :: Bool -> Config -> Config
90setConfDropNull m c = c { confDropNull = m }
83setConfDropNull m c = c { confDropNull = m }
92pretty :: Config -> Value -> YamlBuilder
85pretty :: Config -> Value -> YamlBuilder
93−pretty cfg = go []
86+pretty cfg = go
94− where go ps (Object o) = let sort = sortBy (confCompare cfg (reverse ps) `on` fst)
87+ where go (Object o) = let sort = sortBy (confCompare cfg `on` fst)
96− | confDropNull cfg = HM.filter (/= Null)
89+ | confDropNull cfg = HM.filter (/= Null)
97− | otherwise = id
90+ | otherwise = id
98− in mapping (sort $ fmap (first toText) $ HM.toList $ HM.mapWithKey (\k -> go (Key (toText k): ps)) $ select o)
91+ in mapping (sort $ fmap (first toText) $ HM.toList $ HM.map go $ select o)
99− go ps (Array a) = array $ zipWith (\ix -> go (Index ix: ps)) [0..] (V.toList a)
92+ go (Array a) = array (go <$> V.toList a)
100− go _ Null = null
93+ go Null = null
101− go _ (String s) = string s
94+ go (String s) = string s
102− go _ (Number n) = scientific n
95+ go (Number n) = scientific n
103− go _ (Bool b) = bool b
96+ go (Bool b) = bool b
105-- | Configurable 'encode'.
98-- | Configurable 'encode'.