From f1a1c757a415a0db49d8fa4ad021e509b7d90061 Mon Sep 17 00:00:00 2001 From: Flavio Corpa Date: Mon, 3 Feb 2020 15:06:18 +0100 Subject: [PATCH] =?UTF-8?q?Finish=20parsing=20protocols!=20=F0=9F=8E=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Language/Avro/Parser.hs | 24 ++++++++++++++++++++++-- src/Language/Avro/Types.hs | 10 ++++++---- test/Spec.hs | 4 ++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/Language/Avro/Parser.hs b/src/Language/Avro/Parser.hs index 3dfc37e..fa7ebd7 100644 --- a/src/Language/Avro/Parser.hs +++ b/src/Language/Avro/Parser.hs @@ -91,9 +91,29 @@ parseImport = parseProtocol :: MonadParsec Char T.Text m => m Protocol parseProtocol = - Protocol <$> optional parseNamespace <* reserved "protocol" + buildProtocol <$> optional parseNamespace <* reserved "protocol" <*> identifier - <*> braces (many parseImport) -- TODO: here goes more things! + <*> braces (many serviceThing) + where + buildProtocol :: Maybe Namespace -> T.Text -> [ProtocolThing] -> Protocol + buildProtocol ns name things = + Protocol + ns + name + [i | ProtocolThingImport i <- things] + [t | ProtocolThingType t <- things] + [m | ProtocolThingMethod m <- things] + +data ProtocolThing + = ProtocolThingImport ImportType + | ProtocolThingType Schema + | ProtocolThingMethod Method + +serviceThing :: MonadParsec Char T.Text m => m ProtocolThing +serviceThing = + ProtocolThingImport <$> parseImport + <|> ProtocolThingType <$> parseSchema + <|> ProtocolThingMethod <$> parseMethod parseVector :: MonadParsec Char T.Text m => m a -> m (Vector a) parseVector t = fromList <$> braces (lexeme $ sepBy1 t $ symbol ",") diff --git a/src/Language/Avro/Types.hs b/src/Language/Avro/Types.hs index a047429..5b44f4e 100644 --- a/src/Language/Avro/Types.hs +++ b/src/Language/Avro/Types.hs @@ -1,4 +1,6 @@ -module Language.Avro.Types where +module Language.Avro.Types ( + module Language.Avro.Types, Schema (..) +) where import Data.Avro.Schema import qualified Data.Text as T @@ -7,9 +9,9 @@ data Protocol = Protocol { ns :: Maybe Namespace, pname :: T.Text, - imports :: [ImportType] - -- TODO: , types :: [Schema] - -- TODO: , messages :: [Method] + imports :: [ImportType], + types :: [Schema], + messages :: [Method] } deriving (Eq, Show) diff --git a/test/Spec.hs b/test/Spec.hs index 63c43a7..c92e10e 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -168,6 +168,8 @@ main = hspec $ do Nothing "PeopleService" [IdlImport "People.avdl"] + [] + [] ) it "should parse with namespace" $ parse parseProtocol "" (T.unlines simpleProtocol) @@ -176,6 +178,8 @@ main = hspec $ do (Just (Namespace ["example", "seed", "server", "protocol", "avro"])) "PeopleService" [IdlImport "People.avdl"] + [] + [] ) describe "Parse services" $ do it "should parse simple messages" $