packages feed

http2-grpc-proto-lens (empty) → 0.1.0.0

raw patch · 6 files changed

+148/−0 lines, 6 filesdep +basedep +binarydep +bytestringsetup-changed

Dependencies added: base, binary, bytestring, case-insensitive, http2-grpc-types, proto-lens, zlib

Files

+ ChangeLog.md view
@@ -0,0 +1,3 @@+# Changelog for http2-grpc-proto-lens++## Unreleased changes
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2018++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Author name here nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,3 @@+# http2-grpc-types++A module to put in common HTTP2/gRPC types, values, and helpers.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ http2-grpc-proto-lens.cabal view
@@ -0,0 +1,46 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.31.2.+--+-- see: https://github.com/sol/hpack+--+-- hash: 55c756b55c80ecf8c0e187e1f0b8ec7ff8d54ad1ad8f84a823599f9c2ea01582++name:           http2-grpc-proto-lens+version:        0.1.0.0+synopsis:       Encoders based on `proto-lens` for gRPC over HTTP2.+description:    Please see the README on GitHub at <https://github.com/haskell-grpc-native/http2-grpc-haskell/blob/master/http2-grpc-proto-lens/README.md>+category:       Network+homepage:       https://github.com/haskell-grpc-native/http2-grpc-haskell#readme+bug-reports:    https://github.com/haskell-grpc-native/http2-grpc-haskell/issues+author:         Lucas DiCioccio, Alejandro Serrano+maintainer:     lucas@dicioccio.fr+copyright:      2018, 2019 Lucas DiCioccio, Alejandro Serrano+license:        BSD3+license-file:   LICENSE+build-type:     Simple+extra-source-files:+    README.md+    ChangeLog.md++source-repository head+  type: git+  location: https://github.com/haskell-grpc-native/http2-grpc-haskell++library+  exposed-modules:+      Network.GRPC.HTTP2.ProtoLens+  other-modules:+      Paths_http2_grpc_proto_lens+  hs-source-dirs:+      src+  ghc-options: -Wall+  build-depends:+      base >=4.11 && <5+    , binary >=0.8.5 && <0.11+    , bytestring >=0.10.8 && <0.11+    , case-insensitive >=1.2.0 && <1.3+    , http2-grpc-types+    , proto-lens >=0.5 && <0.7+    , zlib >=0.6.2 && <0.7+  default-language: Haskell2010
+ src/Network/GRPC/HTTP2/ProtoLens.hs view
@@ -0,0 +1,64 @@+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE UndecidableInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE TypeFamilies #-}++module Network.GRPC.HTTP2.ProtoLens where++import           Data.Binary.Builder (fromByteString, singleton, putWord32be)+import           Data.Binary.Get (getByteString, getInt8, getWord32be, runGetIncremental)+import qualified Data.ByteString.Char8 as ByteString+import           Data.ProtoLens.Encoding (encodeMessage, decodeMessage)+import           Data.ProtoLens.Message (Message)+import           Data.ProtoLens.Service.Types (Service(..), HasMethod, HasMethodImpl(..))+import           Data.Proxy (Proxy(..))+import           GHC.TypeLits (Symbol, symbolVal)++import Network.GRPC.HTTP2.Types+import Network.GRPC.HTTP2.Encoding++-- | A proxy type for giving static information about RPCs.+data RPC (s :: *) (m :: Symbol) = RPC++instance (Service s, HasMethod s m) => IsRPC (RPC s m) where+  path rpc = "/" <> pkg rpc Proxy <> "." <> srv rpc Proxy <> "/" <> meth rpc Proxy+    where+      pkg :: (Service s) => RPC s m -> Proxy (ServicePackage s) -> HeaderValue+      pkg _ p = ByteString.pack $ symbolVal p++      srv :: (Service s) => RPC s m -> Proxy (ServiceName s) -> HeaderValue+      srv _ p = ByteString.pack $ symbolVal p++      meth :: (Service s, HasMethod s m) => RPC s m -> Proxy (MethodName s m) -> HeaderValue+      meth _ p = ByteString.pack $ symbolVal p +  {-# INLINE path #-}++instance (Service s, HasMethod s m, i ~ MethodInput s m)+         => GRPCInput (RPC s m) i where+  encodeInput _ = encode+  decodeInput _ = decoder++instance (Service s, HasMethod s m, i ~ MethodOutput s m)+         => GRPCOutput (RPC s m) i where+  encodeOutput _ = encode+  decodeOutput _ = decoder++-- | Decoder for gRPC/HTTP2-encoded Protobuf messages.+decoder :: Message a => Compression -> Decoder (Either String a)+decoder compression = runGetIncremental $ do+    isCompressed <- getInt8      -- 1byte+    let decompress = if isCompressed == 0 then pure else _decompressionFunction compression+    n <- getWord32be             -- 4bytes+    decodeMessage <$> (decompress =<< getByteString (fromIntegral n))++-- | Encodes as binary using gRPC/HTTP2 framing.+encode :: Message m => Compression -> m -> Builder+encode compression plain =+    mconcat [ singleton (if _compressionByteSet compression then 1 else 0)+            , putWord32be (fromIntegral $ ByteString.length bin)+            , fromByteString bin+            ]+  where+    bin = _compressionFunction compression $ encodeMessage plain