on master help sign in
Examples directory
commit22c0a27ba560a3d324424a248ad92b3bcbb4e52b authorChris Allen <cma@bitemyapp.com> dateSun 28 Feb 2016, 15:46 (11 years ago) parentdd8679f
5 files changed+83−1
Data/Yaml.hs+2−1 examples/Main.hs+10 examples/config.hs+50 examples/simple.hs+8 yaml.cabal+13
Data/Yaml.hs+2−1
@@ -1,6 +1,7 @@
11{-# LANGUAGE CPP #-}
22{-# LANGUAGE ScopedTypeVariables #-}
33{-# LANGUAGE OverloadedStrings #-}
4+
45-- | Provides a high-level interface for processing YAML files.
56--
67-- This module reuses most of the infrastructure from the @aeson@ package.
@@ -27,7 +28,7 @@
2728-- > import qualified Data.Yaml as Y
2829-- >
2930-- > main = do
30−-- > putStrLn . show $ (Y.decode "[1,2,3]" :: Maybe [Integer])
31+-- > print $ (Y.decode "[1,2,3]" :: Maybe [Integer])
3132--
3233-- You can go one step further and decode into a custom type by implementing
3334-- '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
133133 , base-compat
134134 ghc-options: -Wall
135135
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+
136149source-repository head
137150 type: git
138151 location: https://github.com/snoyberg/yaml