protobuf-ts icon indicating copy to clipboard operation
protobuf-ts copied to clipboard

Automatic message registry

Open fenos opened this issue 4 years ago • 5 comments

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

fenos avatar Jul 17 '21 12:07 fenos

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.push for each export const MyMessage in generated code
  • support in JSON read / write, probably simply by setting the default value for typeRegistry to 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 avatar Jul 17 '21 23:07 timostamm

@timostamm I'll try to PR this weekend

fenos avatar Jul 22 '21 09:07 fenos

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?

sessfeld avatar Aug 29 '22 17:08 sessfeld

Hey Sebastian, I'd love to hear what approach you took. Is it similar to the globalTypeRegistry.push above?

timostamm avatar Sep 07 '22 09:09 timostamm

Pretty much, I added a PR.

sessfeld avatar Sep 07 '22 19:09 sessfeld