Created
March 10, 2020 02:46
-
-
Save ldub/e546e27d5829c2bc55d244bcfaeb64f4 to your computer and use it in GitHub Desktop.
Xmlbf Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE OverloadedLists #-} | |
module Main where | |
import qualified Data.ByteString as BS | |
import Data.HashMap.Strict (HashMap) | |
import Data.Text (Text) | |
import Data.Text.Lazy (toStrict, fromStrict) | |
import qualified Xmlbf as X | |
import qualified Xmlbf.Xeno as Xx | |
import Lib | |
data AtomFeed = AtomFeed | |
{ feedName :: !Text | |
, feedAttrs :: !(HashMap Text Text) | |
, feedChildren :: ![X.Node] | |
} deriving (Show, Eq) | |
instance X.ToXml AtomFeed where | |
toXml = \(AtomFeed n as cs) -> | |
let title = X.element "atom:title" [] (X.text (fromStrict n)) | |
in X.element "atom:feed" as (title ++ cs) | |
instance X.FromXml AtomFeed where | |
fromXml = X.pElement "atom:feed" $ do | |
title <- X.pElement "atom:title" X.pText | |
attrs <- X.pAttrs | |
pure (AtomFeed (toStrict title) attrs []) | |
rawFeed :: BS.ByteString | |
rawFeed = | |
"<atom:feed xmlns:atom=\"http://www.w3.org/2005/Atom\">\ | |
\ <atom:title>ServicePlan Feed</atom:title>\ | |
\</atom:feed>" | |
expectedFeed :: AtomFeed | |
expectedFeed = AtomFeed | |
{ feedName = "ServicePlan Feed" | |
, feedAttrs = [("xmlns:atom", "http://www.w3.org/2005/Atom")] | |
, feedChildren = [] | |
} | |
actualFeed :: AtomFeed | |
Right actualFeed = X.runParser X.fromXml =<< Xx.fromRawXml rawFeed | |
main :: IO () | |
main = do | |
putStrLn "Raw Feed:" | |
putStrLn $ show rawFeed | |
putStrLn "----------------" | |
putStrLn "Expected Feed:" | |
putStrLn $ show expectedFeed | |
putStrLn "----------------" | |
putStrLn "Actual Feed:" | |
putStrLn $ show actualFeed | |
putStrLn "----------------" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment