Custom scalar type with own serializer
What language does this apply to? It could be a feature for all languages, but I need it mainly in C#. C++ and node in addition. I would need it only for proto3, but I think it is independent from them.
Describe the problem you are trying to solve. Currently I started developing a API with GRPC where the server is implemented in C# and clients can be at the end in any language, but in first step I have C#, Node and C++ in focus. Defining the interface in IDL and generating code (service base class, client stub, request & response messages) is exactly what we need, because we don't want to spend lot of time in maintaining all the different clients. So far no problem. But have one problem where the usage is not nice and it could have performance impacts to the solution at all.
At some point of interface design you need to define the datatype of a request/response message field. I know that it is possible to define sub-messages which can be used here. In the common interface definition (proto file) this is also fine, because it is now programming language independent.
In our API we have some message types I would call "scalar data types". They are definitions on top of basic types (string, int, ..) or structs which gives them a specific semantic. Some examples for better understanding:
message BitField32
{
uint32 value = 1;
}
The BitField32 gives an 32 bit integer value the semantic of a field of bits with a length of 32 bits. In the implementation it provides methods which allows easy manipulating the bits.
message ObjectIdentifier {
uint32 systemId = 1;
uint32 objectId = 2;
uint32 elementId = 3;
uint32 attributeId = 4;
}
The second example is a implementation of a struct which combines several integer values to one object which gives them the semantic of addressing our solution objects by ID.
I know that the generated code can be easy extended by use of partial class, but this does not work for me for several reasons: The structs and classes are not defined by me, they are defined in a lower level of our application and it is not possible to bring a reference to protobuf to this assembly. The objects are already delivered and used by customers and therefore I must consider also compatibility. The objects are used by other solutions which are independent from our GRPC solution.
The only possible solution right now is to do it similar like the WellKnownTypes like timestamp.
In this case we need in our workflow always an additional conversion step from the proto type to the real type or vice versa.
This solution works and is also what we would implement in first step, but this additional conversion step is hard to understand by or customers, who will use the API. Furthermore this extra conversion step will sure not increase the performance.
Describe the solution you'd like
From a basic perspective I want like to archive that the generated code for common request/response messages uses instead of the generated proto types the already existing self implemented types, when there is such a type existing. If not than it generates the type like now. But as the protoc compiler can't know how the memory model of the self implemented type is, it cant generate the seralizer code for it. This means this part must be implemented by my own.
I took a look at the code which is generated by the protoc compiler for C#. I think the output can be logically split into following parts:
- Descriptor: Holding the reflection information about the types which are defined in IDL
- Message implementation: Properties for defined fields, some convenient like
Equals() - Serializer: Writer and parser logic for the defined message properties/fields
The descriptor logic is already most of it in a own class independent from the generated message type. I think this could stay almost as it is. The message implementation is not needed, because it is self implemented. The serializer is still needed but must be implemented partly manually.
The basic solution idea: The protoc compiler generates for such messages just a abstract base class which is representing the serializer (writer + parser) and defines some virtual methods which needs to be implemented in concrete self-implemented class. This base class has also properties and fields (e.g. index number, Parser, Descriptor, ...) which are currently available at generated message class. I think in the protoc plugin this makes no big difference.
Important for me is also that the solution work with immutable structs. I think this is only possible if there is a own interface definition for the serializer, because the current interface implies that the parser updates the current object, which is not possible for such structs. Here we would need the result as reference parameter so that it is the decision of implementation if it updates current object or creates new one.
Open question is: How does the protoc compiler plugin know which messages has there own serializer?
- The easiest way is to define a option at the message type. But this has the disadvantage that interface definition defines how it is implemented.
- Therefore I think it would be better to define it as command argument at protoc compiler.
- Or in a configuration file beside the other files which is given to the protoc compiler. This could be also a proto file.
Describe alternatives you've considered
There is beside Grpc.Core and Grpc-DotNet based on ASP.NET core also a third implementation protobuf-net.Grpc for C#. There it is possible to do also a code-first which maybe solve the problem. But this solution uses its own ecosystem (inclusive code generator) and is therefore incompatible to Grpc-DotNet. And I think it is less maintained, less documented and not so feature-rich (at least my impression). This are the main reasons why it is not a option for us.
We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.
This issue is labeled inactive because the last activity was over 90 days ago.
We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.
This issue was closed and archived because there has been no new activity in the 14 days since the inactive label was added.