packages feed

yaml-pretty-extras (empty) → 0.0.1.0

raw patch · 7 files changed

+194/−0 lines, 7 filesdep +basedep +bytestringdep +errorssetup-changed

Dependencies added: base, bytestring, errors, hspec, mtl, text, yaml, yaml-pretty-extras

Files

+ ChangeLog.md view
@@ -0,0 +1,6 @@+# Changelog for yaml-pretty-extras++## v0.0.1.0++Added ToPrettyYaml typeclass+Added encodePrettyFile function
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2018++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Author name here nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,24 @@+# yaml-pretty-extras++This library adds a ```toPrettyYaml``` typeclass and helper functions.++## Example Usage++```{.haskell}+{-# LANGUAGE DeriveAnyClass    #-}+{-# LANGUAGE DeriveGeneric     #-}+{-# LANGUAGE OverloadedStrings #-}++import Data.Yaml.Pretty.Extras+import GHC.Generics++data Foo = Foo {+  bar :: Text,+  quux :: Text+} deriving (Eq, Show, FromJSON, ToJSON)++instance ToPrettyYaml Foo where+  fieldOrder = const ["quux", "bar"]++main = encodeFilePretty "foo.yaml" (Foo "bar" "quux") -- prints out "quux: quux\nbar: bar"+```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Data/Yaml/Pretty/Extras.hs view
@@ -0,0 +1,41 @@+{-# LANGUAGE OverloadedStrings #-}++module Data.Yaml.Pretty.Extras (++  module Data.Yaml,+  module Data.Yaml.Pretty,++  ToPrettyYaml(..),+  encodeFilePretty,+  PrettyYamlException(..)++) where++import Control.Applicative+import Control.Error.Safe+import Control.Monad.Except+import qualified Data.ByteString as BS+import Data.List+import Data.Typeable+import Data.Text+import Data.Yaml+import Data.Yaml.Pretty++data PrettyYamlException = FieldNotListed Text [Text]+  deriving (Typeable)++instance Show PrettyYamlException where+  show (FieldNotListed x as) = "Could not find field " ++ (show x) ++ "in " ++ (show as)++exceptElemIndex x as = tryJust (FieldNotListed x as) (elemIndex x as)++listElemCmp as x y = either (error . show) id $ runExcept $ liftA2 compare (exceptElemIndex x as) (exceptElemIndex y as)++class ToJSON a => ToPrettyYaml a where+  fieldOrder :: a -> [Text]++  toPrettyYaml :: a -> BS.ByteString+  toPrettyYaml x = encodePretty (setConfCompare (listElemCmp . fieldOrder $ x) defConfig) x++encodeFilePretty :: ToPrettyYaml a => FilePath -> a -> IO ()+encodeFilePretty f x = BS.writeFile f (toPrettyYaml x)
+ test/Spec.hs view
@@ -0,0 +1,31 @@+{-# LANGUAGE DeriveAnyClass    #-}+{-# LANGUAGE DeriveGeneric     #-}+{-# LANGUAGE OverloadedStrings #-}++import Data.Text+import Data.Yaml.Pretty.Extras+import GHC.Generics+import Test.Hspec++import qualified Data.ByteString as BS++data Person = Person {+  mood :: Text,+  job  :: Text+} deriving (Eq, Generic, FromJSON, Show, ToJSON)++data Conf = Conf {+  name :: Text,+  kind :: Text,+  people :: [Person]+} deriving (Eq, Generic, FromJSON, Show, ToJSON)++instance ToPrettyYaml Conf where+  fieldOrder = const ["name", "kind", "people", "mood", "job"]++main :: IO ()+main = hspec $ do+  describe "Data.Yaml.Pretty.Extras" $ do+   it "prints correctly" $ do+    Just x <- either (error . show) id <$> decodeFileEither "test/mock.yaml"  :: IO (Maybe Conf)+    BS.readFile "test/mock.yaml" `shouldReturn` (toPrettyYaml x)
+ yaml-pretty-extras.cabal view
@@ -0,0 +1,60 @@+-- This file has been generated from package.yaml by hpack version 0.28.2.+--+-- see: https://github.com/sol/hpack+--+-- hash: 10a917deabaf96f7b00417d2777df1db675e35e4ce413f5519a569d71b864838++name:           yaml-pretty-extras+version:        0.0.1.0+synopsis:       Extra functionality for pretty printing Yaml documents.+description:    Extra functionality for pretty printing Yaml documents. Allows precise field ordering.+category:       Data+author:         Daniel Firth+maintainer:     locallycompact@gmail.com+copyright:      2018 Daniel Firth+license:        BSD3+license-file:   LICENSE+build-type:     Simple+cabal-version:  >= 1.10+extra-source-files:+    ChangeLog.md+    README.md++source-repository head+  type: git+  location: https://gitlab.com/locallycompact/yaml-pretty-extras++library+  exposed-modules:+      Data.Yaml.Pretty.Extras+  other-modules:+      Paths_yaml_pretty_extras+  hs-source-dirs:+      src+  build-depends:+      base >=4.7 && <5+    , bytestring+    , errors+    , mtl+    , text+    , yaml+  default-language: Haskell2010++test-suite yaml-pretty-extras-test+  type: exitcode-stdio-1.0+  main-is: Spec.hs+  other-modules:+      Paths_yaml_pretty_extras+  hs-source-dirs:+      test+  ghc-options: -threaded -rtsopts -with-rtsopts=-N+  build-depends:+      base >=4.7 && <5+    , bytestring+    , errors+    , hspec+    , mtl+    , text+    , yaml+    , yaml-pretty-extras+  default-language: Haskell2010