salak-yaml 0.2.10 → 0.3
raw patch · 4 files changed
+57/−39 lines, 4 filesdep +exceptionsdep ~basedep ~salakPVP ok
version bump matches the API change (PVP)
Dependencies added: exceptions
Dependency ranges changed: base, salak
API changes (from Hackage documentation)
+ Salak.Yaml: runSalakWithYaml :: (MonadCatch m, MonadIO m) => FilePath -> RunSalakT m a -> m a
- Salak.Yaml: loadYaml :: MonadIO m => FilePath -> LoadSalakT m ()
+ Salak.Yaml: loadYaml :: FilePath -> LoadSalak ()
Files
- salak-yaml.cabal +4/−3
- src/Salak/Yaml.hs +33/−26
- test/Spec.hs +15/−8
- test/salak.yml +5/−2
salak-yaml.cabal view
@@ -1,6 +1,6 @@ cabal-version: 1.12 name: salak-yaml-version: 0.2.10+version: 0.3 license: BSD3 license-file: LICENSE copyright: (c) 2018 Daniel YU@@ -26,7 +26,7 @@ base >=4.10 && <5, conduit >=1.3.1.1 && <1.4, libyaml >=0.1.1.0 && <0.2,- salak >=0.2.10 && <0.3,+ salak ==0.3.*, text >=1.2.3.1 && <1.3 test-suite spec@@ -42,8 +42,9 @@ QuickCheck >=2.13.2 && <2.14, base >=4.10 && <5, conduit >=1.3.1.1 && <1.4,+ exceptions >=0.10.2 && <0.11, hspec ==2.*, libyaml >=0.1.1.0 && <0.2, mtl >=2.2.2 && <2.3,- salak >=0.2.10 && <0.3,+ salak ==0.3.*, text >=1.2.3.1 && <1.3
src/Salak/Yaml.hs view
@@ -14,6 +14,7 @@ module Salak.Yaml( YAML(..) , loadYaml+ , runSalakWithYaml ) where import Control.Exception (throwIO)@@ -21,12 +22,16 @@ import Data.Conduit hiding (Source) import Data.Text.Encoding (decodeUtf8) import Salak-import Salak.Load+import Salak.Internal+import qualified Salak.Trie as T import Text.Libyaml +runSalakWithYaml :: (MonadCatch m, MonadIO m) => FilePath -> RunSalakT m a -> m a+runSalakWithYaml name = runSalakWith name YAML+ -- | Load Yaml-loadYaml :: MonadIO m => FilePath -> LoadSalakT m ()-loadYaml file = load file $ \i s -> liftIO $ runConduitRes (decodeFileMarked file .| loadYAML i s)+loadYaml :: FilePath -> LoadSalak ()+loadYaml file = loadTrie True file (\i -> runConduitRes (decodeFileMarked file .| loadYAML i T.empty)) -- | YAML notation for `loadYaml` data YAML = YAML@@ -34,32 +39,34 @@ instance HasLoad YAML where loaders _ = (, loadYaml) <$> ["yaml", "yml"] -loadYAML :: MonadIO m => Priority -> Source -> ConduitM MarkedEvent o m Source-loadYAML i s = await >>= maybe (return s) go+loadYAML :: MonadIO m => Int -> TraceSource -> ConduitM MarkedEvent o m TraceSource+loadYAML i = start where- go (MarkedEvent (EventScalar a _ _ _) _ _) = return (insertSource (newVStr (decodeUtf8 a) i) s)- go (MarkedEvent EventSequenceStart{} _ _) = goSeq 0 s- go (MarkedEvent EventSequenceEnd _ _) = return emptySource- go (MarkedEvent EventMappingStart{} _ ee) = goMap ee s- go (MarkedEvent (EventAlias a) _ ee) = ge ee $ "alias " ++ a ++ " not supported by salak"- go _ = loadYAML i s- goSeq j s1 = do- s' <- loadYAML i emptySource- if nullSource s'- then return s1- else updateSource (SNum j) (\_ -> return s') s1 >>= goSeq (j+1)- goMap ee s1 = do+ start ts = await >>= maybe (return ts) (go ts)++ go _ (MarkedEvent (EventAlias a) _ ee) = ge ee $ "alias " ++ a ++ " not supported by salak"+ go ts (MarkedEvent (EventScalar a _ _ _) _ _) = return $ setVal i a ts+ go ts (MarkedEvent EventSequenceStart{} _ _) = goS 0 ts+ go ts (MarkedEvent EventMappingStart{} _ _) = goM ts+ go ts _ = start ts++ goS j ts = do v <- await case v of- Nothing -> ge ee "suppose to have data"- Just (MarkedEvent (EventScalar a _ _ _) _ ee') ->- updateSources (simpleSelectors $ decodeUtf8 a) (loadYAML i) s1 >>= goMap ee'- Just (MarkedEvent EventMappingEnd _ _) -> return s1+ Nothing -> liftIO $ throwIO $ YamlException "unexpected end"+ Just (MarkedEvent EventSequenceEnd _ _) -> return ts+ Just e -> do+ val <- go T.empty e+ goS (j+1) (T.modify (KI j) (const val) ts)++ goM ts = do+ v <- await+ case v of+ Nothing -> liftIO $ throwIO $ YamlException "unexpected end"+ Just (MarkedEvent EventMappingEnd _ _) -> return ts+ Just (MarkedEvent (EventScalar a _ _ _) _ _) -> do+ val <- start T.empty+ goM $ T.modify' (Keys $ simpleKeys $ decodeUtf8 a) (const val) ts Just e -> ge (yamlStartMark e) ("suppose scalar and mapping end, but is " ++ show (yamlEvent e)) ge YamlMark{..} e = liftIO $ throwIO $ YamlException $ "(" ++ show yamlLine ++ "," ++ show yamlColumn ++ ")" ++ e-----
test/Spec.hs view
@@ -1,16 +1,22 @@-{-# LANGUAGE DataKinds #-}-{-# LANGUAGE DeriveGeneric #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE OverloadedStrings #-}-{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE RecordWildCards #-}+{-# LANGUAGE ScopedTypeVariables #-} module Main where +import Control.Monad.Catch import Control.Monad.Reader import Data.List (intercalate) import Data.Text (Text, pack)+import Debug.Trace import GHC.Generics import Salak+import Salak.Internal import Salak.Yaml import Test.Hspec import Test.QuickCheck@@ -46,17 +52,18 @@ data SubConf = SubConf { hello :: String } deriving (Eq, Show, Generic) -instance FromProp SubConf where+instance MonadCatch m => FromProp m SubConf where fromProp = SubConf <$> "hello" .?= "yyy" -instance FromProp Conf+instance MonadCatch m => FromProp m Conf jsonProperty :: SpecWith () jsonProperty = do context "load json" $ do it "salak.yml" $ do loadAndRunSalak (loadYaml "test/salak.yml") $ do- as <- require "array"+ SourcePack{..} <- askSalak+ as <- trace (show source) $ require "array" cf <- require "me.icymint.conf" lift $ do as `shouldBe` ["a","b","d","c" :: String]
test/salak.yml view
@@ -6,8 +6,7 @@ hello: world a:- b:- c+ b: c me.icymint.conf: name: daniel@@ -15,3 +14,7 @@ male: yes det: hello: abc++xyz:+ - bcd: hello+ - bcd: world