Skip to content

Commit

Permalink
Add methodOptions to HasMethodImpl to provide custom method optio…
Browse files Browse the repository at this point in the history
…ns. (google#331)

We provide the whole proto message, rather than pulling off individual fields. The set of fields may change for different builds of proto-lens (or based on extensions, though we can't see those directly due to google#27).

Internally, we save the proto message in the source file as an encoded string, and decode it into a Haskell type lazily.
  • Loading branch information
judah authored Aug 30, 2019
1 parent 007b286 commit 7bcdde3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -41,5 +42,4 @@ executables:
main: protoc-gen-haskell.hs
source-dirs: app
dependencies:
- bytestring == 0.10.*
- proto-lens-protoc
3 changes: 3 additions & 0 deletions src/Data/ProtoLens/Compiler/Definitions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import Proto.Google.Protobuf.Descriptor
, FieldDescriptorProto'Type(..)
, FileDescriptorProto
, MethodDescriptorProto
, MethodOptions
, ServiceDescriptorProto
)
import Proto.Google.Protobuf.Descriptor_Fields
Expand Down Expand Up @@ -160,6 +161,7 @@ data MethodInfo = MethodInfo
, methodOutput :: Text
, methodClientStreaming :: Bool
, methodServerStreaming :: Bool
, methodOptions :: MethodOptions
}

-- | Information about a single field of a proto message,
Expand Down Expand Up @@ -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 ::
Expand Down
10 changes: 10 additions & 0 deletions src/Data/ProtoLens/Compiler/Generate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7bcdde3

Please sign in to comment.