jsontypedef / json-typedef-codegen

A CLI tool that generates code from JSON Typedef schemas
https://jsontypedef.com/docs/tools/jtd-codegen
MIT License
157 stars 31 forks source link

Feature Request/Discussion: ProtoBuf definition file codegen backend #46

Open Nyrox opened 2 years ago

Nyrox commented 2 years ago

Heyo. First of all great job on building this tool, it's been a ton of fun for me in the last few days reading the code and playing around with it! We have some pretty high hopes for this becoming a much simpler and more ergonomic alternative to json schemas!

I am currently investigating whether it might be worth it to implement a mapping between the json type schemas and protobuffer files. The reason for this is that at SOUNDBOKS, we have a bunch of internal APIs that would be well fit to utilize jsontypedef for their interfaces, but we also have some firmware code that as to occasionally take data from these services, which currently requires an awkward conversion step, as parsing json in an embedded context is no-bueno. 😛

From a technical point of view I think this is actually fairly doable, I added a target for protobuf on a fork (https://github.com/SOUNDBOKS/json-typedef-codegen) and it already passes more than half the test suite and with that 80+% of the use cases I want. But there is still some annoying kinks to work out, in terms of finding a good 1-to-1 mapping between the two formats + with the current structure of the AST walker implementing Ref is not straight forward as far as I can tell, because proto does not natively support type aliases + I think I need to find a way to generate certain type definitions of enums and structs inside of other ones to prevent name collisions, which I also didn't immediately see an easy solution for (You can look at this file for an example of such a name clash. RootFooBar should ideally be generated as a sub-type of the RootFoo message and just be called Bar).

Anyway let me know if this at all interests you. I could also see the argument for this being out of scope, in which case I would prob. take some inspiration and develop it as a separate tool or something.

Cheers, Mark@SOUNDBOKS 😄