From 259bf47798aac53d7ab4378bde6def5afd53a7e1 Mon Sep 17 00:00:00 2001 From: Aivaras Saulius Date: Tue, 23 Jul 2024 14:28:55 +0300 Subject: [PATCH] Add option to convert null strings to empty --- CHANGELOG.md | 4 ++++ bindgen/src/gen_cs/mod.rs | 1 + bindgen/templates/StringHelper.cs | 7 +++++++ docs/CONFIGURATION.md | 2 ++ 4 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5bf69e..aa5e098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/bindgen/src/gen_cs/mod.rs b/bindgen/src/gen_cs/mod.rs index e9233b4..bf64157 100644 --- a/bindgen/src/gen_cs/mod.rs +++ b/bindgen/src/gen_cs/mod.rs @@ -38,6 +38,7 @@ pub struct Config { external_packages: HashMap, global_methods_class_name: Option, access_modifier: Option, + null_string_to_empty: Option, } #[derive(Debug, Default, Clone, Serialize, Deserialize)] diff --git a/bindgen/templates/StringHelper.cs b/bindgen/templates/StringHelper.cs index 69a928d..3df652b 100644 --- a/bindgen/templates/StringHelper.cs +++ b/bindgen/templates/StringHelper.cs @@ -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); diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 62ac6b6..ff8e38e 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -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.