salak-yaml (empty) → 0.2.7
raw patch · 7 files changed
+205/−0 lines, 7 filesdep +QuickCheckdep +aesondep +basesetup-changed
Dependencies added: QuickCheck, aeson, base, hspec, mtl, salak, text, unordered-containers, vector, yaml
Files
- LICENSE +30/−0
- README.md +1/−0
- Setup.hs +2/−0
- salak-yaml.cabal +51/−0
- src/Salak/Load/Yaml.hs +36/−0
- test/Spec.hs +71/−0
- test/salak.yml +14/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Daniel YU (c) 2019++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 Daniel YU 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,1 @@+# salak-yaml
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ salak-yaml.cabal view
@@ -0,0 +1,51 @@+cabal-version: 1.12+name: salak-yaml+version: 0.2.7+license: BSD3+license-file: LICENSE+copyright: (c) 2018 Daniel YU+maintainer: Daniel YU <leptonyu@gmail.com>+author: Daniel YU+homepage: https://github.com/leptonyu/salak#readme+synopsis: Configuration Loader for yaml+category: Library, Configuration+build-type: Simple+extra-source-files:+ README.md+ test/salak.yml++library+ exposed-modules:+ Salak.Load.Yaml+ hs-source-dirs: src+ other-modules:+ Paths_salak_yaml+ default-language: Haskell2010+ build-depends:+ aeson >=1.4.2.0 && <1.5,+ base >=4.10 && <5,+ mtl >=2.2.2 && <2.3,+ salak ==0.2.7,+ unordered-containers >=0.2.9.0 && <0.3,+ vector >=0.12.0.2 && <0.13,+ yaml >=0.11.0.0 && <0.12++test-suite spec+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ hs-source-dirs: test src+ other-modules:+ Salak.Load.Yaml+ Paths_salak_yaml+ default-language: Haskell2010+ build-depends:+ QuickCheck >=2.12.6.1 && <2.14,+ aeson >=1.4.2.0 && <1.5,+ base >=4.10 && <5,+ hspec ==2.*,+ mtl >=2.2.2 && <2.3,+ salak ==0.2.7,+ text >=1.2.3.1 && <1.3,+ unordered-containers >=0.2.9.0 && <0.3,+ vector >=0.12.0.2 && <0.13,+ yaml >=0.11.0.0 && <0.12
+ src/Salak/Load/Yaml.hs view
@@ -0,0 +1,36 @@+{-# LANGUAGE TupleSections #-}+module Salak.Load.Yaml where++import Control.Monad.IO.Class (MonadIO, liftIO)+import Control.Monad.State+import qualified Data.Aeson as A+import qualified Data.HashMap.Strict as HM+import qualified Data.Vector as V+import qualified Data.Yaml as Y+import Salak+import Salak.Load++loadJSON :: Reload -> A.Value -> SourcePack -> SourcePack+loadJSON name v sp = loadFile name sp $ go v+ where+ go (A.Object m) i s = foldl (g2 i) (return s) $ g3 <$> HM.toList m+ go (A.Array m) i s = foldl (g2 i) (return s) $ zip (g4 <$> [0..]) $ V.toList m+ go (A.String m) i s = return $ insertSource (VStr i m) s+ go (A.Number m) i s = return $ insertSource (VNum i m) s+ go (A.Bool m) i s = return $ insertSource (VBool i m) s+ go A.Null _ s = return s+ g2 i ms (k, v') = ms >>= updateSources k (go v' i)+ g3 (k,x) = (simpleSelectors k, x)+ g4 i = [SNum i]++loadYaml :: MonadIO m => FilePath -> SourcePackT m ()+loadYaml file = do+ v <- liftIO $ Y.decodeFileEither file+ modify $ \sp -> case v of+ Left e -> addErr' (show e) sp+ Right a -> loadJSON (defReload file $ loadYaml file) a sp++data YAML = YAML++instance HasLoad YAML where+ loaders _ = (, loadYaml) <$> ["yaml", "yml"]
+ test/Spec.hs view
@@ -0,0 +1,71 @@+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE ScopedTypeVariables #-}++module Main where++import Control.Monad.Reader+import Control.Monad.Writer+import Data.Either+import Data.List (intercalate)+import Data.Text (Text, pack, unpack)+import GHC.Generics+import Salak+import Salak.Load.Yaml+import Test.Hspec+import Test.QuickCheck++main = hspec spec++spec :: Spec+spec = do+ describe "Salak.Yaml" jsonProperty++newtype SKey = SKey { unKey :: Text } deriving Show++instance Arbitrary SKey where+ arbitrary = do+ key <- choose (1,20)+ vs <- vectorOf key $ do+ k <- choose (1,20)+ v <- vectorOf k $ choose ('a','z')+ b <- choose (0,10) :: Gen Int+ if b > 0 then return v else do+ x <- choose (0,10) :: Gen Int+ return (v ++ "[" ++ show x ++ "]")+ return (SKey $ pack $ intercalate "." vs)++data Conf = Conf+ { name :: String+ , age :: Int+ , male :: Bool+ , det :: SubConf+ } deriving (Eq, Show, Generic)++data SubConf = SubConf+ { hello :: String } deriving (Eq, Show, Generic)++instance FromProp SubConf where+ fromProp = SubConf <$> "hello" .?= "yyy"++instance FromProp Conf++jsonProperty = do+ context "load json" $ do+ it "salak.yml" $ do+ loadAndRunSalak (loadYaml "test/salak.yml") $ do+ as <- require "array"+ cf <- require "me.icymint.conf"+ lift $ do+ as `shouldBe` ["a","b","d","c" :: String]+ name cf `shouldBe` "daniel"+ age cf `shouldBe` 18+ male cf `shouldBe` True+ det cf `shouldBe` SubConf "abc"+++++
+ test/salak.yml view
@@ -0,0 +1,14 @@+array:+ - a+ - b+ - d+ - c+hello:+ world++me.icymint.conf:+ name: daniel+ age: 18+ male: yes+ det:+ hello: abc