protobuf-ts
protobuf-ts copied to clipboard
Automatic message registry
It will be hugely beneficial if we can create a registry of all proto messages automatically so that we don't need to pass typeRegistry for the Any type which is kind of annoying and hard to maintain.
The protobuf golang implementation does exactly this, whenever a proto generated file is imported it has line at the top of the file, something like:
registry.Register("ProtoMessage", ProtoMessage);
in fact the Any types are always automatically handled as far as the file is imported (and registered).
Definately a great development experience.
We can of course keep the typeRegistry option if we ever need to pass those manually in, otherwise it passes the registry by default
I've seen this from a different perspective so far: Users have to import to register anyway - so it can just as well be completely explicit. This works out best in C#, where users can easily scan an assembly for messages and register. No funny import side effects, no need to maintain.
I see your point though, and think this is beneficial. It would just need
- a
globalTypeRegistry, exported from @protobuf-ts/runtime, - a
globalTypeRegistry.pushfor eachexport const MyMessagein generated code - support in JSON read / write, probably simply by setting the default value for
typeRegistryto the global one.
It would also need proper testing, and a documentation update. I'm super constrained at the moment, but if you could PR this I can take care of the docs.
@timostamm I'll try to PR this weekend
Is this still something that we want? I had to implement that for us, if you'd be interested I could upstream that. In that case, I'd like some pointer on where to put the test for that, I'd guess into test-generated?
Hey Sebastian, I'd love to hear what approach you took. Is it similar to the globalTypeRegistry.push above?
Pretty much, I added a PR.