Handling protobuf 'well-known types' #148
Replies: 4 comments 1 reply
-
Zack, have you made progress getting bazel to work for you? |
Beta Was this translation helpful? Give feedback.
-
Don't forget about Another thing that should be implemented is metadata of generated files, for example, we need to know the type's name or its |
Beta Was this translation helpful? Give feedback.
-
Any update on this? |
Beta Was this translation helpful? Give feedback.
-
This is fixed with the rust rewrite. Now we support well known types! |
Beta Was this translation helpful? Give feedback.
-
I wanted to open a discussion about how to deal with 'well-known types' in protobuf, especially as they relate to JSON encoding. There's a couple of other pre-requisites that have to be figured out first.
Context
The protobuf definition includes some types, referred to in the official docs as 'well-known types', used for representing things like generic values and objects. A good example is
google.protobuf.Struct
, which is something like aRecord<string, any>
:Naïvely, this would actually be represented as a protobuf object that looks like this, using
google.protobuf.Value
(which is itself a bigoneof
able to represent a number of different scalar types):But the spec (and the first-party implementations) say this should serialize to JSON as:
I worked on the
--ts_opt=json_names
feature, which aims to bring the output of.toObject()
(and input to.fromObject()
) calls in line with the native JSON serialization of protobufs, and this is relevant to me because I think interoperability with the official JSON representation is important (especially for a language like JS/TS!)What I've Tried So Far
I wanted to start by creating a conformance test that uses just
google.protobuf.Struct
andgoogle.protobuf.Value
at first, but ran into some issues with the Bazel rules (issues that don't happen when just invokingprotoc
simply on the command-line). You can replicate the issues I'm seeing by just writing a simple proto file like the aboveSomeMessage
example, with build rules like this:I'm not super familiar with Bazel rule implementation, so I wouldn't mind some help figuring this out. The next step would be adding some special-casing in the compiler for
google.protobuf.Struct
andgoogle.protobuf.Value
, maybe just in thefromObject()
/toObject()
method implementations, guarded behind a flag. But due to the Bazel issues I haven't even gotten to that yet.Beta Was this translation helpful? Give feedback.
All reactions