specta icon indicating copy to clipboard operation
specta copied to clipboard

How to export a specific type and all its children

Open IgnisDa opened this issue 1 year ago • 7 comments

The export function only exports the given type and not its children. How can I export a type and its children?

IgnisDa avatar Jul 24 '23 05:07 IgnisDa

export is only designed to export a single type, to export a type + all its dependent types you'll need to iterate through the type_map that collects all the types that specta has walked into itself. Specta is a fairly low level library atm and is best used with something that can collect types for you, like rspc or tauri-specta. The way rspc is able to export all dependent types is by maintaining a single type_map that is passed to all invocations of Type::reference, and then the types are converted to TS in a loop to construct the final bindings.

Brendonovich avatar Jul 24 '23 10:07 Brendonovich

I am going to reopen this issue as a reminder to myself that a better solution needs to exist here.

Brendan is completely right in the fact that the current APIs around dealing and exporting dependant types are very low-level and there is no reason that should remain the case. I think it has mostly ended up that way as we have added APIs into Specta as required for rspc and tauri-specta without really thinking super heavily about using Specta without a framework wrapping it.

It's worth noting that the export crate feature (example) does work with exporting dependant types out of the box and I think this will cover many use-cases. It's entirely possible it's not what your going for and that's all good but just wanted to mention it incase you haven't noticed it exists.

oscartbeaumont avatar Jul 26 '23 06:07 oscartbeaumont

Yes I have noticed it. However, the way I want to use specta is that I want to write the export of different types to different files. If I understand correctly, specta right now works by appending each registered type to a typemap. That does not satisfy my use-case.

IgnisDa avatar Jul 26 '23 06:07 IgnisDa

Completely fair.

We could potentially allow the user to define a function to decide the file to put the types into from the ExportConfiguration because I feel like it has come up before but idk how useful that would be unless you had some well-defined measure for what goes in each file. I am fairly sure with #72 (which will be part of Specta v2) we will store the Rust module path on each type so maybe that could be used here for automatic module-based sorting?

Out of curiosity are you organising your types by module or some internal concept?

I will keep this all in mind for when I next have time to work on Specta.

oscartbeaumont avatar Jul 26 '23 07:07 oscartbeaumont

Out of curiosity are you organising your types by module or some internal concept?

No, I just have them in one models.rs. Here is an example of an export by specta.

Schematic has the behavior of just exporting the given type and its children. Here is an export using schematic.

The reason I am using specta and not schematic is because schematic is used for configuration and I just want to export the type signature.

IgnisDa avatar Jul 26 '23 07:07 IgnisDa

On a different note, does specta not export docs for each field?

I have added doc comments for the field but specta does not export them. https://github.com/IgnisDa/ryot/blob/1dea787774047b40a20fea35143fef783b004c27/apps/backend/src/models.rs#L517-L572

IgnisDa avatar Jul 26 '23 07:07 IgnisDa

I don't think it does right now. Nothing would be preventing that from working if added to the macros, was just an oversight.

oscartbeaumont avatar Jul 26 '23 08:07 oscartbeaumont