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

fix: optional parameter ordering CS1737 #62

Merged
merged 4 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions bindgen/src/gen_cs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,17 @@ pub mod filters {
Ok(textwrap::indent(&wrapped, &" ".repeat(spaces)))
}

/// Orders fields in a way that avoids CS1737 errors
pub fn order_fields(fields: &[Field]) -> Result<(Vec<Field>, bool), askama::Error> {
let original_fields = fields.to_vec();
let mut fields = original_fields.clone();
// fields with default values must come after fields without default values
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1737
fields.sort_by_key(|field| field.default_value().is_some());
let is_reordered = fields != original_fields;
Ok((fields, is_reordered))
}

/// Panic with message
pub fn panic(message: &str) -> Result<String, askama::Error> {
panic!("{}", message)
Expand Down
12 changes: 10 additions & 2 deletions bindgen/templates/RecordTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */#}

{%- let rec = ci.get_record_definition(name).unwrap() %}
{%- let (ordered_fields, is_reordered) = rec.fields()|order_fields %}

{%- call cs::docstring(rec, 0) %}
{%- if is_reordered %}
/// <remarks>
/// <b>UniFFI Warning:</b> Optional parameters have been reordered because
/// of a C# syntax limitation. Use named parameters for compatibility with
/// future ordering changes.
/// </remarks>
{%- endif %}
public record {{ type_name }} (
{%- for field in rec.fields() %}
{%- for field in ordered_fields %}
{%- call cs::docstring(field, 4) %}
{{ field|type_name }} {{ field.name()|var_name -}}
{%- match field.default_value() %}
Expand All @@ -29,7 +37,7 @@ class {{ rec|ffi_converter_name }}: FfiConverterRustBuffer<{{ type_name }}> {
public override {{ type_name }} Read(BigEndianStream stream) {
return new {{ type_name }}(
{%- for field in rec.fields() %}
{{ field|read_fn }}(stream){% if !loop.last %},{% endif%}
{{ field.name()|var_name }}: {{ field|read_fn }}(stream){% if !loop.last %},{% endif%}
{%- endfor %}
);
}
Expand Down
27 changes: 27 additions & 0 deletions dotnet-tests/UniffiCS.binding_tests/OptionalParameterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* 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/. */

using uniffi.uniffi_cs_optional_parameters;
using static uniffi.uniffi_cs_optional_parameters.UniffiCsOptionalParametersMethods;

namespace UniffiCS.binding_tests;

public class OptionalParameterTests
{
[Fact]
public void OptionalParameter_CanBeOmitted()
{
var person = new Person(isSpecial: false);
string message = Hello(person);
Assert.Equal("Hello stranger!", message);
}

[Fact]
public void OptionalParameter_CanBeSpecified()
{
var person = new Person(name: "John Connor", isSpecial: false);
string message = Hello(person);
Assert.Equal("Hello John Connor!", message);
}
}
1 change: 1 addition & 0 deletions fixtures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ crate-type = ["cdylib", "lib"]
global-methods-class-name = { path = "global-methods-class-name" }
uniffi-cs-custom-types-builtin = { path = "custom-types-builtin" }
uniffi-cs-disposable-fixture = { path = "disposable" }
uniffi-cs-optional-parameters-fixture = { path = "optional-parameters" }
uniffi-example-arithmetic = { path = "../3rd-party/uniffi-rs/examples/arithmetic" }
uniffi-example-callbacks = { path = "../3rd-party/uniffi-rs/examples/callbacks" }
uniffi-example-custom-types = { path = "../3rd-party/uniffi-rs/examples/custom-types" }
Expand Down
16 changes: 16 additions & 0 deletions fixtures/optional-parameters/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "uniffi-cs-optional-parameters-fixture"
version = "1.0.0"
edition = "2021"
publish = false

[lib]
crate-type = ["lib", "cdylib"]
name = "uniffi_cs_optional_parameters"

[dependencies]
uniffi = {path = "../../3rd-party/uniffi-rs/uniffi", features=["build"]}
uniffi_macros = {path = "../../3rd-party/uniffi-rs/uniffi_macros"}

[build-dependencies]
uniffi = {path = "../../3rd-party/uniffi-rs/uniffi", features=["bindgen-tests"]}
22 changes: 22 additions & 0 deletions fixtures/optional-parameters/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* 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::setup_scaffolding!();

#[derive(uniffi::Record)]
pub struct Person {
#[uniffi(default = None)]
pub name: Option<String>,
pub is_special: bool,
}

#[uniffi::export]
fn hello(person: Person) -> String {
let name = person.name.unwrap_or("stranger".to_string());
if person.is_special {
format!("Hello {}! You are special!", name)
} else {
format!("Hello {}!", name)
}
}
1 change: 1 addition & 0 deletions fixtures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ mod uniffi_fixtures {
global_methods_class_name::uniffi_reexport_scaffolding!();
uniffi_cs_custom_types_builtin::uniffi_reexport_scaffolding!();
uniffi_cs_disposable::uniffi_reexport_scaffolding!();
uniffi_cs_optional_parameters::uniffi_reexport_scaffolding!();
}