fix(napi): either for #[napi(object)] types (#1258)
This commit is contained in:
parent
6a9fdba8b6
commit
cb9239d8dc
6 changed files with 54 additions and 2 deletions
|
@ -117,9 +117,10 @@ macro_rules! either_n {
|
|||
where $( $parameter: TypeName + FromNapiValue + ValidateNapiValue ),+
|
||||
{
|
||||
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> crate::Result<Self> {
|
||||
let mut ret = Err(Error::new(Status::InvalidArg, "Invalid value".to_owned()));
|
||||
$(
|
||||
if unsafe { $parameter::validate(env, napi_val).is_ok() } {
|
||||
unsafe { $parameter ::from_napi_value(env, napi_val).map(Self:: $parameter ) }
|
||||
if unsafe { $parameter::validate(env, napi_val).is_ok() && { ret = $parameter ::from_napi_value(env, napi_val).map(Self:: $parameter ); ret.is_ok() } } {
|
||||
ret
|
||||
} else
|
||||
)+
|
||||
{
|
||||
|
|
|
@ -65,6 +65,16 @@ Generated by [AVA](https://avajs.dev).
|
|||
export function receiveDifferentClass(either: JsClassForEither | AnotherClassForEither): number␊
|
||||
export function returnEitherClass(input: number): number | JsClassForEither␊
|
||||
export function eitherFromOption(): JsClassForEither | undefined␊
|
||||
export interface A {␊
|
||||
foo: number␊
|
||||
}␊
|
||||
export interface B {␊
|
||||
bar: number␊
|
||||
}␊
|
||||
export interface C {␊
|
||||
baz: number␊
|
||||
}␊
|
||||
export function eitherFromObjects(input: A | B | C): string␊
|
||||
/** default enum values are continuos i32s start from 0 */␊
|
||||
export const enum Kind {␊
|
||||
/** Barks */␊
|
||||
|
|
Binary file not shown.
|
@ -97,6 +97,7 @@ import {
|
|||
callbackReturnPromise,
|
||||
returnEitherClass,
|
||||
eitherFromOption,
|
||||
eitherFromObjects,
|
||||
overrideIndividualArgOnFunction,
|
||||
overrideIndividualArgOnFunctionWithCbArg,
|
||||
createObjectWithClassField,
|
||||
|
@ -544,6 +545,12 @@ test('either from option', (t) => {
|
|||
t.true(eitherFromOption() instanceof JsClassForEither)
|
||||
})
|
||||
|
||||
test('either from objects', (t) => {
|
||||
t.is(eitherFromObjects({ foo: 1 }), 'A')
|
||||
t.is(eitherFromObjects({ bar: 2 }), 'B')
|
||||
t.is(eitherFromObjects({ baz: 3 }), 'C')
|
||||
})
|
||||
|
||||
test('either3', (t) => {
|
||||
t.is(either3(2), 2)
|
||||
t.is(either3('hello'), 'hello'.length)
|
||||
|
|
10
examples/napi/index.d.ts
vendored
10
examples/napi/index.d.ts
vendored
|
@ -55,6 +55,16 @@ export function receiveMutClassOrNumber(either: number | JsClassForEither): numb
|
|||
export function receiveDifferentClass(either: JsClassForEither | AnotherClassForEither): number
|
||||
export function returnEitherClass(input: number): number | JsClassForEither
|
||||
export function eitherFromOption(): JsClassForEither | undefined
|
||||
export interface A {
|
||||
foo: number
|
||||
}
|
||||
export interface B {
|
||||
bar: number
|
||||
}
|
||||
export interface C {
|
||||
baz: number
|
||||
}
|
||||
export function eitherFromObjects(input: A | B | C): string
|
||||
/** default enum values are continuos i32s start from 0 */
|
||||
export const enum Kind {
|
||||
/** Barks */
|
||||
|
|
|
@ -115,3 +115,27 @@ fn return_either_class(input: i32) -> Either<u32, JsClassForEither> {
|
|||
fn either_from_option() -> Either<JsClassForEither, Undefined> {
|
||||
Some(JsClassForEither {}).into()
|
||||
}
|
||||
|
||||
#[napi(object)]
|
||||
pub struct A {
|
||||
pub foo: u32,
|
||||
}
|
||||
|
||||
#[napi(object)]
|
||||
pub struct B {
|
||||
pub bar: u32,
|
||||
}
|
||||
|
||||
#[napi(object)]
|
||||
pub struct C {
|
||||
pub baz: u32,
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn either_from_objects(input: Either3<A, B, C>) -> String {
|
||||
match &input {
|
||||
Either3::A(_) => "A".to_owned(),
|
||||
Either3::B(_) => "B".to_owned(),
|
||||
Either3::C(_) => "C".to_owned(),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue