Skip to content

Commit

Permalink
Fix several issues dealing with the error interface
Browse files Browse the repository at this point in the history
Generate pointers to structs consistently, rather than using the
`error` interface in places. This fixes two issues:
 - fix an issue where nested variants did not work correctly when the nested variant was an error.
 - fix an issue where a callback could not be used with an error  (#36)

Signed-off-by: Kegan Dougal <[email protected]>
  • Loading branch information
kegsay authored and arg0d committed Mar 21, 2024
1 parent 62738f9 commit 034f4ff
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
10 changes: 9 additions & 1 deletion bindgen/src/gen_go/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl GoCodeOracle {
pub mod filters {
use super::*;

fn oracle() -> &'static GoCodeOracle {
pub fn oracle() -> &'static GoCodeOracle {
&GoCodeOracle
}

Expand Down Expand Up @@ -578,4 +578,12 @@ impl<'a> TypeRenderer<'a> {
pub fn cgo_callback_fn(&self, name: &str, module_path: &str) -> String {
format!("{module_path}_cgo_{name}")
}

pub fn field_type_name(&self, field: &Field) -> String {
let name = filters::oracle().find(&field.as_type()).type_label();
match self.ci.is_name_used_as_error(&name) {
true => format!("*{name}"),
false => name.to_string(),
}
}
}
8 changes: 4 additions & 4 deletions bindgen/templates/ErrorTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type {{ variant_class_name }} struct {
message string
{%- else %}
{%- for field in variant.fields() %}
{{ field.name()|error_field_name }} {{ field|type_name}}
{{ field.name()|error_field_name }} {{ self.field_type_name(field) }}
{%- endfor %}
{%- endif %}
}
Expand All @@ -39,7 +39,7 @@ type {{ variant_class_name }} struct {
func New{{ variant_class_name }}(
{%- if !e.is_flat() %}
{%- for field in variant.fields() %}
{{ field.name()|var_name }} {{ field|type_name}},
{{ field.name()|var_name }} {{ self.field_type_name(field) }},
{%- endfor %}
{%- endif %}
) *{{ type_name.clone() }} {
Expand Down Expand Up @@ -81,14 +81,14 @@ type {{ e|ffi_converter_name }} struct{}
var {{ e|ffi_converter_name }}INSTANCE = {{ e|ffi_converter_name }}{}

func (c {{ e|ffi_converter_name }}) Lift(eb RustBufferI) error {
return LiftFromRustBuffer[error](c, eb)
return LiftFromRustBuffer[*{{ type_name|class_name }}](c, eb)
}

func (c {{ e|ffi_converter_name }}) Lower(value *{{ type_name|class_name }}) RustBuffer {
return LowerIntoRustBuffer[*{{ type_name|class_name }}](c, value)
}

func (c {{ e|ffi_converter_name }}) Read(reader io.Reader) error {
func (c {{ e|ffi_converter_name }}) Read(reader io.Reader) *{{ type_name|class_name }} {
errorID := readUint32(reader)

{%- if e.is_flat() %}
Expand Down
10 changes: 10 additions & 0 deletions binding_tests/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,13 @@ func TestErrorNamedError(t *testing.T) {
assert.ErrorAs(t, err, &expectedError)
assert.Equal(t, "it's an error", expectedError.Unwrap().(*errors.ErrorNamedErrorError).Error_)
}

func TestNestedError(t *testing.T) {
assert.Equal(t, nil, errors.TryNested(false))
err := errors.TryNested(true)
var expectedError *errors.NestedError
assert.ErrorAs(t, err, &expectedError)
var expectedNestedError *errors.NestedErrorNested
assert.ErrorAs(t, expectedError.Unwrap(), &expectedNestedError)
assert.Equal(t, "ValidationError: UnknownError", expectedNestedError.Source.Error())
}
9 changes: 9 additions & 0 deletions fixtures/errors/src/errors.udl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ enum BoobyTrapError {
"HotDoorKnob",
};

[Error]
interface NestedError {
Nested(ValidationError source);
};

[Error]
interface ValidationError {
InvalidUser(i32 user_id);
Expand All @@ -40,6 +45,10 @@ interface ComplexError {
Option(i32? id_a, i32? id_b);
};

callback interface Callback {
void do_something(BoobyTrapError error);
};

dictionary Vec2 {
double x;
double y;
Expand Down
21 changes: 21 additions & 0 deletions fixtures/errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ pub enum ComplexError {
},
}

#[derive(Debug, thiserror::Error)]
pub enum NestedError {
#[error(transparent)]
Nested { source: ValidationError },
}

#[derive(Debug)]
pub struct Vec2 {
x: f64,
Expand All @@ -57,6 +63,21 @@ impl Vec2 {
}
}

#[uniffi::export]
fn try_nested(trip: bool) -> Result<(), NestedError> {
if trip {
Err(NestedError::Nested {
source: ValidationError::UnknownError,
})
} else {
Ok(())
}
}

pub trait Callback {
fn do_something(&self, error: BoobyTrapError);
}

fn try_void(trip: bool) -> Result<(), BoobyTrapError> {
if trip {
Err(BoobyTrapError::IceSlip)
Expand Down

0 comments on commit 034f4ff

Please sign in to comment.