Marwes / schemafy

Crate for generating rust types from a json schema
MIT License
242 stars 51 forks source link

[Feature] Basic external ref support #31

Closed lloydmeta closed 4 years ago

lloydmeta commented 4 years ago

Closes #3

tl;dr Add basic support for "external" $ref fields that point to files.

When expanding fields, if field's ref is pointing to a .json file, resolve it, and merge all the types it references into the current parent Expander.

The changes in this PR do this by:

To be honest, I need this for another project I plan on working on, so mostly hacked around until the tests passed without paying too much attention to efficiency. Here to learn so suggestions are welcome :)

Things not handled thus far:

lloydmeta commented 4 years ago

A relatively easy solution would be to add the ability to hook into the generation and allow it to replace the type being generated for any given field. Then override the type of any fields holding a Schema and make it hold an Arc which would give us cheap clones.

Can you give a concrete example of how to do what you’re suggesting? You say easy but it doesn’t sounds trivial given the bootstrapping of the structs.

I could just wrap all the Schemas in Expander in Arc (or even Rc since we’re not threaded?) quite easily though I think and get cheap clones.

lloydmeta commented 4 years ago

I gave the reference-counted Schema idea a shot: it turns out we also need to at least RefCell the Schema because merge_all_of needs a mutable Schema

https://github.com/Marwes/schemafy/blob/81ba451290820b421a8831afced0eccb3d220a9e/schemafy_lib/src/lib.rs#L140-L165

called from

https://github.com/Marwes/schemafy/blob/81ba451290820b421a8831afced0eccb3d220a9e/schemafy_lib/src/lib.rs#L323

Not sure at this point if I'm getting further and further away from what you're envisioning, so it's the last commit and can easily be reverted if you just want to drive bit that yourself.

lloydmeta commented 4 years ago

I opened another PR that contains a commit (14aef62450e76f7bc60881faee6c5d5c0ae10ef7) that might be closer to what you envisioned wrt adding a trigger to wrap Schema in a reference counting type, please take a look.

lloydmeta commented 4 years ago

@Marwes I'm closing this because I think #32 is the way forward with cheaper clones due to the wrapping.