Last active
May 11, 2020 12:01
-
-
Save winpat/20a36739b60e1bcd7d5750f75b715d50 to your computer and use it in GitHub Desktop.
Aeson parsing problem
This file contains 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
src/Main.hs:50:38-51: error: Parse error in pattern: parseJSON | |
| | |
50 | for (HM.toList o) \((_, args) -> parseJSON args) | |
| ^^^^^^^^^^^^^^ |
This file contains 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 RecordWildCards #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE QuasiQuotes #-} | |
module Main where | |
import Data.ByteString (ByteString) | |
import Text.RawString.QQ | |
import qualified Data.HashMap.Strict as HM | |
import Data.Traversable | |
import Data.Foldable | |
import Data.Yaml | |
import Data.Aeson.Types | |
yamlData :: ByteString | |
yamlData = [r|--- | |
- file: | |
path: "test" | |
content: "help" | |
- file: | |
path: "test" | |
content: "Hello World!" | |
- other: | |
name: "other"|] | |
main :: IO () | |
main = do | |
let content = decodeEither' yamlData | |
case content of | |
Left err -> putStrLn $ "Parsing failed!" ++ show err | |
Right p -> putStrLn $ show (p :: Playbook) | |
return () | |
data Module | |
= File { path :: String , content :: String} | |
| Other { name :: String } | |
deriving (Show, Eq) | |
instance FromJSON Module where | |
parseJSON = withObject "module" $ \o -> asum [ | |
File <$> o .: "path" <*> o .: "content", | |
Other <$> o .: "name" ] | |
data Playbook = Playbook [Module] deriving (Show, Eq) | |
instance FromJSON Playbook where | |
parseJSON = withObject "playbook" $ \o -> do | |
-- We don't need the key of the JSON object | |
return $ Playbook $ for (HM.toList o) (\(_, args) -> parseJSON args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment