Skip to content

Commit

Permalink
Support lists of values (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cariad Eccleston authored Jan 20, 2021
1 parent e3d6f69 commit 0529f5a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
7 changes: 4 additions & 3 deletions docs/plugins/wev-echo.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

## Configuration

| Property | Required | Description |
|----------|----------|----------------------|
| value | ✔️ | Value to resolve to. |
| Property | Required | Description |
|-----------|----------|---------------------------------------------------------------------------------|
| separator || Separator to insert between values when `value` is a list. Defaults to a space. |
| value | ✔️ | Value to resolve to. String or list of strings. |

## Examples

Expand Down
27 changes: 19 additions & 8 deletions wev/wev_echo/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime, timedelta
from logging import Logger
from typing import List
from typing import List, Union

from wev.sdk import PluginBase, Resolution, ResolutionSupport
from wev.sdk.exceptions import MissingConfigurationError
Expand Down Expand Up @@ -34,16 +34,27 @@ def resolve(self, support: ResolutionSupport) -> Resolution:
Returns:
Resolution.
"""
expires_at = datetime.now() + timedelta(seconds=60)
support.logger.debug("Calculated expiry date: %s", expires_at)
return Resolution.make(value=self.value, expires_at=expires_at)
value = (
self.value
if isinstance(self.value, str)
else self.separator.join(self.value)
)
return Resolution.make(
value=value,
expires_at=datetime.now() + timedelta(seconds=60),
)

@property
def value(self) -> str:
"""
Gets the hard-coded value from the configuration.
"""
def separator(self) -> str:
""" Gets the single value to return. """
return self.get("separator", " ")

@property
def value(self) -> Union[str, List[str]]:
""" Gets the single value or list of values to return. """
try:
if isinstance(self["value"], list):
return self["value"]
return str(self["value"])
except KeyError as ex:
raise MissingConfigurationError(
Expand Down
20 changes: 16 additions & 4 deletions wev/wev_echo/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from logging import Logger
from typing import Any, Dict

from pytest import raises
from pytest import mark, raises

from wev.sdk import ResolutionSupport
from wev.sdk.exceptions import MissingConfigurationError
Expand All @@ -13,9 +14,20 @@ def test_explain(logger: Logger) -> None:
]


def test_resolve(resolution_support: ResolutionSupport) -> None:
resolution = Plugin({"value": "foo"}).resolve(support=resolution_support)
assert resolution.values == ("foo",)
@mark.parametrize(
"d, expect",
[
({"value": "foo"}, ("foo",)),
({"value": ["foo", "bar"]}, ("foo bar",)),
({"separator": ".", "value": ["foo", "bar"]}, ("foo.bar",)),
],
)
def test_resolve(
d: Dict[str, Any],
expect: str,
resolution_support: ResolutionSupport,
) -> None:
assert Plugin(d).resolve(support=resolution_support).values == expect


def test_resolve__missing_config(resolution_support: ResolutionSupport) -> None:
Expand Down

0 comments on commit 0529f5a

Please sign in to comment.