Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### 🎉 New features
- Add Postgres execution result store ([#171](https://github.com/roostorg/osprey/pull/171) by [@serendipty01](https://github.com/serendipty01))
- Add `ParseInt` UDF — converts a numeric string to an integer ([#190](https://github.com/roostorg/osprey/pull/190) by [@bealsbe](https://github.com/bealsbe))
- Add `StringSlice` UDF which extracts a substring by index range ([#189](https://github.com/roostorg/osprey/pull/189) by [@bealsbe](https://github.com/bealsbe))

### 🐛 Bug fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ class UdfCategories(str, Enum):
HTTP = 'HTTP'
IP = 'IP'
PHONE = 'Phone'
CAST = 'Cast'
RANDOM = 'Random'
STRING = 'String'
20 changes: 20 additions & 0 deletions osprey_worker/src/osprey/engine/stdlib/udfs/parse_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from osprey.engine.executor.execution_context import ExpectedUdfException

from ._prelude import ArgumentsBase, ExecutionContext, UDFBase
from .categories import UdfCategories


class Arguments(ArgumentsBase):
s: str


class ParseInt(UDFBase[Arguments, int]):
"""Converts a numeric string to an integer."""

category = UdfCategories.CAST

def execute(self, execution_context: ExecutionContext, arguments: Arguments) -> int:
try:
return int(arguments.s)
except ValueError:
raise ExpectedUdfException()
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest
from osprey.engine.conftest import ExecuteFunction
from osprey.engine.stdlib.udfs.parse_int import ParseInt
from osprey.engine.udf.registry import UDFRegistry

pytestmark = [pytest.mark.use_udf_registry(UDFRegistry.with_udfs(ParseInt))]


@pytest.mark.parametrize('s,expected', [('15', 15), ('04', 4), ('0', 0), ('-7', -7), ('-092', -92)])
def test_parse_int(execute: ExecuteFunction, s: str, expected: int) -> None:
data = execute(f'Result = ParseInt(s="{s}")')
assert data == {'Result': expected}


def test_parse_int_invalid(execute: ExecuteFunction) -> None:
data = execute('Result = ParseInt(s="ABC")')
assert data == {'Result': None}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from osprey.engine.stdlib.udfs.list_read import ListRead
from osprey.engine.stdlib.udfs.list_sort import ListSort
from osprey.engine.stdlib.udfs.mx_lookup import MXLookup
from osprey.engine.stdlib.udfs.parse_int import ParseInt
from osprey.engine.stdlib.udfs.phone_country import PhoneCountry
from osprey.engine.stdlib.udfs.phone_prefix import PhonePrefix
from osprey.engine.stdlib.udfs.random_bool import RandomBool
Expand Down Expand Up @@ -94,6 +95,7 @@ def register_udfs() -> Sequence[Type[UDFBase[Any, Any]]]:
ListRead,
ListSort,
MXLookup,
ParseInt,
PhoneCountry,
PhonePrefix,
RandomBool,
Expand Down
Loading