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

feat(Result): introduce Result::unwrapOr() #470

Merged
merged 5 commits into from
May 4, 2024
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
14 changes: 14 additions & 0 deletions src/Psl/Result/Failure.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ public function getResult(): void
throw $this->throwable;
}

/**
* Unwrap the Result if it is succeeded or return $default value.
*
* @template D
*
* @param D $default
*
* @return D
*/
public function unwrapOr(mixed $default): mixed
{
return $default;
}

/**
* Since this is a failed result wrapper, this always returns the `Throwable` thrown during the operation.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Psl/Result/ResultInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ public function always(Closure $always): ResultInterface;
*/
public function getResult();

/**
* Unwrap the Result if it is succeeded or return $default value.
*
* @template D
*
* @param D $default
*
* @return T|D
*/
public function unwrapOr(mixed $default): mixed;

/**
* Return the underlying throwable, or fail with a invariant violation exception.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Psl/Result/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ public function getResult(): mixed
return $this->value;
}

/**
* Unwrap the Result if it is succeeded or return $default value.
*
* @template D
*
* @param D $default
*
* @return T
*/
public function unwrapOr(mixed $default): mixed
{
return $this->value;
}

/**
* Since this is a successful result wrapper, this always throws a
* `Psl\Exception\InvariantViolationException` saying that there was no exception thrown from the operation.
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/Result/FailureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public function testGetResult(): void
$wrapper->getResult();
}

public function testUnwrapFailure(): void
{
$result = new Failure(new Exception());
$value = $result->unwrapOr(null);

static::assertNull($value);
}

public function testGetException(): void
{
$exception = new Exception('bar');
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/Result/SuccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public function testGetResult(): void
static::assertSame('hello', $wrapper->getResult());
}

public function testUnwrap(): void
{
$result = new Success('foo');
$value = $result->unwrapOr(null);

static::assertSame('foo', $value);
}

public function testGetException(): void
{
$wrapper = new Success('hello');
Expand Down