This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encounter runtime parse error in ghci representation issue #4
- Loading branch information
Showing
3 changed files
with
41 additions
and
39 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,35 @@ | ||
module ProtoRoute.Message | ||
( constructProtoMessage | ||
, MessageName | ||
, FieldName | ||
, FieldValue | ||
) where | ||
( MsgName (..) | ||
, FieldName (..) | ||
, FVType (..) | ||
, Msg (..) | ||
, FieldValue (..) | ||
, constructProtoMsg) where | ||
|
||
import Data.ProtoLens | ||
import Data.Text (pack) | ||
import ProtoExports | ||
import Data.Text (pack, Text) | ||
|
||
newtype MsgName = MN Text deriving (Show) | ||
newtype FieldName = FN Text deriving (Show) | ||
|
||
newtype MessageName = MessageName { unMessageName :: Text } | ||
newtype FieldName = FieldName { unFieldName :: Text } | ||
data FieldValue = FV [Int] | FV [Text] | FV [FieldValue] | ||
data FieldValue a = Req a | Opt (Maybe a) | Rep [a] deriving (Show) | ||
data FVType = FieldText (FieldValue Text) | FieldInt (FieldValue Int) | | ||
FieldMsg (FieldValue Msg) deriving (Show) | ||
-- Typesafe representation of a protobuf mesage | ||
data Msg = Msg | ||
{ messageName :: MsgName | ||
, messageFields :: [(FieldName, FVType)] | ||
} deriving (Show) | ||
|
||
constructProtoMessage :: MessageName -> [(FieldName, FieldValue)] -> Text | ||
constructProtoMessage msgName namesVals = msgName ++ pack " {" | ||
constructProtoMsg :: MsgName -> [(FieldName, FVType)] -> Text | ||
constructProtoMsg msgName namesVals = | ||
pack (show msgName ++ " {" ++ addRest msgName namesVals ++ "} ") | ||
where | ||
singleEntry = pack ("_" ++ msgName ++ "\'" ++ fn ++ " = " ++ pack . show fv) | ||
singleEntry :: MsgName -> (FieldName, FVType) -> String | ||
singleEntry name (fn, fv) = | ||
"_" ++ show name ++ "\'" ++ show fn ++ " = " ++ show fv | ||
|
||
addRest :: MessageName -> [(FieldName, FieldValue)] -> Text | ||
addRest _ [] = pack "" | ||
addRest _ [(fn, fv)] = singleEntry | ||
addRest msgName (fn,fv):others = | ||
pack (singleEntry ++ ", " ++ addRest msgName rest) | ||
addRest :: MsgName -> [(FieldName, FVType)] -> String | ||
addRest _ [] = "" | ||
addRest name [(fn, fv)] = singleEntry name (fn, fv) | ||
addRest name ((fn, fv) : others) = | ||
singleEntry name (fn, fv) ++ ", " ++ addRest name others |