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

Refactor Bindgen #37

Merged
merged 3 commits into from
Nov 11, 2024
Merged

Refactor Bindgen #37

merged 3 commits into from
Nov 11, 2024

Conversation

bhelx
Copy link
Contributor

@bhelx bhelx commented Oct 28, 2024

See dylibso/xtp-bindgen#18

This refactor should serve as the basis for how we change the other bindgens. I've kept it backwards compatible but once the changes are rolled out everywhere, we should trim unused properties and features.

Note we have removed all code that references XTP schema type directly like:

  • type
  • format
  • items
  • $ref
  • additionalProperties

Also note that coincidentally, this implements #36

package.json Outdated Show resolved Hide resolved
@bhelx bhelx marked this pull request as ready for review November 1, 2024 17:02
Comment on lines +37 to +38
case 'int64':
throw Error(`We do not support format int64 yet`)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we may want to make this a bigint?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, bigint would cover it!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

need to think a bit about how we should encode it since i think we're limited by the JSON spec. maybe just as a numerical string? "123456789098765432"?

Copy link
Member

Choose a reason for hiding this comment

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

I like how (at least in Chrome) throws:

Do not know how to serialize a BigInt

😆

function dateToJson(v: Date): string {
function castArray(caster: (v: any) => any) {
return (v?: Array<any>) => {
if (isNull(v)) return v
Copy link
Contributor

Choose a reason for hiding this comment

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

A (kind-of lengthy) question! So, IIUC right now the JS bindgen is more permissive than other bindgens about what values it accepts. That can lead to some surprise down the line for hosts if they build their host service and initial plugins using JS. (This happened in extendabot: some interfaces were actually nullable but not marked as such. The JS bindgen plugins worked, but go, rust, and others failed in ways that were difficult to debug from the guest side.)

Should we track type nullability here and fail if an array (or array member) is set to null inappropriately to match the other bindgens? (It's possible we're doing that and I just missed it, too!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This kind of comes at the question of the line b/w types and validation. Who is responsible for validation and where should that happen? I wanted this casting code to be agnostic to that. Though I understand with other, typed languages you get more validation out of the box. I still need to think a bit about where they should happen. We could maybe follow up with a stricter casting routine. I'm still holding out hope that we can use a dependency for all this code and delete it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Something to note about the casting code if it's not clear, we don't cast the whole object. only the parts that need it. So i do think if we consider some kind of validation we may need to do that on the whole object somehow.

Comment on lines +37 to +38
case 'int64':
throw Error(`We do not support format int64 yet`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, bigint would cover it!

@nilslice
Copy link
Member

nilslice commented Nov 1, 2024

Nice - not too bad at all to convert this. I am curious if the isCastable check is geared mostly to TS, vs. something like Go / Rust w/ a type system that works for you. Or, is there something in the upstream xtp-bindgen change that makes this important for other bindgens too?

@bhelx
Copy link
Contributor Author

bhelx commented Nov 11, 2024

Nice - not too bad at all to convert this. I am curious if the isCastable check is geared mostly to TS, vs. something like Go / Rust w/ a type system that works for you. Or, is there something in the upstream xtp-bindgen change that makes this important for other bindgens too?

@nilslice I think this is just something that we have to workaround in JS for now. I'd like to follow up by looking at a dependency to handle this, or now that I see that JSON.rawJSON exists, seeing if we can write something that plugs into the JSON parser and stringifier.

Copy link
Contributor

@mhmd-azeez mhmd-azeez left a comment

Choose a reason for hiding this comment

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

lgtm, but let's also update xtp-bindgen to the latest rc

@bhelx bhelx merged commit 0b347f2 into main Nov 11, 2024
3 checks passed
@bhelx bhelx deleted the refactor-bindgen branch November 11, 2024 14:46
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.

4 participants