|
[@bs.deriving jsConverter] |
|
type args = { |
|
key1: option(string), |
|
key2: option(string), |
|
key3: option(string), |
|
}; |
|
|
|
let concat = ({key1, key2, key3}) => |
|
switch (key1, key2, key3) { |
|
| (Some(k1), Some(k2), Some(k3)) => k1 ++ ", " ++ k2 ++ ", " ++ k3 |
|
| _ => "Something is None" |
|
}; |
|
|
|
let concatFromJs = [%raw {| |
|
({key1, key2, key3}) => { |
|
const argArr = argsFromJs({key1, key2, key3}) |
|
return concat(argArr) |
|
/* argsFromJs is generated by jsConverter */ |
|
} |
|
|}]; |
|
|
|
/* test */ |
|
|
|
Js.log( |
|
"call from reason: " ++ |
|
concat( |
|
{key1: Some("value1"), key2: Some("value2"), key3: None} |
|
) |
|
); |
|
|
|
Js.log( |
|
"call from reason: " ++ |
|
concat( |
|
{key1: Some("value1"), key2: Some("value2"), key3: Some("value3")} |
|
) |
|
); |
|
|
|
[%raw {| |
|
// optional check is fine with None |
|
console.log('call from js:', concatFromJs({key1: "value1", key2: "value2"})) |
|
|}]; |
|
|
|
[%raw {| |
|
// optional check is not fine with Some |
|
console.log('call from js:', concatFromJs({key1: "value1", key2: "value2", key3: "value3"})) |
|
|}]; |
|
|
|
/* result are below |
|
|
|
"call from reason: Something is None" |
|
"call from reason: value1, value2, value3" |
|
"call from js:" "Something is None" |
|
"call from js:" "v, v, v" // argsFromJs is not handling option(string) from javascript correctly |
|
*/ |
|
|
|
// so it makes really difficult to expose a function to javascript side that accepts objects possibly contains optional values |
|
|
I made this gist to file an issue to Reason.
Now I tried the same code with Reason online editor, and it works fine, so I think it's fixed now with new version.