From 595cb6a3bfbc50348af44d4bbd981ba0c02e45dd Mon Sep 17 00:00:00 2001 From: Gabriel Gonzalez Date: Mon, 10 Feb 2020 10:19:29 -0800 Subject: [PATCH] Update `Arithmetic` example to use newer `proto3-suite` (#102) Related to https://github.com/awakesecurity/proto3-suite/issues/119 --- examples/tutorial/Arithmetic.hs | 71 +++++++++++++++++++-------------- grpc-haskell.cabal | 2 + 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/examples/tutorial/Arithmetic.hs b/examples/tutorial/Arithmetic.hs index dc42ee1..8892347 100644 --- a/examples/tutorial/Arithmetic.hs +++ b/examples/tutorial/Arithmetic.hs @@ -1,6 +1,8 @@ {-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} +{-# LANGUAGE TypeApplications #-} {-# LANGUAGE OverloadedStrings #-} {-# OPTIONS_GHC -fno-warn-unused-imports #-} {-# OPTIONS_GHC -fno-warn-name-shadowing #-} @@ -9,30 +11,35 @@ -- | Generated by Haskell protocol buffer compiler. DO NOT EDIT! module Arithmetic where import qualified Prelude as Hs -import qualified Proto3.Suite.DotProto as HsProtobuf -import qualified Proto3.Suite.Types as HsProtobuf import qualified Proto3.Suite.Class as HsProtobuf +import qualified Proto3.Suite.DotProto as HsProtobuf import qualified Proto3.Suite.JSONPB as HsJSONPB import Proto3.Suite.JSONPB ((.=), (.:)) +import qualified Proto3.Suite.Types as HsProtobuf import qualified Proto3.Wire as HsProtobuf -import Control.Applicative ((<*>), (<|>), (<$>)) import qualified Control.Applicative as Hs +import Control.Applicative ((<*>), (<|>), (<$>)) +import qualified Control.DeepSeq as Hs import qualified Control.Monad as Hs -import qualified Data.Text.Lazy as Hs (Text) import qualified Data.ByteString as Hs -import qualified Data.String as Hs (fromString) -import qualified Data.Vector as Hs (Vector) +import qualified Data.Coerce as Hs import qualified Data.Int as Hs (Int16, Int32, Int64) -import qualified Data.Word as Hs (Word16, Word32, Word64) +import qualified Data.List.NonEmpty as Hs (NonEmpty(..)) +import qualified Data.Map as Hs (Map, mapKeysMonotonic) import qualified Data.Proxy as Proxy -import qualified GHC.Generics as Hs +import qualified Data.String as Hs (fromString) +import qualified Data.Text.Lazy as Hs (Text) +import qualified Data.Vector as Hs (Vector) +import qualified Data.Word as Hs (Word16, Word32, Word64) import qualified GHC.Enum as Hs +import qualified GHC.Generics as Hs +import qualified Unsafe.Coerce as Hs import Network.GRPC.HighLevel.Generated as HsGRPC import Network.GRPC.HighLevel.Client as HsGRPC import Network.GRPC.HighLevel.Server as HsGRPC hiding (serverLoop) import Network.GRPC.HighLevel.Server.Unregistered as HsGRPC (serverLoop) - + data Arithmetic request response = Arithmetic{arithmeticAdd :: request 'HsGRPC.Normal Arithmetic.TwoInts Arithmetic.OneInt @@ -46,7 +53,7 @@ data Arithmetic request response = Arithmetic{arithmeticAdd :: (response 'HsGRPC.ClientStreaming Arithmetic.OneInt)} deriving Hs.Generic - + arithmeticServer :: Arithmetic HsGRPC.ServerRequest HsGRPC.ServerResponse -> HsGRPC.ServiceOptions -> Hs.IO () @@ -72,7 +79,7 @@ arithmeticServer optUserAgentSuffix = userAgentSuffix, optInitialMetadata = initialMetadata, optSSLConfig = sslConfig, optLogger = logger}) - + arithmeticClient :: HsGRPC.Client -> Hs.IO (Arithmetic HsGRPC.ClientRequest HsGRPC.ClientResult) @@ -85,13 +92,15 @@ arithmeticClient client ((Hs.pure (HsGRPC.clientRequest client)) <*> (HsGRPC.clientRegisterMethod client (HsGRPC.MethodName "/arithmetic.Arithmetic/RunningSum"))) - + data TwoInts = TwoInts{twoIntsX :: Hs.Int32, twoIntsY :: Hs.Int32} - deriving (Hs.Show, Hs.Eq, Hs.Ord, Hs.Generic) - + deriving (Hs.Show, Hs.Eq, Hs.Ord, Hs.Generic, Hs.NFData) + instance HsProtobuf.Named TwoInts where nameOf _ = (Hs.fromString "TwoInts") - + +instance HsProtobuf.HasDefault TwoInts + instance HsProtobuf.Message TwoInts where encodeMessage _ TwoInts{twoIntsX = twoIntsX, twoIntsY = twoIntsY} = (Hs.mconcat @@ -117,24 +126,24 @@ instance HsProtobuf.Message TwoInts where (HsProtobuf.Single "y") [] "")] - + instance HsJSONPB.ToJSONPB TwoInts where toJSONPB (TwoInts f1 f2) = (HsJSONPB.object ["x" .= f1, "y" .= f2]) toEncodingPB (TwoInts f1 f2) = (HsJSONPB.pairs ["x" .= f1, "y" .= f2]) - + instance HsJSONPB.FromJSONPB TwoInts where parseJSONPB = (HsJSONPB.withObject "TwoInts" (\ obj -> (Hs.pure TwoInts) <*> obj .: "x" <*> obj .: "y")) - + instance HsJSONPB.ToJSON TwoInts where toJSON = HsJSONPB.toAesonValue toEncoding = HsJSONPB.toAesonEncoding - + instance HsJSONPB.FromJSON TwoInts where parseJSON = HsJSONPB.parseJSONPB - + instance HsJSONPB.ToSchema TwoInts where declareNamedSchema _ = do let declare_x = HsJSONPB.declareSchemaRef @@ -153,13 +162,15 @@ instance HsJSONPB.ToSchema TwoInts where HsJSONPB._schemaProperties = HsJSONPB.insOrdFromList [("x", twoIntsX), ("y", twoIntsY)]}}) - + data OneInt = OneInt{oneIntResult :: Hs.Int32} - deriving (Hs.Show, Hs.Eq, Hs.Ord, Hs.Generic) - + deriving (Hs.Show, Hs.Eq, Hs.Ord, Hs.Generic, Hs.NFData) + instance HsProtobuf.Named OneInt where nameOf _ = (Hs.fromString "OneInt") - + +instance HsProtobuf.HasDefault OneInt + instance HsProtobuf.Message OneInt where encodeMessage _ OneInt{oneIntResult = oneIntResult} = (Hs.mconcat @@ -175,23 +186,23 @@ instance HsProtobuf.Message OneInt where (HsProtobuf.Single "result") [] "")] - + instance HsJSONPB.ToJSONPB OneInt where toJSONPB (OneInt f1) = (HsJSONPB.object ["result" .= f1]) toEncodingPB (OneInt f1) = (HsJSONPB.pairs ["result" .= f1]) - + instance HsJSONPB.FromJSONPB OneInt where parseJSONPB = (HsJSONPB.withObject "OneInt" (\ obj -> (Hs.pure OneInt) <*> obj .: "result")) - + instance HsJSONPB.ToJSON OneInt where toJSON = HsJSONPB.toAesonValue toEncoding = HsJSONPB.toAesonEncoding - + instance HsJSONPB.FromJSON OneInt where parseJSON = HsJSONPB.parseJSONPB - + instance HsJSONPB.ToSchema OneInt where declareNamedSchema _ = do let declare_result = HsJSONPB.declareSchemaRef @@ -205,4 +216,4 @@ instance HsJSONPB.ToSchema OneInt where Hs.Just HsJSONPB.SwaggerObject}, HsJSONPB._schemaProperties = HsJSONPB.insOrdFromList - [("result", oneIntResult)]}}) + [("result", oneIntResult)]}}) \ No newline at end of file diff --git a/grpc-haskell.cabal b/grpc-haskell.cabal index a3a17a8..e2ef30b 100644 --- a/grpc-haskell.cabal +++ b/grpc-haskell.cabal @@ -122,6 +122,7 @@ executable arithmetic-server , async , bytestring == 0.10.* , containers >=0.5 && <0.7 + , deepseq , grpc-haskell , grpc-haskell-core , optparse-generic @@ -145,6 +146,7 @@ executable arithmetic-client , async , bytestring == 0.10.* , containers >=0.5 && <0.7 + , deepseq , grpc-haskell , grpc-haskell-core , optparse-generic