on master help sign in
A bit classier
commitac9f8c7ea4a81bbd1ac505203d8e74eccfe4c25b authorMichael Snoyman <michael@snoyman.com> dateSun 9 Feb 2014, 14:47 (13 years ago) parent393dbb3
2 files changed+17−20
Data/Yaml/Builder.hs+4−4 builder-test.hs+13−16
Data/Yaml/Builder.hs+4−4
@@ -60,11 +60,11 @@ toEvents :: YamlBuilder -> [Event]
6060toEvents (YamlBuilder front) =
6161 EventStreamStart : EventDocumentStart : front [EventDocumentEnd, EventStreamEnd]
6262
63−toSource :: Monad m => YamlBuilder -> Source m Event
64−toSource = mapM_ yield . toEvents
63+toSource :: (Monad m, ToYaml a) => a -> Source m Event
64+toSource = mapM_ yield . toEvents . toYaml
6565
66−toByteString :: YamlBuilder -> ByteString
66+toByteString :: ToYaml a => a -> ByteString
6767toByteString yb = unsafePerformIO $ runResourceT $ toSource yb $$ encode
6868
69−writeYamlFile :: FilePath -> YamlBuilder -> IO ()
69+writeYamlFile :: ToYaml a => FilePath -> a -> IO ()
7070writeYamlFile fp yb = runResourceT $ toSource yb $$ encodeFile fp
builder-test.hs+13−16
@@ -3,22 +3,19 @@ import Data.Yaml.Builder
33import qualified Data.ByteString as S
44import Data.Text (Text)
55
6−asText :: Text -> Text
7−asText = id
8−
9−asInt :: Int -> Int
10−asInt = id
6+data Person = Person
7+ { name :: !Text
8+ , age :: !Int
9+ }
10+instance ToYaml Person where
11+ toYaml (Person n a) = mapping
12+ [ "name" .= n
13+ , "age" .= a
14+ ]
1115
1216main :: IO ()
1317main = do
14− let yb = array
15− [ mapping
16− [ "name" .= asText "Alice"
17− , "age" .= asInt 57
18− ]
19− , mapping
20− [ "name" .= asText "Bob"
21− , "age" .= asInt 23
22− ]
23− ]
24− S.putStr $ toByteString yb
18+ S.putStr $ toByteString
19+ [ Person "Alice" 57
20+ , Person "Bob" 23
21+ ]