Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

create a custom data types in proto buffer

80 views
Skip to first unread message

jawad arbahi

unread,
Mar 10, 2025, 1:39:15 PMMar 10
to Protocol Buffers
Hello everyone, I want to create my own data types in Protocol Buffers (protobuf). For example, I need a UInt8 (1-byte unsigned integer). If I want to add this data type, can I use add-ons or fork protobuf and customize it for my needs? What do you think?

Marc Gravell

unread,
Mar 10, 2025, 2:02:53 PMMar 10
to jawad arbahi, Protocol Buffers

I think this doesn't fit inside any existing wire-type, which means there is no protocol-compliant way of adding such data. You could create your own fork that adds a custom wire-type (there are a couple of unused values), but it would no longer be protobuf and would not be compatible with any implementation, so you'd also need to rework all the read/write logic on any platforms that you're using. IMO that would be a terrible idea, and you'd be better off just using a varint and eating the extra byte for values in the range 128-255.


On Mon, 10 Mar 2025, 17:39 jawad arbahi, <jawad.a...@gmail.com> wrote:
Hello everyone, I want to create my own data types in Protocol Buffers (protobuf). For example, I need a UInt8 (1-byte unsigned integer). If I want to add this data type, can I use add-ons or fork protobuf and customize it for my needs? What do you think?

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/protobuf/0602b674-482e-4da5-a9a6-9031908d27a0n%40googlegroups.com.

ARBAHI Jawad

unread,
Mar 14, 2025, 3:25:42 PMMar 14
to Protocol Buffers

Regarding the subject of creating my own data types, I discovered that I can use FieldOptions and create a custom plugin in Protocol Buffers. This allows me to define custom FieldOptions and apply them to generated fields.

For example, if we have a bytes field in a ProtoBuf schema, we can define a FieldOption to specify the offset and length of the field. Then, the generated code can use this FieldOption to correctly parse the field in the response.

Reply all
Reply to author
Forward
0 new messages