gRPC-haskell icon indicating copy to clipboard operation
gRPC-haskell copied to clipboard

Generic runtime representation

Open tim2CF opened this issue 5 years ago • 7 comments

Hello! Is there some sort of generic runtime representation of protobuf types generated from protobuf files? Something similar to this (but for protobuf) https://hackage.haskell.org/package/aeson-1.5.3.0/docs/Data-Aeson.html#t:Value

tim2CF avatar Sep 04 '20 08:09 tim2CF

@tim2CF: There is not a generic representation, as far as I know.

Gabriella439 avatar Sep 04 '20 16:09 Gabriella439

I meant not exactly GHC.Generics, but some sort of protobuf AST which can be used to fold protobuf messages in generic way. Or at least some sort of protobuf reflection API which can give information about message field types and indexes in terms of Haskell.

tim2CF avatar Sep 04 '20 19:09 tim2CF

@tim2CF: Yeah, I understand what you mean, but I don't think there is such a representation. Protobuf is not like CBOR or JSON so there is no standard generic AST for the data.

Gabriella439 avatar Sep 04 '20 20:09 Gabriella439

Well, maybe not data itself, but schema of data. Information about, types, names and indexes of messages in form of Haskell terms. Protobuf reflection API exists in some implementations. I'm not sure is it part of standard itself or not, but you can take a look for example to other Haskell implementation here

https://hackage.haskell.org/package/proto-lens-0.7.0.0/docs/Data-ProtoLens-Message.html#g:1

It's quite useful in some cases, for example for generalized Arbitrary instance for any protobuf message

http://hackage.haskell.org/package/proto-lens-arbitrary-0.1.2.9/docs/src/Data.ProtoLens.Arbitrary.html#arbitraryMessage

tim2CF avatar Sep 04 '20 20:09 tim2CF

@tim2CF: Oh, so if all you need is the AST of the .proto file then our proto3-suite package supports that. This module is the entry point to parsing the AST from a .proto file: https://hackage.haskell.org/package/proto3-suite-0.4.0.0/docs/Proto3-Suite-DotProto-Generate.html

Gabriella439 avatar Sep 04 '20 20:09 Gabriella439

@Gabriel439 cool, thanks for the link! I'm building library for signing/verifying protobuf messages in deterministic way, and wanna to support both Haskell implementations, because I'm using both of them. We just started this library, but eventually it will be there

https://github.com/coingaming/signable

In general for me more preferable way is to get proto information through type class instance, like in proto-lens (I basically can give Proxy MyMessage and get all meta info about MyMessage). I think in your library I can get all meta information exactly the same way through dotProto method, because every generated protobuf message implements it, right?

https://hackage.haskell.org/package/proto3-suite-0.4.0.0/docs/Proto3-Suite-Class.html#v:dotProto

tim2CF avatar Sep 04 '20 20:09 tim2CF

@tim2CF: Yeah, I believe the dotProto method will allow you to retrieve metadata about fields

Gabriella439 avatar Sep 10 '20 20:09 Gabriella439