-
Notifications
You must be signed in to change notification settings - Fork 3
Switch to UniFFI Procedural Macros #36
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
Conversation
I don't see passing null values is a big issue, considering we'll need to do that on the response objects, i.e. But it's be nice to generate initialisers to use // wp_api.swift
public struct PostListParams {
public init(
page: UInt32? = nil,
perPage: UInt32? = nil) {
self.page = page
self.perPage = perPage
}
} |
That's exactly what I was referring to in the PR description:
This was possible with UDL, but not yet supported in procmacros. |
I was thinking a bit more about this, and I think it might be possible to handle it in a different way. If I am not mistaken, that'd mean that we'll be able to call In any case, since this is not the main point for this PR, I think we should keep the implementation as is for now. At some point, I'll make a proposal about this topic with all the different options I am aware of. How does that sound @crazytonyli? |
555bd2c
to
c7968fb
Compare
Either way, I'd imagine we'll want to implement them for many of these types to make instantiation more simple in the default case?
I agree about handling this in a separate PR |
Oh nice! I didn't check out the generated Swift code. I thought the public struct PostListParams {
public let page: UInt32 = 1
public let perPage: UInt32 = 10
}
I'd say the current generated code looks pretty good. It'd be nice to have nil value (hopefully we don't have wait too long), but figuring out the default value at the server side and assigning a default value here in the library seems fine to me. In Swift, there is rarely a |
That's how Kotlin works as well. I could see a future where Although this is probably not too difficult to implement - of course without knowing the internals it's hard to say - I assume there are more important things to improve in the Another alternative is to generate these bindings ourselves. Now that I have some experience of that with In any case, I am happy that we have multiple ways to resolve these minor issues. |
This PR builds on #35 and should be merged after it, so it's marked as Draft, but it's otherwise ready to be merged.
In #28, we'd like to generate different objects depending on different contexts. In UDL based design, even if we generate the Rust code, we'd also have to generate the UDL code alongside it. That's not something readily available, so instead, we are going to use the uniffi procmacros. This will allow us to generate only the Rust code and then derive the uniffi bindings from it.
There are additional benefits to using macros. Most importantly, debugging UDL errors can be time consuming because it's not parsed at a proper language level, so even a missing
;
can result in hard to identify errors. It also makes it so that we only have 1 source of truth, and that's the Rust code. Finally, it simplifies the process of adding new types, because there is only one place to add them.Of course this decision does have its own drawbacks, mainly UDL being a more accessible format for developers - as opposed to the Rust code. We thought this was an acceptable compromise because we'll have documentation for the API, so developers won't need to consult the source code. We also have native bindings and will introduce native wrappers that work on a higher level.
The conversion was fairly simple and all I had to do was adjust a few things. One minor drawback I found was that uniffi doesn't currently have
None
(ornull
) as a possible default value. That means usingnull
as a way to signal "use the default value set by the server" doesn't work - not unless we are willing to pass in a bunch ofnull
values from our native wrappers. That resulted in the following change:This sets the default values for these parameters to the default values set by the API. I don't think this change is good or bad, as it's just a different way of handling the default. So, I didn't see this as something concerning - and even if I did - I think we can just contribute to
uniffi
to add the possibility of usingNone
as a default value. For now, I think this is a good implementation and we should proceed with it.