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

Consider undefined/null for optional rust types (close #278) #280

Merged
merged 3 commits into from
Nov 17, 2023
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
## [6.0.2] - 2023-11-17
- Consider undefined/null for rust type Option<T>

## [6.0.1] - 2023-09-27
- doc update

Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node-bindgen"
version = "6.0.1"
version = "6.0.2"
authors = ["Fluvio Contributors <team@fluvio.io>"]
edition = "2021"
description = "easy way to write nodejs module using rust"
Expand All @@ -17,7 +17,7 @@ uuid = ["nj-core/convert-uuid"]

[dependencies]
nj-sys = { path = "nj-sys", version = "4.0.0", optional = true }
nj-core = { path = "nj-core", version = "6.0.0", optional = true }
nj-core = { path = "nj-core", version = "6.0.1", optional = true }
nj-build = { path = "nj-build", version = "0.3.0", optional = true }
nj-derive = { path = "nj-derive", version = "3.2.0", optional = true }

Expand Down
16 changes: 12 additions & 4 deletions examples/Cargo.lock

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

1 change: 1 addition & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [
"tuples",
"uuid",
"logging",
"option",
]


Expand Down
6 changes: 5 additions & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ clean:
make -C tuple clean
make -C cleanup clean
make -C logging clean
make -C option clean

install:
npm install

test: install test-function test-cb test-async-cb test-promise test-json test-class-simple \
test-class-wrapper test-class-async test-stream test-buffer test-array test-bigint test-logging\
test-cleanup test-jsenv
test-cleanup test-jsenv test-option

test-function:
make -C function test
Expand Down Expand Up @@ -77,6 +78,9 @@ test-cleanup:
test-jsenv:
make -C js-env test

test-option:
make -C option test

check-clippy:
cargo clippy --all --all-features -- \
-D warnings \
Expand Down
1 change: 1 addition & 0 deletions examples/option/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dylib
18 changes: 18 additions & 0 deletions examples/option/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "nj-example-option"
version = "0.0.0"
authors = ["fluvio.io"]
edition = "2021"
publish = false


[lib]
crate-type = ["cdylib"]


[dependencies]
node-bindgen = { workspace = true}
fluvio-future = { workspace = true}

[build-dependencies]
node-bindgen = { path = "../../", default-features = false, features = ["build"] }
12 changes: 12 additions & 0 deletions examples/option/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
all: build

build:
nj-cli build

test: build
node test.js


clean:
rm -rf dist

1 change: 1 addition & 0 deletions examples/option/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
simple hello world
3 changes: 3 additions & 0 deletions examples/option/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
node_bindgen::build::configure();
}
10 changes: 10 additions & 0 deletions examples/option/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use node_bindgen::derive::node_bindgen;

#[node_bindgen]
fn test(a: Option<i32>, b: Option<i32>) -> i32 {
if let (Some(a), Some(b)) = (a, b) {
a + b
} else {
a.unwrap_or(b.unwrap_or(1))
}
}
19 changes: 19 additions & 0 deletions examples/option/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const assert = require('assert');

let addon = require('./dist');

assert.equal(addon.test(100, 200), 300);

assert.equal(addon.test(100), 100);

assert.equal(addon.test(undefined, 200), 200);

assert.equal(addon.test(null, 200), 200);

assert.equal(addon.test(100, undefined), 100);

assert.equal(addon.test(100, null), 100);

assert.equal(addon.test(undefined, undefined), 1);

assert.equal(addon.test(null, null), 1);
2 changes: 1 addition & 1 deletion nj-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nj-core"
version = "6.0.0"
version = "6.0.1"
authors = ["fluvio.io"]
edition = "2021"
description = "high level wrapper for Node N-API"
Expand Down
14 changes: 13 additions & 1 deletion nj-core/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,14 @@ impl JsEnv {
Ok(valuetype)
}

/// is value undefined or null
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn is_undefined_or_null(&self, napi_value: napi_value) -> Result<bool, NjError> {
let valuetype = self.value_type(napi_value)?;
Ok(valuetype == crate::sys::napi_valuetype_napi_undefined
|| valuetype == crate::sys::napi_valuetype_napi_null)
}

/// get string representation of value type
pub fn value_type_string(&self, napi_value: napi_value) -> Result<&'static str, NjError> {
Ok(napi_value_type_to_string(self.value_type(napi_value)?))
Expand Down Expand Up @@ -886,7 +894,11 @@ where

fn convert_arg_at(js_cb: &'a JsCallback, index: usize) -> Result<Self, NjError> {
if index < js_cb.args.len() {
Ok(Some(T::convert_to_rust(js_cb.env(), js_cb.args[index])?))
if js_cb.env().is_undefined_or_null(js_cb.args[index])? {
Ok(None)
} else {
Ok(Some(T::convert_to_rust(js_cb.env(), js_cb.args[index])?))
}
} else {
Ok(None)
}
Expand Down
Loading