From 0479091126f281710fd3a2b082be575410a1025f Mon Sep 17 00:00:00 2001 From: Kegan Dougal <7190048+kegsay@users.noreply.github.com> Date: Thu, 14 Mar 2024 10:20:03 +0000 Subject: [PATCH] Fix issue 43: Add package name to RustBufferI if it needs it Signed-off-by: Kegan Dougal <7190048+kegsay@users.noreply.github.com> --- bindgen/src/gen_go/mod.rs | 15 ++++++++++++++ bindgen/templates/macros.go | 2 +- binding_tests/issue43_test.go | 21 ++++++++++++++++++++ fixtures/Cargo.toml | 1 + fixtures/regressions/issue43/Cargo.toml | 17 ++++++++++++++++ fixtures/regressions/issue43/build.rs | 7 +++++++ fixtures/regressions/issue43/src/issue43.udl | 1 + fixtures/regressions/issue43/src/lib.rs | 13 ++++++++++++ fixtures/regressions/issue45/src/lib.rs | 6 +++--- fixtures/src/lib.rs | 1 + 10 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 binding_tests/issue43_test.go create mode 100644 fixtures/regressions/issue43/Cargo.toml create mode 100644 fixtures/regressions/issue43/build.rs create mode 100644 fixtures/regressions/issue43/src/issue43.udl create mode 100644 fixtures/regressions/issue43/src/lib.rs diff --git a/bindgen/src/gen_go/mod.rs b/bindgen/src/gen_go/mod.rs index e6b78b7..1c1c638 100644 --- a/bindgen/src/gen_go/mod.rs +++ b/bindgen/src/gen_go/mod.rs @@ -475,6 +475,21 @@ pub mod filters { Ok(result) } + // Return the Go package name for the given type, if it has one, followed by a '.' + // Returns the empty string if the type has no package name associated with it e.g primitive types. + // + // for multi-package bindings, it may be required to specify which + // RustBufferI should be used when creating completeFunc. Failure to + // specify the right package here will result in compilation errors. + pub fn maybe_namespace(type_: &impl AsType) -> Result { + let type_label = oracle().find(type_).type_label(); + if let Some((package_name, _)) = type_label.split_once(".") { + Ok(format!("{}.", package_name)) + } else { + Ok(String::from("")) + } + } + /// FFI type name to be used to reference cgo types. Such that they exactly match to the cgo bindings and can be used with `//export`. pub fn ffi_type_name_cgo_safe>( type_: &T, diff --git a/bindgen/templates/macros.go b/bindgen/templates/macros.go index 0fbb555..855105c 100644 --- a/bindgen/templates/macros.go +++ b/bindgen/templates/macros.go @@ -60,7 +60,7 @@ {% macro return_type_decl_async(func) %} {%- match func.return_type() -%} {%- when Some with (return_type) -%} - {{ return_type|ffi_type_name }} + {{return_type|maybe_namespace}}{{ return_type|ffi_type_name }} {%- when None -%} {%- endmatch %} {%- endmacro %} diff --git a/binding_tests/issue43_test.go b/binding_tests/issue43_test.go new file mode 100644 index 0000000..e72ccec --- /dev/null +++ b/binding_tests/issue43_test.go @@ -0,0 +1,21 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package binding_tests + +import ( + "testing" + + "github.com/NordSecurity/uniffi-bindgen-go/binding_tests/generated/issue43" + "github.com/stretchr/testify/assert" +) + +// Ensure you can call async functions which return types in a different package. +// See https://github.com/NordSecurity/uniffi-bindgen-go/issues/43 + +func TestIssue43(t *testing.T) { + record := issue43.GetAsyncExternalType() + assert.Equal(t, record.Id, "foo") + assert.Equal(t, record.Tag, "bar") +} diff --git a/fixtures/Cargo.toml b/fixtures/Cargo.toml index 960d3d2..ceb736e 100644 --- a/fixtures/Cargo.toml +++ b/fixtures/Cargo.toml @@ -41,4 +41,5 @@ uniffi-go-fixture-destroy = { path = "destroy" } uniffi-go-fixture-errors = { path = "errors" } uniffi-go-fixture-name-case = { path = "name-case" } uniffi-go-fixture-objects = { path = "objects" } +uniffi-go-fixture-issue43 = { path = "regressions/issue43" } uniffi-go-fixture-issue45 = { path = "regressions/issue45" } diff --git a/fixtures/regressions/issue43/Cargo.toml b/fixtures/regressions/issue43/Cargo.toml new file mode 100644 index 0000000..81559ce --- /dev/null +++ b/fixtures/regressions/issue43/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "uniffi-go-fixture-issue43" +version = "1.0.0" +edition = "2021" +publish = false + +[lib] +crate-type = ["lib", "cdylib"] +name = "uniffi_go_issue43" + +[dependencies] +uniffi = {path = "../../../3rd-party/uniffi-rs/uniffi"} +uniffi_macros = {path = "../../../3rd-party/uniffi-rs/uniffi_macros"} +uniffi-go-fixture-issue45 = {path = "../issue45"} + +[build-dependencies] +uniffi_build = {path = "../../../3rd-party/uniffi-rs/uniffi_build", features=["builtin-bindgen"]} diff --git a/fixtures/regressions/issue43/build.rs b/fixtures/regressions/issue43/build.rs new file mode 100644 index 0000000..e8c743a --- /dev/null +++ b/fixtures/regressions/issue43/build.rs @@ -0,0 +1,7 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +fn main() { + uniffi_build::generate_scaffolding("./src/issue43.udl").unwrap(); +} diff --git a/fixtures/regressions/issue43/src/issue43.udl b/fixtures/regressions/issue43/src/issue43.udl new file mode 100644 index 0000000..3df3dcd --- /dev/null +++ b/fixtures/regressions/issue43/src/issue43.udl @@ -0,0 +1 @@ +namespace issue43 {}; diff --git a/fixtures/regressions/issue43/src/lib.rs b/fixtures/regressions/issue43/src/lib.rs new file mode 100644 index 0000000..1f75b47 --- /dev/null +++ b/fixtures/regressions/issue43/src/lib.rs @@ -0,0 +1,13 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#[uniffi::export] +async fn get_async_external_type() -> uniffi_go_issue45::Record { + uniffi_go_issue45::Record { + id: "foo".to_string(), + tag: "bar".to_string(), + } +} + +include!(concat!(env!("OUT_DIR"), "/issue43.uniffi.rs")); diff --git a/fixtures/regressions/issue45/src/lib.rs b/fixtures/regressions/issue45/src/lib.rs index 0d70c23..14595a9 100644 --- a/fixtures/regressions/issue45/src/lib.rs +++ b/fixtures/regressions/issue45/src/lib.rs @@ -3,9 +3,9 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #[derive(uniffi::Record)] -struct Record { - id: String, - tag: String, +pub struct Record { + pub id: String, + pub tag: String, } // Ensure multiple futures packages work fine together, the other one being diff --git a/fixtures/src/lib.rs b/fixtures/src/lib.rs index fccd3de..431519f 100644 --- a/fixtures/src/lib.rs +++ b/fixtures/src/lib.rs @@ -37,6 +37,7 @@ mod uniffi_fixtures { // Go specific uniffi_go_destroy::uniffi_reexport_scaffolding!(); uniffi_go_errors::uniffi_reexport_scaffolding!(); + uniffi_go_issue43::uniffi_reexport_scaffolding!(); uniffi_go_issue45::uniffi_reexport_scaffolding!(); uniffi_go_name_case::uniffi_reexport_scaffolding!(); uniffi_go_objects::uniffi_reexport_scaffolding!();