on master help sign in
Examples directory
22c0a27Chris Allen
Data/Yaml.hs+2−1
@@ -1,6 +1,7 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE ScopedTypeVariables #-}
2{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE OverloadedStrings #-}
4+
4-- | Provides a high-level interface for processing YAML files.
5-- | Provides a high-level interface for processing YAML files.
5--
6--
6-- This module reuses most of the infrastructure from the @aeson@ package.
7-- This module reuses most of the infrastructure from the @aeson@ package.
@@ -27,7 +28,7 @@
27-- > import qualified Data.Yaml as Y
28-- > import qualified Data.Yaml as Y
28-- >
29-- >
29-- > main = do
30-- > main = do
30−-- > putStrLn . show $ (Y.decode "[1,2,3]" :: Maybe [Integer])
31+-- > print $ (Y.decode "[1,2,3]" :: Maybe [Integer])
31--
32--
32-- You can go one step further and decode into a custom type by implementing
33-- You can go one step further and decode into a custom type by implementing
33-- 'FromJSON' for that type. This is also appropriate where extra
34-- 'FromJSON' for that type. This is also appropriate where extra
examples/Main.hsnew file+10
@@ -0,0 +1,10 @@
1+module Main where
2+
3+import qualified Config
4+import qualified Simple
5+
6+main :: IO ()
7+main = do
8+ Simple.main
9+ Config.main
10+
examples/config.hsnew file+50
@@ -0,0 +1,50 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE QuasiQuotes #-}
3+
4+module Config where
5+
6+import Data.Text (Text)
7+import qualified Data.Yaml as Y
8+import Data.Yaml (FromJSON(..), (.:))
9+import Text.RawString.QQ
10+
11+configYaml = [r|
12+resolver: lts-3.7
13+packages:
14+ - ./yesod-core
15+ - ./yesod-static
16+ - ./yesod-persistent
17+ - ./yesod-newsfeed
18+ - ./yesod-form
19+ - ./yesod-auth
20+ - ./yesod-auth-oauth
21+ - ./yesod-sitemap
22+ - ./yesod-test
23+ - ./yesod-bin
24+ - ./yesod
25+ - ./yesod-eventsource
26+ - ./yesod-websockets
27+
28+# Needed for LTS 2
29+extra-deps:
30+- wai-app-static-3.1.4.1
31+|]
32+
33+data Config =
34+ Config {
35+ resolver :: Text
36+ , packages :: [FilePath]
37+ , extraDeps :: [Text]
38+ } deriving (Eq, Show)
39+
40+instance FromJSON Config where
41+ parseJSON (Y.Object v) =
42+ Config <$>
43+ v .: "resolver" <*>
44+ v .: "packages" <*>
45+ v .: "extra-deps"
46+ parseJSON _ = fail "Expected Object for Config value"
47+
48+main :: IO ()
49+main =
50+ print $ (Y.decode configYaml :: Maybe Config)
examples/simple.hsnew file+8
@@ -0,0 +1,8 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
3+module Simple where
4+
5+import qualified Data.Yaml as Y
6+
7+main = do
8+ print $ (Y.decode "[1,2,3]" :: Maybe [Integer])
yaml.cabal+13
@@ -133,6 +133,19 @@ test-suite spec
133 , base-compat
133 , base-compat
134 ghc-options: -Wall
134 ghc-options: -Wall
135
135
136+executable examples
137+ if flag(no-exe)
138+ Buildable: False
139+ else
140+ Buildable: True
141+ hs-source-dirs: examples
142+ main-is: Main.hs
143+ build-depends: base
144+ , raw-strings-qq
145+ , text
146+ , yaml
147+ ghc-options: -Wall
148+
136source-repository head
149source-repository head
137 type: git
150 type: git
138 location: https://github.com/snoyberg/yaml
151 location: https://github.com/snoyberg/yaml