on master help sign in
Add Data.Yaml.TH
commit9f9a6510274598eba86c6de75cd13be679e9cff3 authorMitchell Rosen <mitchellwrosen@gmail.com> dateThu 13 Oct 2016, 23:21 (10 years ago) parent64bf822
2 files changed+43−2
Data/Yaml/TH.hs+39 yaml.cabal+4−2
Data/Yaml/TH.hsnew file+39
@@ -0,0 +1,39 @@
1+{-# LANGUAGE LambdaCase #-}
2+{-# LANGUAGE ScopedTypeVariables #-}
3+{-# LANGUAGE TemplateHaskell #-}
4+
5+module Data.Yaml.TH
6+ ( -- * Decoding
7+ decodeFile
8+ -- * Re-exports from "Data.Yaml"
9+ , Value (..)
10+ , Parser
11+ , Object
12+ , Array
13+ , object
14+ , array
15+ , (.=)
16+ , (.:)
17+ , (.:?)
18+ , (.!=)
19+ , FromJSON (..)
20+ ) where
21+
22+import Data.Yaml hiding (decodeFile)
23+import Language.Haskell.TH
24+import Language.Haskell.TH.Syntax
25+
26+-- | Decode a @yaml@ file at compile time.
27+--
28+-- @
29+-- {-# LANGUAGE TemplateHaskell #-}
30+--
31+-- config :: Config
32+-- config = $$('decodeFile' "config.yaml")
33+-- @
34+decodeFile :: forall a. (Lift a, FromJSON a) => FilePath -> Q (TExp a)
35+decodeFile path = do
36+ addDependentFile path
37+ runIO (decodeFileEither path) >>= \case
38+ Left err -> fail (prettyPrintParseException err)
39+ Right x -> TExp <$> lift (x :: a)
yaml.cabal+4−2
@@ -59,14 +59,16 @@ library
5959 , directory
6060 , enclosed-exceptions
6161 , semigroups
62+ , template-haskell
6263 exposed-modules: Text.Libyaml
6364 Data.Yaml
6465 Data.Yaml.Aeson
6566 Data.Yaml.Builder
6667 Data.Yaml.Config
67− Data.Yaml.Pretty
68− Data.Yaml.Parser
6968 Data.Yaml.Include
69+ Data.Yaml.Parser
70+ Data.Yaml.Pretty
71+ Data.Yaml.TH
7072 other-modules:
7173 Data.Yaml.Internal
7274 ghc-options: -Wall