Best way to generate Rescript from json schema?

Hi,

Given some JSON schema, I’d like to convert it to a set of rescript types. I don’t care about duplicates since there’s a graph like dependency.

I’d like to know the best way to codegen rescript. Is it possible to first build some AST, and then reuse some inner rescript tool to convert it to the rs syntax?

I would just use string interpolation to be honest. It’s probably much easier, that’s what they do in similar projects like ATD for example.

Then I’d use generators in bsconfig to call the executable that would generate the types.

1 Like

I’ve started looking into this problem, and approach I’m planning to use is:

  1. is to parse the JSON schema into an AST that reflects the way JSONSchema is expressed
  2. flatten the AST into a more restrictive AST (so that every sub-object can become its own record type, $ref’s can make sense, and complex magic like oneOf and allOf can carved out)
  3. generate ReScript code directly from the flattened-AST

At no point would I be using one of the internal compiler ASTs - it’s too much trouble.

For similar projects I’ve created that both do code generation, I spit out the code directly and let the builtin formatter clean it up, as its much quicker and doesn’t require compiler internals (which I don’t have the time to familiarise myself with):

2 Likes

I thought the team was unsure whether generators will stay there in the future.

I’d probably not run this script on every single compiled file, and rather run it as an npm script?

1 Like

We use an npm script too but we make sure to check/sync in CI.

1 Like