awakesecurity / gRPC-haskell

Haskell gRPC support
https://hackage.haskell.org/package/grpc-haskell
Apache License 2.0
238 stars 75 forks source link

Export the constructor for the `GRPC` type #142

Closed riz0id closed 1 year ago

riz0id commented 1 year ago

This PR exports the constructor GRPC datatype. gRPC-haskell uses this datatype to witness proof that gRPC server has been started. A GRPC value can only be obtained via the withGRPC function. It seems like this was done to prevent a GRPC value from being created without first initializing the gRPC server.

Disallowing access to the constructor for GRPC makes sense, but it also makes it impossible write custom bracketing functions for initialization and cleanup of gRPC servers with initGrpc and grpcShutdown.

Having access to this constructor would be very helpful when writing tests that need to create a gRPC server for the duration of the test with Test.Tasty.withResource. Otherwise, the only option I have to obtain a GRPC value (as far as I know) is something like:

import Unsafe.Coerce (unsafeCoerce)

unsafeMakeGRPC :: GRPC 
unsafeMakeGRPC = unsafeCoerce ()
riz0id commented 1 year ago

This has also bit me and I've considered doing the same thing but never got around to this. Is there any way a pattern synonym could be exported instead that would construct / match on the constructor that we could name with the prefix "unsafe"?

On second thought, it would be better to keep the constructor for GRPC hidden and instead export a version of initGrpc that returns a GRPC value. This is what withGRPC does currently and would expose a way of obtaining a GRPC value that doesn't break the any assumptions behind how GRPC is used internally.

ixmatus commented 1 year ago

Yeah, I like that idea better too.

intractable commented 1 year ago

I agree w/ the latter proposed solution, and I too have wanted to do something along these lines for a while. Thanks, @riz0id!

ixmatus commented 1 year ago

@riz0id could you update the PR description for posterity?