diff --git a/Changelog.md b/Changelog.md index 492911fc..4379c79d 100644 --- a/Changelog.md +++ b/Changelog.md @@ -5,6 +5,7 @@ ### Breaking Changes - Reexport transitive definitions from modules generated for `.proto` files with `import public` statements (#329). +- Add `methodOptions` to `HasMethodImpl` to provide custom method options. ### Backwards-Compatible Changes - Fix a potential naming conflict when message types and enum values diff --git a/package.yaml b/package.yaml index e6219937..dfbe18a1 100644 --- a/package.yaml +++ b/package.yaml @@ -18,6 +18,7 @@ extra-source-files: dependencies: - base >= 4.9 && < 4.13 + - bytestring == 0.10.* - containers >= 0.5 && < 0.7 - lens-family == 1.2.* - proto-lens == 0.5.* @@ -41,5 +42,4 @@ executables: main: protoc-gen-haskell.hs source-dirs: app dependencies: - - bytestring == 0.10.* - proto-lens-protoc diff --git a/src/Data/ProtoLens/Compiler/Definitions.hs b/src/Data/ProtoLens/Compiler/Definitions.hs index 1af1914d..bdc74c65 100644 --- a/src/Data/ProtoLens/Compiler/Definitions.hs +++ b/src/Data/ProtoLens/Compiler/Definitions.hs @@ -70,6 +70,7 @@ import Proto.Google.Protobuf.Descriptor , FieldDescriptorProto'Type(..) , FileDescriptorProto , MethodDescriptorProto + , MethodOptions , ServiceDescriptorProto ) import Proto.Google.Protobuf.Descriptor_Fields @@ -160,6 +161,7 @@ data MethodInfo = MethodInfo , methodOutput :: Text , methodClientStreaming :: Bool , methodServerStreaming :: Bool + , methodOptions :: MethodOptions } -- | Information about a single field of a proto message, @@ -352,6 +354,7 @@ collectServices fd = fmap (toServiceInfo $ fd ^. package) $ fd ^. service , methodOutput = fromString . T.unpack $ md ^. outputType , methodClientStreaming = md ^. clientStreaming , methodServerStreaming = md ^. serverStreaming + , methodOptions = md ^. options } messageAndEnumDefs :: diff --git a/src/Data/ProtoLens/Compiler/Generate.hs b/src/Data/ProtoLens/Compiler/Generate.hs index 52e6b447..9ab546f8 100644 --- a/src/Data/ProtoLens/Compiler/Generate.hs +++ b/src/Data/ProtoLens/Compiler/Generate.hs @@ -16,12 +16,14 @@ module Data.ProtoLens.Compiler.Generate( import Control.Arrow (second) +import qualified Data.ByteString.Char8 as B import qualified Data.Foldable as F import qualified Data.List as List import qualified Data.Map as Map import Data.Maybe (isJust) import Data.Monoid ((<>)) import Data.Ord (comparing) +import Data.ProtoLens (encodeMessage) import qualified Data.Set as Set import Data.String (fromString) import Data.Text (unpack) @@ -263,6 +265,14 @@ generateServiceDecls env si = (True, False) -> "Data.ProtoLens.Service.Types.ClientStreaming" (False, True) -> "Data.ProtoLens.Service.Types.ServerStreaming" (True, True) -> "Data.ProtoLens.Service.Types.BiDiStreaming" + -- methodOptions _ _ = decodeMessageOrDie (pack "...") + -- (where "..." is the encoded version of the proto message). + , instMatch + [ match "methodOptions" [pWildCard, pWildCard] + $ "Data.ProtoLens.decodeMessageOrDie" + @@ ("Data.ByteString.Char8.pack" + @@ stringExp (B.unpack $ encodeMessage $ methodOptions m)) + ] ] | m <- serviceMethods si , let instanceHead = tyPromotedString (T.unpack $ methodIdent m)