on master help sign in
Merge pull request #127 from sol/master
9b0ff24Michael Snoyman
ChangeLog.md+4
@@ -1,3 +1,7 @@
1+## 0.8.28
2+
3+* Add `Data.Yaml.TH.yamlQQ`
4+
1## 0.8.27
5## 0.8.27
2
6
3* Support conduit 1.3
7* Support conduit 1.3
Data/Yaml/TH.hs+48−6
@@ -1,10 +1,13 @@
1+{-# LANGUAGE CPP #-}
1{-# LANGUAGE LambdaCase #-}
2{-# LANGUAGE LambdaCase #-}
2{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE ScopedTypeVariables #-}
3−{-# LANGUAGE TemplateHaskell #-}
4
4
5module Data.Yaml.TH
5module Data.Yaml.TH
6 ( -- * Decoding
6 ( -- * Decoding
7+ yamlQQ
8+#if MIN_VERSION_template_haskell(2,9,0)
7− decodeFile
9+ , decodeFile
10+#endif
8 -- * Re-exports from "Data.Yaml"
11 -- * Re-exports from "Data.Yaml"
9 , Value (..)
12 , Value (..)
10 , Parser
13 , Parser
@@ -19,11 +22,16 @@ module Data.Yaml.TH
19 , FromJSON (..)
22 , FromJSON (..)
20 ) where
23 ) where
21
24
22−import Data.Yaml hiding (decodeFile)
25+import Data.Text.Encoding
26+import qualified Data.Text as T
23−import Language.Haskell.TH
27+import Language.Haskell.TH
24−import Language.Haskell.TH.Syntax
28+import Language.Haskell.TH.Syntax
29+import Language.Haskell.TH.Quote
25
30
31+import Data.Yaml hiding (decodeFile)
32+
33+#if MIN_VERSION_template_haskell(2,9,0)
26−-- | Decode a @yaml@ file at compile time. Only available on GHC version @7.8.1@
34+-- | Decode a YAML file at compile time. Only available on GHC version @7.8.1@
27-- or higher.
35-- or higher.
28--
36--
29-- @since 0.8.19.0
37-- @since 0.8.19.0
@@ -42,3 +50,37 @@ decodeFile path = do
42 runIO (decodeFileEither path) >>= \case
50 runIO (decodeFileEither path) >>= \case
43 Left err -> fail (prettyPrintParseException err)
51 Left err -> fail (prettyPrintParseException err)
44 Right x -> fmap TExp (lift (x :: a))
52 Right x -> fmap TExp (lift (x :: a))
53+#endif
54+
55+decodeValue :: String -> Either String Value
56+decodeValue = decodeEither . encodeUtf8 . T.pack
57+
58+yamlExp :: String -> Q Exp
59+yamlExp input = case decodeValue input of
60+ Left err -> fail err
61+ Right a -> lift a
62+
63+-- | A @QuasiQuoter@ for YAML.
64+--
65+-- @since 0.8.28.0
66+--
67+-- ==== __Examples__
68+--
69+-- @
70+-- {-\# LANGUAGE QuasiQuotes \#-}
71+-- import Data.Yaml.TH
72+--
73+-- value :: Value
74+-- value = [yamlQQ|
75+-- name: John Doe
76+-- age: 23
77+-- |]
78+-- @
79+yamlQQ :: QuasiQuoter
80+yamlQQ = QuasiQuoter {
81+ quoteExp = yamlExp
82+, quotePat = notDefined "quotePat"
83+, quoteType = notDefined "quoteType"
84+, quoteDec = notDefined "quoteDec"
85+} where
86+ notDefined name _ = fail (name ++ " is not defined for yamlQQ")
test/Data/Yaml/THSpec.hsnew file+17
@@ -0,0 +1,17 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE QuasiQuotes #-}
3+module Data.Yaml.THSpec (spec) where
4+
5+import Test.Hspec
6+import Data.Aeson
7+
8+import Data.Yaml.TH
9+
10+spec :: Spec
11+spec = do
12+ describe "yamlQQ" $ do
13+ it "parses yaml" $ do
14+ [yamlQQ|
15+ name: John Doe
16+ age: 23
17+ |] `shouldBe` object ["name" .= String "John Doe", "age" .= Number 23]
yaml.cabal+4−4
@@ -1,5 +1,5 @@
1name: yaml
1name: yaml
2−version: 0.8.27
2+version: 0.8.28
3license: BSD3
3license: BSD3
4license-file: LICENSE
4license-file: LICENSE
5author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov
5author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov
@@ -60,6 +60,7 @@ library
60 , filepath
60 , filepath
61 , directory
61 , directory
62 , semigroups
62 , semigroups
63+ , template-haskell
63 exposed-modules: Text.Libyaml
64 exposed-modules: Text.Libyaml
64 Data.Yaml
65 Data.Yaml
65 Data.Yaml.Aeson
66 Data.Yaml.Aeson
@@ -68,9 +69,7 @@ library
68 Data.Yaml.Include
69 Data.Yaml.Include
69 Data.Yaml.Parser
70 Data.Yaml.Parser
70 Data.Yaml.Pretty
71 Data.Yaml.Pretty
71− if impl(ghc >= 7.8.1)
72− build-depends: template-haskell >= 2.8.0.0
73− exposed-modules: Data.Yaml.TH
72+ Data.Yaml.TH
74 other-modules:
73 other-modules:
75 Data.Yaml.Internal
74 Data.Yaml.Internal
76 ghc-options: -Wall
75 ghc-options: -Wall
@@ -126,6 +125,7 @@ test-suite spec
126 main-is: Spec.hs
125 main-is: Spec.hs
127 other-modules: Data.YamlSpec
126 other-modules: Data.YamlSpec
128 Data.Yaml.IncludeSpec
127 Data.Yaml.IncludeSpec
128+ Data.Yaml.THSpec
129 cpp-options: -DTEST
129 cpp-options: -DTEST
130 build-depends: hspec >= 1.3
130 build-depends: hspec >= 1.3
131 , HUnit
131 , HUnit