-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from phadej/pretty
Pretty, resolve #65
- Loading branch information
Showing
8 changed files
with
154 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ dist | |
cabal-dev/ | ||
.cabal-sandbox/ | ||
cabal.sandbox.config | ||
.stack-work/ |
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
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,46 @@ | ||
-- | Prettier YAML encoding. | ||
module Data.Yaml.Pretty | ||
( encodePretty | ||
, Config | ||
, getConfCompare | ||
, setConfCompare | ||
, defConfig | ||
) where | ||
|
||
import Data.Yaml.Builder | ||
import Data.Aeson.Types | ||
import Data.Text (Text) | ||
import Data.Monoid | ||
import qualified Data.Vector as V | ||
import qualified Data.HashMap.Strict as HM | ||
import Data.Function (on) | ||
import Data.List (sortBy) | ||
import Data.ByteString (ByteString) | ||
import Prelude hiding (null) | ||
|
||
data Config = Config | ||
{ confCompare :: Text -> Text -> Ordering -- ^ Function used to sort keys in objects | ||
} | ||
|
||
-- | The default configuration: do not sort objects. | ||
defConfig :: Config | ||
defConfig = Config mempty | ||
|
||
getConfCompare :: Config -> Text -> Text -> Ordering | ||
getConfCompare = confCompare | ||
|
||
setConfCompare :: (Text -> Text -> Ordering) -> Config -> Config | ||
setConfCompare cmp c = c { confCompare = cmp } | ||
|
||
pretty :: Config -> Value -> YamlBuilder | ||
pretty cfg = go | ||
where go (Object o) = mapping (sortBy (confCompare cfg `on` fst) $ HM.toList $ HM.map go o) | ||
go (Array a) = array (fmap go $ V.toList a) | ||
go Null = null | ||
go (String s) = string s | ||
go (Number n) = number n | ||
go (Bool b) = bool b | ||
|
||
-- | Configurable 'encode'. | ||
encodePretty :: ToJSON a => Config -> a -> ByteString | ||
encodePretty cfg = toByteString . pretty cfg . toJSON |
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,9 @@ | ||
flags: | ||
yaml: | ||
no-exe: false | ||
system-libyaml: false | ||
no-unicode: false | ||
packages: | ||
- '.' | ||
extra-deps: [] | ||
resolver: lts-2.22 |
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