Skip to content

Commit

Permalink
Add unwrap feature to Result object. Add cache for pip requirements i…
Browse files Browse the repository at this point in the history
…n github action. Add pre-commit config file
  • Loading branch information
acostapazo committed Jan 9, 2020
1 parent 8faf748 commit f9133f3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ jobs:
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
- uses: actions/cache@v1
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/ci.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
10 changes: 0 additions & 10 deletions .pre-commit-config.yaml

This file was deleted.

14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,20 @@ class AuthService:
return NotImplementedMethodError
```

### Unwrap Result

If you *wrap* a Result object, its will return a valid value if it is success. Otherwise, it will return None.

```python
result = Result(success="Hi!")
value = result.unwrap()
assert value == "Hi!"

result = Failure(Error())
value = result.unwrap()

assert value is None
```

### Handle Result

Expand Down
6 changes: 6 additions & 0 deletions meiga/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def is_success(self):
def is_failure(self):
return not self._is_success

def unwrap(self):
if not self._is_success:
return None
else:
return self._value_success

def handle(
self,
success_handler: Callable = None,
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/test_result_unwrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest

from meiga import Result, Error, Failure


@pytest.mark.unit
def test_should_return_the_value_when_unwrap_a_success_result():

result = Result(success="Hi!")
value = result.unwrap()

assert value == "Hi!"


@pytest.mark.unit
def test_should_return_none_when_unwrap_a_failure_result():

result = Failure(Error())
value = result.unwrap()

assert value is None

0 comments on commit f9133f3

Please sign in to comment.