Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeBox-Codegen #17

Merged
merged 26 commits into from
Jul 31, 2023
Merged

TypeBox-Codegen #17

merged 26 commits into from
Jul 31, 2023

Conversation

sinclairzx81
Copy link
Owner

This PR implements compiler support various reference models used in TB code generation. The options are inline, ordered and cyclic with the latter option used to express schematics representing mutual recursive types (specifically for ArkType code generation). The reference model can be configured by way of option on TypeScriptToTypeBox. The default is inline

const code = TypeScriptToTypeBox.Generate('...', {
   referenceModel: 'cyclic' // default is 'inline'
})

Inline

const A = Type.Object({
  x: Type.String(),
})
const B = Type.Object({
  a: A // javascript reference
})

Ordered

const A = Type.Object({
  x: Type.String(),
}, { $id: 'A' })
const B = Type.Object({
  a: Type.Ref(A) // required A be topologically ordered first
})

Cyclic

const A = Type.Object({
  b: Type.Unsafe({ [Kind]: 'Ref', $ref: 'B' }) // synthetic reference
}, { $id: 'A' })
const B = Type.Object({
  a: Type.Unsafe({ [Kind]: 'Ref', $ref: 'A' }) // synthetic reference
})

PR also includes a new ArkType transform (very provisional and waiting on some features upstream)

@sinclairzx81 sinclairzx81 requested a review from xddq June 27, 2023 19:05
@sinclairzx81
Copy link
Owner Author

@xddq Heya, made a few updates to the code generation this evening. I wouldn't expect this to break anything on your side, but have added a couple of code generation options to get the TB model mapping over to ArkType (it's quite a interesting library)

If you can just give these updates a quick review, can merge em into main, let me know if you run into any issues.

Cheers!
S

Copy link
Collaborator

@xddq xddq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heya!

mhh, arktype looks interesting! The ergonomics look somewhat weird on first sight.

I quickly skimmed over it, nothing to suspicious from my side. Will try to include in ts2typebox in the next couple of days after the merge

@sinclairzx81
Copy link
Owner Author

@xddq Yeah, it's quite an interesting library with a few very interesting features (specifically these scopes for handling mutual recursive inference). Have the workbench setup for it currently to test it out. https://sinclairzx81.github.io/typebox-workbench/

Happy to let this PR sit for a few days (may add a few additional updates yet), so no hurry on things (though I may need a reminder to merge at some point :D)

Cheers mate!

@sinclairzx81 sinclairzx81 changed the title Feature: Reference Model | ArkType Feature: Reference Model | ArkType | io-Ts Jun 29, 2023
@sinclairzx81 sinclairzx81 changed the title Feature: Reference Model | ArkType | io-Ts Feature: Reference Model | ArkType | Io-Ts Jun 29, 2023
@sinclairzx81
Copy link
Owner Author

@xddq Hi, just pushed a couple more commits to this PR. Have included Yup, and also updated to TB 0.30.0 (which is major revision)

Tests are still passing here so will likely merge this one through (finally :D) once you've had a look. You may want to check ts2typebox and schema2typebox are still in order on the 0.30.0 dev release. You can test by installing @sinclair/typebox-0.30.0-dev-3. Main breaking changes in the release relate mostly Optional and Readonly modifiers (where the Modifier symbol is deprecated).

Full rundown of new and breaking changes can be found https://github.com/sinclairzx81/typebox/blob/modifiers/changelog/0.30.0.md.

Cheers mate!

@xddq
Copy link
Collaborator

xddq commented Jul 29, 2023

Wow, you are doing insane work here!!
I just skimmed over it(not to much time at the moment) and it looks fine. I will attempt to bump ts2typebox and schema2typebox in next week.

Btw. I can't wait to see how people will react to this package once it is ready to be published.

@sinclairzx81
Copy link
Owner Author

@xddq Heya, made a few more updates to TypeScriptToTypeBox (partially supporting mapped types (at least at runtime)), and did a bit more work to get this project into a publishable state.

Can preview the current state of the generators over at the typebox-transform website (which got a rename). If you spot something broken with the types, there's the best place to repro.

Tests are all still passing, will merge this one through (finally)!
Cheers!

@sinclairzx81 sinclairzx81 changed the title Feature: Reference Model | ArkType | Io-Ts TypeBox-Codegen Jul 31, 2023
@sinclairzx81 sinclairzx81 merged commit a9ea8dc into main Jul 31, 2023
6 checks passed
@sinclairzx81 sinclairzx81 deleted the references branch July 31, 2023 16:45
sinclairzx81 added a commit that referenced this pull request Jul 31, 2023
@xddq
Copy link
Collaborator

xddq commented Aug 4, 2023

@sinclairzx81 hey!

Sorry to bother, but did not want to create a separate issue for this one. Just to make sure, ts2typebox is finally obsolete, isn't it? Will then just archive it and make it link to this repo instead.

@sinclairzx81
Copy link
Owner Author

@xddq Hi!

Hey, I don't think ts2typebox is obsolete as it's still providing a command line interface (and this project will only ever be a library), however, it could potentially be extended to support some of these other validation libraries if you're interested (so perhaps a rename?), maybe ts2everything ?

There's still work to do on this project, have recently been eying up ProtoBuf IDL and GQL as next transform targets (and maybe a runtypes and myzod transforms), so could use some help getting those in order (also I'm not much of GQL user so could really use some insights getting an idea on what might be useful to generate for GQL users)

As for ts2typebox, I think it's worth keeping around as many users prefer to automate codegen via npm scripts :)

@xddq
Copy link
Collaborator

xddq commented Aug 4, 2023

@sinclairzx81 Hey!

Alright, will keep it around then! Damn, ts2everything is an awesome name. You should create/own that package! : ]

I assume you mean GraphQL? I think GQL is rather this. For GraphQL I could certainly help! As a first step I guess simple schema creation could be useful. As another one I guess custom scalars should be a good fit since the "basic" type system of GraphQL is somewhat limited (but can be extended via custom scalars).

Something cool/interesting at the later stages could be creating the resolvers. But I guess Typebox has no way to model functions types.. correct?

I will go ahead and do some research if people would even like Typescript to GraphQL. Will create an issue or discussion in this repo in the next couple of weeks on this.

Edit: Giving it a quick thought, I am not sure if creating GraphQL will be useful. It e.g. has no "undefined", requires types for functions to be complete, needs a ton of custom scalars (and their typescript implementation) to support the json schema keywords.

@xddq
Copy link
Collaborator

xddq commented Aug 25, 2023

@sinclairzx81 quick update, hope you are doing good!

  1. ts2typebox (finally) uses the typebox-codegen package :]
  2. I did not get to many responses when asking if typescript to GraphQL would be helpful. I think I would double down on that (until I see why it can be helpful/useful).

@sinclairzx81
Copy link
Owner Author

@xddq Hey, thanks for the update! :)

I did not get to many responses when asking if typescript to GraphQL would be helpful. I think I would double down on that (until I see why it can be helpful/useful).

Yeah, still pondering how to approach GraphQL, ProtoBuf and SQL (DDL) (which is another thing I'm looking into). The problem mapping over to these targets is that the type systems for them are misaligned to the capabilities of TypeScript (so creating mappings for them usually requires losing something in the process).

I do still think there's some merit to mapping them though (even if the developer needs to constrain types to those expressible in these targets) ... But it's tricky one as usually it's a case of being up against developer expectations while simultaneously being constrained by the thing being mapped into.

That said, I did make a start on ProtoBuf (gRPC) IDL https://github.com/sinclairzx81/typebox-workbench/blob/main/src/codegen/model/model-to-grpc.ts which is 90% UnsupportedType :D Needs more work!!

Thanks again for the update. Currently letting the codegen package settle for a while before doing more work on it (and to give a few of the target libraries some time to update), Also it's great to hear ts2typebox is using the codegen package! :)

Cheers!
S

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants