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

Pluck will error on null value #216

Closed
mbonneau opened this issue Apr 28, 2023 · 0 comments · Fixed by #219
Closed

Pluck will error on null value #216

mbonneau opened this issue Apr 28, 2023 · 0 comments · Fixed by #219

Comments

@mbonneau
Copy link
Member

Pluck uses isset to check for the existence of a key. isset will not return true if the associated value is null which causes pluck to error.

This should use array_key_exists instead. (property_exists for objects)

Example:

\Rx\Observable::of([null])
    ->pluck(0)
    ->subscribe(
        fn($x) => printf("Next: %s\n", json_encode($x)),
        fn(\Throwable $e) => printf("Error: %s\n", $e->getMessage()),
        fn() => printf("Completed.\n")
    );

Outputs:

Error: Unable to pluck "0"

The expected output would be:

Next: null
Completed.

Here is the object version which exhibits the same behavior:

\Rx\Observable::of((object)['foo' => null])
    ->pluck('foo')
    ->subscribe(
        fn($x) => printf("Next: %s\n", json_encode($x)),
        fn(\Throwable $e) => printf("Error: %s\n", $e->getMessage()),
        fn() => printf("Completed.\n")
    );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant