packages feed

yaml-pretty-extras 0.0.2.0 → 0.0.2.1

raw patch · 4 files changed

+89/−10 lines, 4 filesdep +microlens-platform

Dependencies added: microlens-platform

Files

ChangeLog.md view
@@ -1,5 +1,9 @@ # Changelog for yaml-pretty-extras +## v0.0.2.1++Add lens helpers, transformFile, overFile, traverseOfFile, sanitizeFile+ ## v0.0.2.0  Add haddock documentation.
src/Data/Yaml/Pretty/Extras.hs view
@@ -23,12 +23,22 @@   , displayPrettyYaml   , decodeFileThrowLogged   , encodeFilePrettyLogged++  -- Transformers and Lenses++  , transformFile+  , inplace+  , sanitizeFile+  , overFile+  , traverseOfFile+   ) where  import           Data.Typeable import           Data.Yaml import           Data.Yaml.Pretty+import           Lens.Micro.Platform import           RIO import qualified RIO.ByteString                as BS import           RIO.List@@ -119,3 +129,43 @@     ]   encodeFilePretty x d   logInfo $ "Saved " <> displayShow x++-- | Run a function over a decoded file f and save the result to g, passthrough the new value.++transformFile+  :: (MonadIO m, FromJSON a1, MonadThrow m, ToPrettyYaml a2) =>+      FilePath -> FilePath -> (a1 -> a2) -> m a2+transformFile f g l = do+  (x :: a1) <- decodeFileThrow f+  encodeFilePretty g (l x)+  return (l x)++-- | Performs a file operation in place++inplace :: (FilePath -> FilePath -> a) -> FilePath -> a+inplace = join++-- | Perform a roundtrip decode/encode on a file to rearrange field order. Doesn't change the type.++sanitizeFile+  :: (MonadIO m, FromJSON a, MonadThrow m, ToPrettyYaml a) =>+     FilePath -> m a+sanitizeFile = flip (inplace transformFile) id++-- | Uses a lens over to modify a file, passthrough the new value.++overFile+  :: (MonadIO m, FromJSON a1, MonadThrow m, ToPrettyYaml a2) =>+     FilePath -> FilePath -> ASetter a1 a2 a b -> (a -> b) -> m a2+overFile f g l a = transformFile f g (over l a)++-- | Use a lens traverseOf to modify a file, passthrough the new value.++traverseOfFile+  :: (MonadIO m, FromJSON a1, MonadThrow m, ToPrettyYaml a2) =>+     FilePath -> FilePath -> LensLike m a1 a2 b1 b2 -> (b1 -> m b2) -> m a2+traverseOfFile f g l a = do+  (x :: a1) <- decodeFileThrow f+  y <- traverseOf l a x+  encodeFilePretty g y+  return y
test/Spec.hs view
@@ -1,21 +1,44 @@-{-# LANGUAGE DeriveAnyClass #-}+{-# LANGUAGE TemplateHaskell #-}+ import           Data.Yaml.Pretty.Extras import           GHC.Generics+import           Lens.Micro.Platform hiding ((.=)) import           RIO import qualified RIO.ByteString                as BS import           Test.Hspec   data Person = Person {-  mood :: Text,-  job  :: Text-} deriving (Eq, Generic, FromJSON, Show, ToJSON)+  _mood :: Text,+  _job  :: Text+} deriving (Eq, Generic, Show) +instance FromJSON Person where+  parseJSON (Object v) = Person+    <$> v .: "mood"+    <*> v .: "job"++instance ToJSON Person where+  toJSON x = object ["mood" .= _mood x, "job" .= _job x]++$(makeLenses ''Person)+ data Conf = Conf {-  name :: Text,-  kind :: Text,-  people :: [Person]-} deriving (Eq, Generic, FromJSON, Show, ToJSON)+  _name :: Text,+  _kind :: Text,+  _people :: [Person]+} deriving (Eq, Generic, Show)++instance FromJSON Conf where+  parseJSON (Object v) = Conf+    <$> v .: "name"+    <*> v .: "kind"+    <*> v .: "people"++instance ToJSON Conf where+  toJSON x = object ["name" .= _name x, "kind" .= _kind x, "people" .= _people x]++$(makeLenses ''Conf)  instance ToPrettyYaml Conf where   fieldOrder = const ["name", "kind", "people", "mood", "job"]
yaml-pretty-extras.cabal view
@@ -2,10 +2,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 010d745442891bcf2faf89ea29834fe82716bfc1ab9555e945b01d2990a228d6+-- hash: 30a719434a8c574644339e466446c2756dcb9b3b71213d82bd7a55dc77c157c1  name:           yaml-pretty-extras-version:        0.0.2.0+version:        0.0.2.1 synopsis:       Extra functionality for pretty printing Yaml documents. description:    Extra functionality for pretty printing Yaml documents. Allows precise field ordering. category:       Data@@ -34,6 +34,7 @@   default-extensions: AutoDeriveTypeable BangPatterns BinaryLiterals ConstraintKinds DataKinds DefaultSignatures DeriveDataTypeable DeriveFoldable DeriveFunctor DeriveGeneric DeriveTraversable DoAndIfThenElse EmptyDataDecls ExistentialQuantification FlexibleContexts FlexibleInstances FunctionalDependencies GADTs GeneralizedNewtypeDeriving InstanceSigs KindSignatures LambdaCase MonadFailDesugaring MultiParamTypeClasses MultiWayIf NamedFieldPuns NoImplicitPrelude OverloadedStrings PartialTypeSignatures PatternGuards PolyKinds RankNTypes RecordWildCards ScopedTypeVariables StandaloneDeriving TupleSections TypeFamilies TypeSynonymInstances ViewPatterns   build-depends:       base >=4.7 && <5+    , microlens-platform     , rio     , yaml   default-language: Haskell2010@@ -50,6 +51,7 @@   build-depends:       base >=4.7 && <5     , hspec+    , microlens-platform     , rio     , yaml     , yaml-pretty-extras