Skip to content

Commit

Permalink
Merge pull request #81 from NordSecurity/null_str_conversion
Browse files Browse the repository at this point in the history
Add option to convert null strings to empty
  • Loading branch information
Aivaras Saulius authored Jul 23, 2024
2 parents efea2d6 + 024d92a commit 537a793
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### v0.8.1+v0.25.0

- Add a configuration option `null_string_to_empty` to convert non-optional null strings to empty strings

----

### v0.8.0+v0.25.0
Expand Down
1 change: 1 addition & 0 deletions bindgen/src/gen_cs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct Config {
external_packages: HashMap<String, String>,
global_methods_class_name: Option<String>,
access_modifier: Option<String>,
null_string_to_empty: Option<bool>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
Expand Down
7 changes: 7 additions & 0 deletions bindgen/templates/StringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public override string Read(BigEndianStream stream) {
}

public override RustBuffer Lower(string value) {
{%- match config.null_string_to_empty %}
{%- when Some(true) %}
if (value == null) {
value = "";
}
{%- when _ %}
{%- endmatch %}
var bytes = System.Text.Encoding.UTF8.GetBytes(value);
var rbuf = RustBuffer.Alloc(bytes.Length);
rbuf.AsWriteableStream().WriteBytes(bytes);
Expand Down
2 changes: 2 additions & 0 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ uniffi-bindgen-cs path/to/definitions.udl --config path/to/uniffi.toml
```

- `access_modifier` - override the default `internal` access modifier for "exported" uniffi symbols.

- `null_string_to_empty` - when set to `true`, `null` strings will be converted to empty strings even if they are not optional.

0 comments on commit 537a793

Please sign in to comment.