Extending the code generator for custom "well-known" types

243 views
Skip to first unread message

Raphaël Barrois

unread,
Jun 8, 2021, 3:35:04 PM6/8/21
to prot...@googlegroups.com
Hi,

I am currently looking at ways of extending the generated code, in order to
add some helpers for some messages which map to standard types in our
codebase (think UUID, IPv6 — akin to the extended features awarded to the
well-known types).


My goal is to be able to go from:

message User {
example.wellknown.UUID id = 1;
}

To (for Python):

>>> user.id.to_uuid()
UUID("4fc93ada-dded-4737-9a5c-e3811c37f99d")
>>> user.id.from_uuid(UUID("002ce13b-31bc-4332-80f9-51f83a49eb65"))


I thought it would be possible to provide, for my own types, the same kind
of extended features as the "well-known types" enjoy.
However, I couldn't find how to do that in the documentation; and a look
at the Python implementation showed that these extensions are hardcoded in
the codebase (see
https://github.com/protocolbuffers/protobuf/blob/master/python/google/protobuf/internal/python_message.py#L151-L152).

Have I missed some part of the documentation? What is the common pattern
for such extensions?

Thanks,

--
Raphaël Barrois

Derek Perez

unread,
Jun 8, 2021, 4:15:58 PM6/8/21
to Raphaël Barrois, Protocol Buffers
well known types are not extendable in this way, currently. 

By their nature, they are not designed to support custom types as they wouldn't exactly be well known by our definition of well known (meaning, known to all).

--
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 on the web visit https://groups.google.com/d/msgid/protobuf/a591a464-ab78-47a5-b837-29d6fdec310c%40gmail.com.

Raphaël Barrois

unread,
Jun 9, 2021, 3:15:52 AM6/9/21
to Derek Perez, Protocol Buffers
On Tuesday, 8 June 2021 22:15:36 CEST, Derek Perez wrote:
> well known types are not extendable in this way, currently.
>
> By their nature, they are not designed to support custom types as they
> wouldn't exactly be well known by our definition of well known (meaning,
> known to all).

Hello,

I am well aware of that fact!
My idea was "If it is possible to do that with Google's well-known types,
surely I can use the same method to have a better API for our internal
types".

However, it appears that the enhanced support for well-known types is
hardcoded into the library. Would the right path here be to write a
compiler plugin, to adjust the generated code in a similar manner?

--
Raphaël Barrois



>
> On Tue, Jun 8, 2021 at 12:35 PM Raphaël Barrois <raphael...@gmail.com>
> wrote:
>
>> Hi,
>>
>> I am currently looking at ways of extending the generated
>> code, in order to
>> add some helpers for some messages which map to standard types in our
>> codebase (think UUID, IPv6 — akin to the extended features awarded to the
>> well-known types). ...
>
>

Reply all
Reply to author
Forward
0 new messages